Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to have build-only service? #4744

Closed
rivaros opened this issue Apr 19, 2017 · 6 comments
Closed

Is it possible to have build-only service? #4744

rivaros opened this issue Apr 19, 2017 · 6 comments

Comments

@rivaros
Copy link

rivaros commented Apr 19, 2017

Suppose I have the following config (simplified):

  src:
    build:
      context: deploy/src
    image: myname/base

  fpm:
    build:
      context: deploy/fpm
    image: myname/fpm

Dockerfile for fpm is located in deploy/fpm, Dockerfile for src is located in deploy/src
Dockerfile for myname/fpm image extends from myname/base image

FROM myname/base

Therefore I need to include both services (src & fpm), and make containers from them.
Is it possible to specify that src service is for build only.
So it first builds myname/base image and then myname/fpm image.

If I omit src it will not be able to find myname/base image since it was not build yet.

@shin-
Copy link

shin- commented Apr 19, 2017

See my answer to a similar question in #4489

@rivaros
Copy link
Author

rivaros commented Apr 20, 2017

@shin- thank you but it's more like "we don't support it/was not designed to do that etc". running 2 commands - this is obvious, but not nice. Have you tried any other tools like dobi?

@shin-
Copy link

shin- commented Apr 20, 2017

I have used dobi. It's a great tool, written by @dnephin who has been a Compose maintainer for a while, so I highly recommend trying it out.

@shin- shin- closed this as completed May 23, 2017
@n0099
Copy link

n0099 commented Jan 6, 2025

With

services:
  test-build-only:
    build:
      dockerfile_inline: |
        FROM scratch
    # choose one flavor between two entrypoints below
    entrypoint: sh -c exit
    entrypoint: 'true'
  some-service-build-on-test:
    build:
      args:
        COMPOSE_PROJECT_NAME: $COMPOSE_PROJECT_NAME

and in the Dockerfile of service some-service-build-on-test

FROM ${COMPOSE_PROJECT_NAME}-test-build-only

the image of services some-service-build-on-test will be build on test-build-only.

That two flavor of entrypoints only differences in running a shell sh -c with string exit to eval, or a process /bin/true to exit with code 0 that prevents docker container or compose restarting it with configured restart policy.

Make sure to build the image of service test-build-only before service some-service-build-on-test that its Dockerfile of image is based on test-build-only like manually determine the order by

docker compose build test-build-only && docker compose build

as currently there's no way to set the building order of uncached images in compose.yaml even with depends_on: #5228 (comment)

Note if you are not using services.build.dockerfile_inline, you will have to pass the compose env var $COMPOSE_PROJECT_NAME into Dockerfile via:

services:
  some-service-build-on-test:
    build:
      args:
        COMPOSE_PROJECT_NAME: $COMPOSE_PROJECT_NAME
ARG COMPOSE_PROJECT_NAME
FROM ${COMPOSE_PROJECT_NAME}-test-build-only

For your case in 8 years ago:

services:
  src:
    build:
      context: deploy/src
    image: myname/base
+    entrypoint: sh -c exit
+    entrypoint: 'true'

  fpm:
    build:
      context: deploy/fpm
      args:
        COMPOSE_PROJECT_NAME: $COMPOSE_PROJECT_NAME
    image: myname/fpm

and replace the FROM src in the dockerfile of service fpm with

ARG COMPOSE_PROJECT_NAME
FROM ${COMPOSE_PROJECT_NAME}-src

finally:

docker compose build src && docker compose build

@n0099
Copy link

n0099 commented Jan 11, 2025

And if you are using build-only service to achieve extending Dockerfile, also try build.additional_contexts.

@n0099
Copy link

n0099 commented Feb 6, 2025

  some-service-build-on-test:
    build:
      args:
        COMPOSE_PROJECT_NAME: $COMPOSE_PROJECT_NAME

and

ARG COMPOSE_PROJECT_NAME
FROM ${COMPOSE_PROJECT_NAME}-test-build-only

can be simplified with services.*.build.dockerfile_inline:

  some-service-build-on-test:
    build:
      dockerfile_inline:
        # syntax=docker/dockerfile:1
        FROM ${COMPOSE_PROJECT_NAME}-test-build-only
    depends_on:
      - test-build-only

And with

  some-service-build-on-test:
    image: ${COMPOSE_PROJECT_NAME}-test-build-only

the depends_on won't affect the building order, but the above dockerfile form will.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants