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

时间:2018-08-01 04:44:34

标签: shell docker dockerfile

如何在运行时将环境变量作为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....!!!!

该如何解决?

2 个答案:

答案 0 :(得分:2)

我找到了答案。

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

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

答案 1 :(得分:1)

请参见“ 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使用一个替代文件进行构建。
  还必须指定一个构建路径。

build:
  context: .
  dockerfile: Dockerfile-alternate
相关问题