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

如何将环境变量作为Dockerfile中shell脚本的参数传递?,shell,docker,dockerfile,Shell,Docker,Dockerfile,如何在运行时将环境变量作为Dockerfile中shell脚本的参数传递 发布主机取自.env文件 我的docker compose文件如下所示: version: '2' services: tomcat: build: context: . args: PUBLISH_HOST: ${PUBLISH_HOST} dockerfile: Dockerfile.tomcat container_name: tomcat

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

发布主机取自.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}
但是在这个shell脚本中,输入
${PUBLISH\u HOST}
不被接受为参数

有没有办法克服这个问题

这是我在尝试编写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....!!!!
如何解决此问题?

请参阅“”

你的台词应该是:

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.yml
可能不使用您的dockerfile,因为
dockerfile:dockerfile.tomcat
缩进不正确。
见:

Compose使用备用文件进行构建。
还必须指定生成路径

见“

你的台词应该是:

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.yml
可能不使用您的dockerfile,因为
dockerfile:dockerfile.tomcat
缩进不正确。
见:

Compose使用备用文件进行构建。
还必须指定生成路径


我找到了答案

要在shell脚本中传递参数,只需在参数之间使用双引号

RUN cd /opt/tomcat/webapps && chmod 755 update-tomcat-path-config.sh && \
    ./update-tomcat-path-config.sh "${PUBLISH_HOST}"
#                                  ^               ^

我找到了答案

要在shell脚本中传递参数,只需在参数之间使用双引号

RUN cd /opt/tomcat/webapps && chmod 755 update-tomcat-path-config.sh && \
    ./update-tomcat-path-config.sh "${PUBLISH_HOST}"
#                                  ^               ^

可能重复的“我认为它不是我的问题”无法在dockerfile内的shell脚本中作为参数传递环境变量“它将不接受”不是问题描述。发生了什么?你期望发生什么?而
ENV PUBLISH\u HOST$MAXPAAS\u PUBLISH\u HOST
应该做什么?可能的重复我认为它不是我的问题无法将ENV变量作为dockerfile内shell脚本中的参数传递。文件“it will not accept”不是问题描述。发生了什么?你期望发生什么?而
ENV PUBLISH_HOST$MAXPAAS_PUBLISH_HOST
应该做什么呢?我需要传递一个环境变量作为shell脚本的参数,但在尝试使用如下参数传递时${PUBLISH_HOST},它不起作用。抱歉,它是自动编辑的。我试图传递参数,因为${PUBLISH\u HOST}它在shelldocker compose中不被接受意味着运行时。ARG仅用于构建时。“它不会接受”是什么意思?如何使用dokcer compose传递值?你在打什么?请编辑您的问题。@AnishVarghese不要评论:请编辑您的问题。@AnishVarghese感谢您编辑的问题(我重新格式化了它)。请参阅我编辑的答案。我需要将环境变量作为shell脚本的参数传递,但在尝试使用以下参数传递时${PUBLISH\u HOST},它不工作。抱歉,它是自动编辑的。我试图传递参数,因为${PUBLISH\u HOST}它在shelldocker compose中不被接受意味着运行时。ARG仅用于构建时。“它不会接受”是什么意思?如何使用dokcer compose传递值?你在打什么?请编辑您的问题。@AnishVarghese不要评论:请编辑您的问题。@AnishVarghese感谢您编辑的问题(我重新格式化了它)。看我编辑过的答案。接得好+1.还是不确定你的dockerfile论点。很好+1.但仍然不能确定dockerfile的论点。