compose: docker-compose ups a bad build target with multiple compose files
Description of the issue
A bad build target in used on containers up when working with multiple compose files.
Seems like the image check doesn’t consider the build.target
value.
Context information (for bug reports)
Output of docker-compose version
docker-compose version 1.25.4, build 8d51620a
docker-py version: 4.1.0
CPython version: 3.7.5
OpenSSL version: OpenSSL 1.1.1d 10 Sep 2019
Output of docker version
Client: Docker Engine - Community
Version: 19.03.5
API version: 1.40
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:22:34 2019
OS/Arch: darwin/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 19.03.5
API version: 1.40 (minimum version 1.12)
Go version: go1.12.12
Git commit: 633a0ea
Built: Wed Nov 13 07:29:19 2019
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: v1.2.10
GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339
runc:
Version: 1.0.0-rc8+dev
GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657
docker-init:
Version: 0.18.0
GitCommit: fec3683
Steps to reproduce the issue
- Use
Dockerfile
:
FROM node:13 AS builder
FROM builder AS development
CMD ["echo", "DEVELOPMENT"]
FROM builder AS ci
CMD ["echo", "CI"]
- Create 3 different docker compose files:
docker-compose.yml
:
version: '3.4'
services:
api:
build:
context: .
target: development
docker-compose-ci.yml
:
version: '3.4'
services:
api:
build:
context: .
target: ci
docker-compose-local.yml
:
version: '3.4'
services:
api:
build:
context: .
target: development
-
Run
docker-compose -f docker-compose.yml -f docker-compose-ci.yml up
. -
Then run
docker-compose -f docker-compose.yml -f docker-compose-local.yml up
.
Observed result
On the 1st run, it will use ci
build target and will execute echo CI
, which is expected.
On the 2nd run, it should use development
build target, but it still uses the previous ci
target build and executes the same ci command, which is NOT expected.
The same bug happens if you will run commands in reverse order.
Expected result
Should run the expected build target based on extended docker file.
Stacktrace / full error message
grigorii-duca:testapp greg$ docker-compose -f docker-compose.yml -f docker-compose-ci.yml up
Creating network "testapp_default" with the default driver
Building api
Step 1/5 : FROM node:13 AS builder
---> f7756628c1ee
Step 2/5 : FROM builder AS development
---> f7756628c1ee
Step 3/5 : CMD ["echo", "DEVELOPMENT"]
---> Running in 960536e7bd45
Removing intermediate container 960536e7bd45
---> 2a69c4326cad
Step 4/5 : FROM builder AS ci
---> f7756628c1ee
Step 5/5 : CMD ["echo", "CI"]
---> Running in c68aa47d3686
Removing intermediate container c68aa47d3686
---> 28cd629f5770
Successfully built 28cd629f5770
Successfully tagged testapp_api:latest
WARNING: Image for service api was built because it did not already exist. To rebuild this image you must use `docker-compose build` or `docker-compose up --build`.
Creating testapp_api_1 ... done
Attaching to testapp_api_1
api_1 | CI
testapp_api_1 exited with code 0
grigorii-duca:testapp greg$ docker-compose -f docker-compose.yml -f docker-compose-local.yml up
Recreating testapp_api_1 ... done
Attaching to testapp_api_1
api_1 | CI
testapp_api_1 exited with code 0
grigorii-duca:testapp greg$
Additional information
MacOS
docker-compose -f docker-compose.yml -f docker-compose-ci.yml config
services:
api:
build:
context: /htdocs/combats/testapp
target: ci
version: '3.4'
docker-compose -f docker-compose.yml -f docker-compose-local.yml config
services:
api:
build:
context: /htdocs/combats/testapp
target: development
version: '3.4'
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 27
- Comments: 23 (1 by maintainers)
Commits related to this issue
- Docker-compose: no image name to build right image https://github.com/docker/compose/issues/7262 — committed to playerla/flask-mega-tutorial by playerla 4 years ago
This issue has been automatically marked as not stale anymore due to the recent activity.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I had problems with this in github actions, but no in my local. I added
DOCKER_BUILDKIT: 1
thinking that docker-compose it’s just a wrapper of docker, and it worked.@DracotMolver you might be missing DOCKER_BUILDKIT=1
https://stackoverflow.com/a/55320725/368144
@eweidner For me, rebuilding does not fix it, and I’m not running multiple compose files at once. Do you think it’s a different issue?
In my case @bog-h i only work with one docker compose file. I’m using version 3.7
This is happening to me but with a different configuration. It’s creating dangling images because when I target to
prod
it creates thedev
stage as well, and this stage was previously created.My docker file (I removed some info):
My docker-compose (I removed some info too):
If I run
docker-compose up
for the very first time. It will pull the node image and then will use it as a base for thedev
stage. The container is up and running fine.base stage
… I skipped all the instalation of npm
dev stage

But then I face 2 situations when I change the target:
target
value toprod
, thedocker-compose up
doesn’t rebuild the image based on that stage on the Dockerfile. Instead, it’s setting up my container like it was in development mode (devstage). Doesn’tdocker-compose up
build (rebuild) the image if it doesn’t exist (prod stage doesn’t exist yet) and then run the container?.rebuild dev stage
docker-compose build
to avoid the step above. It rebuild the base image from cache, perfect!. Thedev
stage now is dangle because theprod
stage has the same label (Am I right why the dangle exist?). But when I see the terminal, it is building thedev
stage and then theprod
. It make sense If I think docker-compose build runs through all the Dockerfile. If that the case, it is inevitable don’t go through thedev
stage.images after all of that
Now. If I change again to
dev
and rebuild the image withdocker-compose up server --build
. It dangled the prev image (prod stage) and create again thedev
from scratch, not form the cache. so know i have more dangle images, and this cycle will never ends.What I finally ended up doing was:
docker build ./server -t server-dev --target=dev
anddocker build ./server -t server-prod --target=prod
There’s a few different things going on in this issue:
target
isn’t doing anything at all, it’s likely that you’re not building with BuildKit. Refer to @rafawhs’s comment.then running
compose up
then changing the Compose file to:and running
compose up
again, the image won’t be rebuilt according to the new target. A quick fix to this is runningcompose up --build
, to force image rebuilding. This same issue is what was seen in the original issue, but with overwriting thetarget
attribute of the build section. This doesn’t actually have anything to do with multiple Compose files, but with how/when images are built. After Compose has built the image for the service (with whatever target was defined at the time), the image won’t be rebuilt in consequentup
s, unless specified.Please comment (feel free to @ me) if there are other issues other than these.
Same issue here running
docker-compose.yml
with adocker-compose.override.yml
file.It worked with BuildKit enabled, as sugested by @infinito84
More about BuildKit: What is BuildKit? - Introducing BuildKit
Hello Guys! I’m glad that it is a common problem because I started to lose my mind 😄
Looks like docker-compose override is ignoring build: target argument. I thought the problem is in the cache, but not!
If you will specify a target (ex debug or base) in the main docker-compose.yaml file then everything will work as expected in depends on your target build: target: debug|base
But if you will specify the target in an override file (ex: docker-compose.debug.yaml) then this parameter is ignored!