docker-compose build
does not follow the order in which the services are specified.
docker-compose build base prod dev
will actually try to build in the order base
, dev
and prod
,
which is alphabetical order and not the order specified on the command line.
Hi @vochicong
Thank you for your report. We don't consider this to be a bug. Is this causing issues with your application?
Hi @shin- . Yes, I have dev
built upon prod
upon base
.
#Dockerfile.base
FROM ruby
...
#Dockerfile.prod
FROM myapp-base
...
#Dockerfile.dev
FROM myapp-prod
...
#docker-compose.yml
version: '2'
services:
base:
image: myapp-base
build:
context: .
dockerfile: Dockerfile.base
prod:
extends: base
image: myapp-prod
build:
context: .
dockerfile: Dockerfile.prod
dev:
extends: prod
image: myapp-dev
build:
context: .
dockerfile: Dockerfile.dev
Ok, one simple solution would be to run docker-compose build base && docker-compose build prod && docker-compose build dev
instead, but if you have some time, I highly recommend you look into using multi-stage builds: https://docs.docker.com/engine/userguide/eng-image/multistage-build/
@shin- I would like to re-open this, I am following multi stage builds that depends on another multi stage builds (micorservices). I would like the docker compose to build in the order specified , so that we build our common base first, then depends all our services on top of this. If the common base image exists already, compose skips it even though the Dockerfile for the base image have been changed, and the dependent images use the old image, literally wanting me to run compose twice. (BTW i dont want to use a script that builds it as compose definition is super easy to pass metadata as env variables during build).
Users would expect that docker compose builds images in the order specified.
Multi-stage build is for building an image efficiently.
I run into the same problem but solved by reordering the service definitions in docker-compose.yaml file. docker-compose follows the service order defined in that file. See: https://github.com/docker/compose/blob/e9220f45df07c1f884d5d496507778d2cd4a1687/compose/project.py#L182-L183
Most helpful comment
Users would expect that docker compose builds images in the order specified.
Multi-stage build is for building an image efficiently.