docker-compose构建args不传递给Dockerfile [英] docker-compose build args not passing to Dockerfile
问题描述
docker-compose.yml:
docker-compose.yml:
version: "3"
services:
ei:
build:
context: .
dockerfile: Dockerfile
args:
NODE_VERSION: 8
HELLO: 5
Dockerfile:
Dockerfile:
ARG NODE_VERSION
ARG HELLO
FROM node:$NODE_VERSION
RUN echo "-> $HELLO"
RUN echo "-> $NODE_VERSION"
结果:
km@Karls-MBP ~/dev/ve (km/ref) $ docker-compose -f docker-compose.yml build --no-cache
vertica uses an image, skipping
Building ei
Step 1/14 : ARG NODE_VERSION
Step 2/14 : ARG HELLO
Step 3/14 : FROM node:$NODE_VERSION
---> e63de54eee16
Step 4/14 : RUN echo "-> $HELLO"
---> Running in e93d89e15913
->
Removing intermediate container e93d89e15913
---> c305b277291c
Step 5/14 : RUN echo "-> $NODE_VERSION"
---> Running in 39e8e656c0bd
-> 8
我正在摸索为什么这行不通。如果更改节点版本号,则数字也会更改。
I'm scratching my head as to why this isn't working. If I change the node version number the number changes.
推荐答案
撰写文件上定义的参数在Dockerfile上可用,但仅 FROM
之前和之后。在 FROM
之后,参数不可用:
The defined arguments on the compose file are available on the Dockerfile but only before and on the FROM
. After the FROM
the arguments are not available:
An
FROM
之前声明的> ARG 在构建阶段之外,因此在从
。 - 来自docker文档
An
ARG
declared before aFROM
is outside of a build stage, so it can't be used in any instruction after aFROM
. - from docker docs
为什么参数 NODE_VERSION
起作用?
NODE_VERSION
不起作用。该参数仅在 FROM
( FROM节点:8
)上使用。在 FROM
之后,有一个图片的环境变量具有相同的名称。因此,您回显图像的环境变量,而不是撰写文件的参数。
Why is the argument NODE_VERSION
working?
The argument NODE_VERSION
isn't working after the FROM
. The argument is only used on the FROM
(FROM node:8
). After FROM
there is a environment variable of the image with the same name. So you echo the environment variable of the image instead of the argument of your compose file.
但是您可以在之后使用参数的默认值FROM
:
使用
ARG $ c $的默认值在第一个
FROM
之前声明的c>使用ARG
指令,在构建阶段内部不带任何值。 - 来自docker文档
To use the default value of an
ARG
declared before the firstFROM
use anARG
instruction without a value inside of a build stage. - from docker docs
ARG NODE_VERSION
FROM node:$NODE_VERSION
ARG HELLO
RUN echo "-> $HELLO"
RUN echo "-> $NODE_VERSION"
要使用和显示参数中定义的节点版本,您需要重命名此参数。因此,您可以使用以下命令显示图像的所有参数和环境变量:
To use and show the node version defined in the arguments you need to rename this argument. So you can use the following to show all your arguments and the environment variable of the image:
Dockerfile:
ARG CUSTOM_NODE_VERSION
FROM node:$CUSTOM_NODE_VERSION
ARG CUSTOM_NODE_VERSION
ARG HELLO
RUN echo "-> $HELLO" #output: 5
RUN echo "-> $NODE_VERSION" #output: 8.9.4
RUN echo "-> $CUSTOM_NODE_VERSION" #output: 8
docker-compose.yml:
version: "3"
services:
ei:
build:
context: .
dockerfile: Dockerfile
args:
CUSTOM_NODE_VERSION: 8
HELLO: 5
这篇关于docker-compose构建args不传递给Dockerfile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!