简体   繁体   English

为什么`docker-compose.yml`中指定的ARGS在`Dockerfile`中可用于`docker compose run`而不是`docker compose build`?

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

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. Docker 和 Compose 相对较新,但我已经阅读了 Docker Compose 文档的每一个字母,并且在 SO 附近反弹了几个小时,但没有解决上述问题。

I have an (example) directory with the following files:我有一个(示例)目录,其中包含以下文件:

./Dockerfile : ./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 : ./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以使用args文件中指定的docker-compose.yml ,但它没有:

> 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:run工作正常:

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 :这对我来说更加莫名其妙,因为根据Compose 文档

args

Add build arguments, which are environment variables accessible only during the build process.添加构建 arguments,它们是只能在构建过程中访问的环境变量。

Any help is much appreciated任何帮助深表感谢

Build args were only recently added to compose-cli .构建参数最近才添加到 compose-cli中。 Most likely that change hasn't reached the version of docker compose you're running.很可能该更改尚未达到您正在运行的docker compose的版本。 You can use docker-compose build (with a - ) until this feature reaches your install.您可以使用docker-compose build (带有- ),直到此功能到达您的安装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM