是否可以ENV根据构建的值在Dockerfile中有条件地设置变量ARG?
ENV
ARG
例如:类似
ARG BUILDVAR=sad ENV SOMEVAR=if $BUILDVAR -eq "SO"; then echo "hello"; else echo "world"; fi
更新:基于Mario的答案的当前用法:
ARG BUILD_ENV=prod ENV NODE_ENV=production RUN if [ "${BUILD_ENV}" = "test" ]; then export NODE_ENV=development; fi
但是,先运行--build-arg BUILD_ENV=test然后进入主机,我仍然可以
--build-arg BUILD_ENV=test
docker run -it mycontainer bin/bash [root@brbqw1231 /]# echo $NODE_ENV production
是的,可以,但是您需要使用build参数作为标志。您可以使用Shell的参数扩展功能来检查条件。这是概念验证的Docker文件:
FROM debian:stable ARG BUILD_DEVELOPMENT # if --build-arg BUILD_DEVELOPMENT=1, set NODE_ENV to 'development' or set to null otherwise. ENV NODE_ENV=${BUILD_DEVELOPMENT:+development} # if NODE_ENV is null, set it to 'production' (or leave as is otherwise). ENV NODE_ENV=${NODE_ENV:-production}
测试版本:
docker build --rm -t env_prod ./ ... docker run -it env_prod bash root@2a2c93f80ad3:/# echo $NODE_ENV production root@2a2c93f80ad3:/# exit docker build --rm -t env_dev --build-arg BUILD_DEVELOPMENT=1 ./ ... docker run -it env_dev bash root@2db6d7931f34:/# echo $NODE_ENV development