gpt4 book ai didi

shell - 如何在Dockerfile中将环境变量作为Shell脚本的参数传递?

转载 作者:行者123 更新时间:2023-12-02 20:37:57 26 4
gpt4 key购买 nike

如何在运行时将环境变量作为Dockerfile中的Shell脚本的参数传递?

PUBLISH_HOST来自.env文件

我的docker-compose文件如下所示:

version: '2'
services:
tomcat:
build:
context: .
args:
PUBLISH_HOST: ${PUBLISH_HOST}
dockerfile: Dockerfile.tomcat
container_name: tomcat
image: tomcat-new:latest
restart: always
ports:
- 8080:8080

Dockerfile如下:
FROM 99taxis/tomcat7
ARG PUBLISH_HOST //taken from docker-compose for all other this is working fine not working while passing in a shell script
ENV PUBLISH_HOST $MAXPAAS_PUBLISH_HOST
COPY update-tomcat-path-config.sh /opt/tomcat/webapps
RUN cd /opt/tomcat/webapps && chmod 755 update-tomcat-path-config.sh && ./update-tomcat-path-config.sh ${PUBLISH_HOST}

但是输入的 ${PUBLISH_HOST}在此shell脚本中未被接受为参数。

有什么办法可以解决这个问题?

这是我在尝试 docker-compose up时收到的错误消息
Step 25/37 : /opt/tomcat/webapps && chmod 755 update-tomcat-path-config.sh && /update-tomcat-path-config.sh ${PUBLISH_HOST} 
---> Running in 8ad3b1ecd73e

###################
Script starts Here:
###################

No parameter to the script is given, please enter it....!!!!

如何解决呢?

最佳答案

参见“Setting Default Docker Environment Variables During Image Build

您的行应为:

ARG PUBLISH_HOST //taken from docker-compose for all other this is working fine not working while passing in a shell script
ENV PUBLISH_HOST $PUBLISH_HOST

这将允许 ENV在构建时被 ARG值覆盖:
docker build --build-arg PUBLISH_HOST=aValue

对于 docker-compose build
docker-compose build --build-arg PUBLISH_HOST=aValue aSERVICE

注意:

您的 docker-compose.yml可能不使用您的dockerfile,因为 dockerfile: Dockerfile.tomcat没有正确缩进。
参见 docker-compose.yml "alternate Dockerfile":

Compose uses an alternate file to build with.
A build path must also be specified.


build:
context: .
dockerfile: Dockerfile-alternate

关于shell - 如何在Dockerfile中将环境变量作为Shell脚本的参数传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51625481/

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