如何在Dockerfile的RUN中设置环境变量?

时间:2019-12-20 14:46:05

标签: docker docker-compose dockerfile containers

我需要将RUN命令的输出设置为环境变量,该变量在构建容器后就可以在我的容器中使用:

...

RUN ...
    GO111MODULE=on go get github.com/emersion/hydroxide/cmd/hydroxide && \
    echo "the_password" | hydroxide auth the_username@protonmail.com > bridge_password.txt && \
    BRIDGE_PASSWORD=`sed -n 's/Password: Bridge password: //p' bridge_password.txt` && \
    hydroxide smtp

...

上面,我需要$BRIDGE_PASSWORD可以在我的NodeJS项目(process.env.BRIDGE_PASSWORD)中使用。我该如何实现?

1 个答案:

答案 0 :(得分:0)

您需要:

  1. 使用Dockerfile中的ENV命令声明环境变量。
  2. 在图像入口点中用命令替换当前的RUN,以便每次启动新容器时都能正确设置变量,并且该变量可用于任何已启动的命令。