Skip to content

docker-compose ups a bad build target with multiple compose files #7262

Description

@greg-md

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

  1. Use Dockerfile:
FROM node:13 AS builder

FROM builder AS development

CMD ["echo", "DEVELOPMENT"]

FROM builder AS ci

CMD ["echo", "CI"]
  1. 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
  1. Run docker-compose -f docker-compose.yml -f docker-compose-ci.yml up.

  2. 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'

Metadata

Metadata

Assignees

Labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions