Skip to content

[BUG] docker compose build <service_name> fails with "invalid reference format" when using additional_contexts #12673

@zvchei

Description

@zvchei

Description

When attempting to build a specific service using docker compose build <service_name>, and that service is defined using additional_contexts to stage another service image, the build process fails with either an "invalid reference format" error or, when using COMPOSE_BAKE=true, a "failed to find target" error.

Steps To Reproduce

Create a docker-compose.yaml file with the following content :

services:
  base:
    build:
      context: .
      dockerfile_inline: |
        FROM alpine
  my-service:
    build:
      context: .
      dockerfile_inline: |
        FROM base
    additional_contexts:
      base: service:base

Run docker compose build to confirm both services build successfully.

$ docker compose build 

[+] Building 2/2
 ✔ base        Built
 ✔ my-service  Built 

Attempt to build only the dependent service:

$ docker compose build my-service

failed to solve: invalid reference format

Alternatively, try building with COMPOSE_BAKE=true:

$ COMPOSE_BAKE=true docker compose build my-service

failed to find target base

For reference, building the base service alone is done without issues:

$ docker compose build base

[+] Building 1/1
 ✔ base  Built

Compose Version

Docker Compose version v2.34.0

Docker Environment

Client: Docker Engine - Community
 Version:    28.0.4
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.22.0
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.34.0
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
 Containers: 5
  Running: 0
  Paused: 0
  Stopped: 5
 Images: 63
 Server Version: 28.0.4
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Using metacopy: false
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 nvidia runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 753481ec61c7c8955a23d6ff7bc8e4daed455734
 runc version: v1.2.5-0-g59923ef
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: builtin
  userns
  cgroupns
 Kernel Version: 6.11.0-19-generic
 Operating System: Ubuntu 24.04.2 LTS
 OSType: linux
 Architecture: x86_64
 CPUs: 24
 Total Memory: 62.4GiB
 Name: valhalla
 ID: 17c74018-ea57-48af-8c04-23a98ae5ab7e
 Docker Root Dir: /var/lib/docker/231072.231072
 Debug Mode: false
 Experimental: false
 Insecure Registries:
  ::1/128
  127.0.0.0/8
 Live Restore Enabled: false

Anything else?

No response

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions