如何将环境variables传递给RUN命令

我有一个像这样docker-compose.yml文件:

 version : '2' services : s1 : build : . environment : HELLO : world 

和这样一个Dockerfile

 FROM ubuntu RUN /bin/bash -c 'echo "$HELLO" > /txt' 

我怎样才能结束与一个txt文件中的文本world中的图像?

现在,当我testing给定的例子,文件是空的!

[UPDATE]

如果我把环境variables放在Dockerfile中, Dockerfile 可以正常工作 ,这让我觉得这是一个docker-compose问题!

 FROM ubuntu ENV HELLO=world RUN /bin/bash -c 'echo "$HELLO" > /txt' 

docker-compose.yml中定义的环境在构build时将无法访问。

为了达到这个目的,你必须使用build字段的args选项(仅在docker-compose的第二版文件格式中被支持)。 它会让你添加构build参数。

https://docs.docker.com/compose/compose-file/#args

find如何使用它:

泊坞窗,compose.yml

 services : s1 : build : context: . args: HELLO: world 

此外,请注意,您将不得不在Dockerfile中使用相同的密钥名称定义ARG标记。

 FROM ubuntu ARG HELLO RUN echo "$HELLO" > /txt