compose.ymlからDockerfileのARGを設定する方法。

Dockerfileはこう。VERSIONを指定しない状態だとlatestが使われる。

ARG VERSION=latest
FROM nginx:${VERSION}

ENTRYPOINT ["nginx", "-v"]

compose.ymlはこう。

services:
  web:
    build:
      context: .

現時点でのlatestは 1.27.3 なのでそれが使われる。

$ docker compose run web
nginx version: nginx/1.27.3

build args でバージョンを与える

compose.ymlをこう書き換える。argsVERSIONを指定する。

services:
  web:
    build:
      context: .
      args:
        VERSION: 1.26

build する。

$ docker compose build web
[+] Building 1.8s (6/6) FINISHED                                                                                                                                       docker:desktop-linux
 => [web internal] load build definition from Dockerfile                                                                                                                               0.0s
 => => transferring dockerfile: 143B                                                                                                                                                   0.0s
 => [web internal] load metadata for docker.io/library/nginx:1.26                                                                                                                      1.7s
 => [web internal] load .dockerignore                                                                                                                                                  0.0s
 => => transferring context: 2B                                                                                                                                                        0.0s
 => CACHED [web 1/1] FROM docker.io/library/nginx:1.26@sha256:df6f3c8e3fb6161cc5e85c8db042c8e62cfb7948fc4d6fddfad32741c3e2520d                                                         0.0s
 => => resolve docker.io/library/nginx:1.26@sha256:df6f3c8e3fb6161cc5e85c8db042c8e62cfb7948fc4d6fddfad32741c3e2520d                                                                    0.0s
 => [web] exporting to image                                                                                                                                                           0.0s
 => => exporting layers                                                                                                                                                                0.0s
 => => exporting manifest sha256:3b8968dc218a15a95488513857a1a17bdeeff48d197085f9a02557cd76e3a341                                                                                      0.0s
 => => exporting config sha256:83ee5e84205a7d733f02ad9d195ec0feabc4458df7f4c9ee98c0b7ebf9136bb5                                                                                        0.0s
 => => exporting attestation manifest sha256:395fadef7d8b1e56a13f974534ab62b6112afea955ae04ebe85a469349522462                                                                          0.0s
 => => exporting manifest list sha256:92797de465fcf79dcf2625f60328aa08edb026855e255d1a3d7ec34e31687070                                                                                 0.0s
 => => naming to docker.io/library/docker-arg-web:latest                                                                                                                               0.0s
 => => unpacking to docker.io/library/docker-arg-web:latest                                                                                                                            0.0s
 => [web] resolving provenance for metadata file                                                                                                                                       0.0s

runする。

$ docker compose run web
nginx version: nginx/1.26.2

参考