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

docker-compose multiple dockerfiles which are interdependent #4489

Closed
immae1 opened this issue Feb 15, 2017 · 7 comments
Closed

docker-compose multiple dockerfiles which are interdependent #4489

immae1 opened this issue Feb 15, 2017 · 7 comments

Comments

@immae1
Copy link

immae1 commented Feb 15, 2017

hi there i have a question:

  1. Dockerfile (This is my base image for production ready env):
FROM ubuntu:trusty

# install system packages
RUN apt-get update -qq -y && export DEBIAN_FRONTEND=noninteractive \
    && apt-get install -qq -y gettext git libffi-dev libpq-dev \
     python python-pip python-dev \
...

the resulted images must tagged as : web_base

  1. Dockerfile (this is my dev image which is interdependent with the first dockerfile - see the FROM Part):
FROM web_base
USER root
# install system packages
RUN apt-get update -qq -y && export DEBIAN_FRONTEND=noninteractive \
        && apt-get install -qq -y git openssh-server vim wget
....

My docker-compose_dev.yml:

...
web_base:
    build: .
    image: web_base
  web:
    build:
      context: .
      dockerfile: Dockerfile.dev
    security_opt:
      - apparmor=docker-default
      - no-new-privileges
    cap_drop:
      - ALL
    cap_add:
      - CHOWN
      - DAC_OVERRIDE
      - SETGID
      - SETUID
      - NET_BIND_SERVICE
      - SYS_CHROOT
      - AUDIT_WRITE
    depends_on:
      - db
      - db_templates
      - redis
      - web_base

if i run docker-compose -f docker-compose_dev.yml up --build
the first dockerfile is builded. The seccond dockerfile which depends on the first dockerfile is builded and the web container starts. but also from the first dockerfile a container named "web_base" is created which stops.

I want to build the 1. Dockerfile and then the 2. Dockerfile and create only 1 Container named web, how could this be reached?
I know that i could run docker build . -t web_base and then run docker-compose without the "web_base" service.But than i have to execute 2 commands...

thanks

@shin-
Copy link

shin- commented Feb 15, 2017

Compose is straight up not designed to solve that problem. As you noted, your best bet right now is to run two commands. If you expect your configuration to grow more complex in the future, you might also want to take a look at dobi or similar tools.

@immae1
Copy link
Author

immae1 commented Feb 16, 2017

@shin ok thx. You can remove this issue.

@larytet
Copy link

larytet commented Jun 18, 2018

@immae1 You can add another abstraction layer and generate the Dockerfiles dynamically depending on what you need right now. A YAML and a short Python script can do the trick. Check, for example, my approach here https://github.com/larytet/dockerfile-generator

@larsblumberg
Copy link

larsblumberg commented May 8, 2021

I have the same use case. There's one service app-base in my docker-compose.yaml which merely makes sure that a base image gets built which gets inherited by another (or more) services. The following example shows the required ingredients to get this to work:

app/Dockerfile:

FROM app-base
COPY ...
RUN ...

docker-compose.yaml:

app-base:
    command: "tail -F /dev/null"  # A pseudo, do-nothing command which runs as long as docker-compose is up
    image: app-base  # Necessary so that above app/Dockerfile can be built

app:
    depends_on:
    - app-base
    ...

As stated in the above issue comments, a run command needs to be specified. I chose tail -F /dev/null b/c it is available in most images: it does basically nothing, does never fail and runs forever as docker-compose is up.

@Johnson145
Copy link

I know this is an old issue, but just in case anyone else is getting here from Google etc.:

Be aware that the previously described approach does not work with the current docker-compose v2. The deprecated v1 used to build images sequentially in the order as defined in the .yml file. At least by default, the new implementation builds the images concurrently, which breaks the presented approach by introducing race conditions. As a workaround you can use DOCKER_BUILDKIT: 0 to enforce using the old implementation.

@laurazard
Copy link
Contributor

You can also force Compose to run the builds one at a time (non-concurrently) without disabling BuildKit by using --parallel, such as docker compose --parallel 1 build.

@n0099
Copy link

n0099 commented Nov 7, 2024

the new implementation builds the images concurrently, which breaks the presented approach by introducing race conditions.

depends_on

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

7 participants