Why are ARGS specified in `docker-compose.yml` available in `Dockerfile` for `docker compose run` but not `docker compose build`?

Viewed 176

Relatively new to Docker and Compose, but I have read every letter of the Docker Compose documentation, and unsuccessfully bounced around SO for hours, with no resolution to the above question.

I have an (example) directory with the following files:

./Dockerfile:

# syntax=docker/dockerfile:1
ARG CUSTOM_NODE_VERSION

FROM node:$CUSTOM_NODE_VERSION

ARG CUSTOM_NODE_VERSION
ARG HELLO

RUN echo "HELLO: -> $HELLO"
RUN echo "NODE_VERSION -> $NODE_VERSION"
RUN echo "CUSTOM_NODE_VERSION -> $CUSTOM_NODE_VERSION"

./docker-compose.yml:

version: "3.8"

services:
  test:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        CUSTOM_NODE_VERSION: alpine
        HELLO: 5

What I want is for docker compose build to use the args specified in the docker-compose.yml file, but it doesn't:

> docker compose build test           
[+] Building 0.8s (4/4) FINISHED                                                                                          
 => [internal] load build definition from Dockerfile                                                                 0.1s
 => => transferring dockerfile: 32B                                                                                  0.0s
 => [internal] load .dockerignore                                                                                    0.0s
 => => transferring context: 2B                                                                                      0.0s
 => resolve image config for docker.io/docker/dockerfile:1                                                           0.5s
 => CACHED docker-image://docker.io/docker/dockerfile:1@sha256:e2a8561e419ab1ba6b2fe6cbdf49fd92b95912df1cf7d313c3e2  0.0s
failed to solve: rpc error: code = Unknown desc = failed to solve with frontend dockerfile.v0: failed to solve with frontend gateway.v0: rpc error: code = Unknown desc = failed to create LLB definition: failed to parse stage name "node:": invalid reference format

But run works fine:

docker compose run test  
[+] Running 1/1
 ⠿ Network compose-args_default  Created                                                                             4.3s
[+] Building 3.1s (10/10) FINISHED                                                                                        
 => [internal] load build definition from Dockerfile                                                                 0.0s
 => => transferring dockerfile: 32B                                                                                  0.0s
 => [internal] load .dockerignore                                                                                    0.0s
 => => transferring context: 2B                                                                                      0.0s
 => resolve image config for docker.io/docker/dockerfile:1                                                           0.5s
 => CACHED docker-image://docker.io/docker/dockerfile:1@sha256:e2a8561e419ab1ba6b2fe6cbdf49fd92b95912df1cf7d313c3e2  0.0s
 => [internal] load metadata for docker.io/library/node:alpine                                                       1.0s
 => CACHED [1/4] FROM docker.io/library/node:alpine@sha256:f372a9ffcec27159dc9623bad29997a1b61eafbb145dbf4f7a64568b  0.0s
 => [2/4] RUN echo "HELLO: -> 5"                                                                                     0.5s
 => [3/4] RUN echo "NODE_VERSION -> 16.3.0"                                                                          0.3s
 => [4/4] RUN echo "CUSTOM_NODE_VERSION -> alpine"                                                                   0.5s
 => exporting to image                                                                                               0.1s
 => => exporting layers                                                                                              0.0s
 => => writing image sha256:e61653277599e3555b67c1a50699dd83d5c1ed1a93fe8a1a16529c6ec20e3e31                         0.0s
 => => naming to docker.io/library/compose-args_test 

This is all the more baffling to me because, per the Compose docs:

args

Add build arguments, which are environment variables accessible only during the build process.

Any help is much appreciated

1 Answers

Build args were only recently added to compose-cli. Most likely that change hasn't reached the version of docker compose you're running. You can use docker-compose build (with a -) until this feature reaches your install.

Related