gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 07:33:23 65 4
gpt4 key购买 nike

对 Docker 和 Compose 来说相对较新,但我已经阅读了 Docker Compose 文档的每一封信,并且在 SO 周围徘徊了几个小时没有成功,没有解决上述问题。
我有一个(示例)目录,其中包含以下文件:./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
我想要的是 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
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
这对我来说更令人困惑,因为根据 Compose docs :

args

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


非常感谢任何帮助🙏🏻

最佳答案

构建参数为 only recently added to compose-cli .最有可能的变化还没有达到 docker compose 的版本你在运行。您可以使用 docker-compose build (带有 - )直到此功能到达您的安装。

关于docker - 为什么 `docker-compose.yml` 中指定的 ARGS 在 `Dockerfile` 中可用于 `docker compose run` 而不是 `docker compose build` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67853731/

65 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com