docker ENV覆盖不起作用

问题描述 投票:0回答:1

我想替换docker图片中的文本所以我用ENV代替文字

但是当我在ENV中放入文字时,不会覆盖ENV

我的dockerfile

RUN apt-get update
RUN apt-get install -y nginx
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf

# Define mountable directories.
VOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/etc/nginx/conf.d", "/var/log/nginx", "/var/www/html"]

ENV PROXY_PASS http://localhost:3000
ENV SERVER_NAME myserver.com
ENV PEM_PATH /etc/nginx/certs/cert.pem
ENV KEY_PATH /etc/nginx/certs/cert.key

WORKDIR /etc/nginx

RUN rm -f sites-enabled/default
ADD ./sites-available/ssl /etc/nginx/sites-available/ssl
RUN sed -ri 's@PROXY_PASS@'${PROXY_PASS}'@' sites-available/ssl
RUN sed -ri 's@SERVER_NAME@'${SERVER_NAME}'@' sites-available/ssl
RUN sed -ri 's@PEM_PATH@'${PEM_PATH}'@' sites-available/ssl
RUN sed -ri 's@KEY_PATH@'${KEY_PATH}'@' sites-available/ssl
RUN cp -f sites-available/ssl sites-available/default
#RUN ln -s /etc/nginx/sites-available/ssl sites-enabled/default

EXPOSE 80 443

CMD ["nginx"]

目标文件:ssl

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        listen 443 ssl default_server;
        listen [::]:443 ssl default_server;

        ssl    on;
        ssl_certificate  PEM_PATH;
        ssl_certificate_key   KEY_PATH;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name SERVER_NAME;

        location / {
                        # First attempt to serve request as file, then
                        # as directory, then fall back to displaying a 404.
                        proxy_pass PROXY_PASS;
                        proxy_http_version 1.1;
                        proxy_set_header Upgrade $http_upgrade;
                        proxy_set_header Connection 'upgrade';
                        proxy_set_header Host $host;
                        proxy_cache_bypass $http_upgrade;
        }
}

我的执行管理员

docker run -d -e "SERVER_NAME=docker.havehad.kr" --name nginx-ssl -p 443:443 --rm nginx-ssl

当我运行docker image时,ssl文件中的SERVER_NAME应该替换为'docker.havehad.kr',但没有它仍然保留为“ myserver.com”我不知道是什么问题请帮助我

docker nginx environment-variables
1个回答
0
投票

[在处理docker映像时,您有2个不同的上下文:build上下文(docker build命令)和run上下文(docker run命令)。

来自dockerfile reference

ENV指令将环境变量设置为value。此值将在构建阶段的所有后续指令所在的环境中使用,也可以在很多情况下内联替换。

ARG指令定义了一个用户可以在其处传递的变量使用docker build命令构建到构建器的时间--build-arg =标志。如果用户指定了Dockerfile中未定义的构建参数,则该构建会输出一个警告。

似乎您需要一种方法来更改Dockerfile中使用的变量以更改sed命令。您应该使用参数来实现这一点,并且可以将它们与环境变量组合以在运行时使它们可用(docker run命令)

RUN apt-get update
RUN apt-get install -y nginx
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf

# Define mountable directories.
VOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/etc/nginx/conf.d", "/var/log/nginx", "/var/www/html"]

ARG PROXY_PASS=http://localhost:3000
ARG SERVER_NAME=myserver.com
ARG PEM_PATH=/etc/nginx/certs/cert.pem
ARG KEY_PATH=/etc/nginx/certs/cert.key

ENV PROXY_PASS ${PROXY_PASS}
ENV SERVER_NAME ${SERVER_NAME}
ENV PEM_PATH ${PEM_PATH}
ENV KEY_PATH ${KEY_PATH}


WORKDIR /etc/nginx

RUN rm -f sites-enabled/default
ADD ./sites-available/ssl /etc/nginx/sites-available/ssl
RUN sed -ri 's@PROXY_PASS@${PROXY_PASS}@' sites-available/ssl
RUN sed -ri 's@SERVER_NAME@${SERVER_NAME}@' sites-available/ssl
RUN sed -ri 's@PEM_PATH@${PEM_PATH}@' sites-available/ssl
RUN sed -ri 's@KEY_PATH@${KEY_PATH}@' sites-available/ssl
RUN cp -f sites-available/ssl sites-available/default
#RUN ln -s /etc/nginx/sites-available/ssl sites-enabled/default

EXPOSE 80 443

CMD ["nginx"]

并且您可以在构建图像时更改参数值:

$ docker build --build-arg SERVER_NAME=docker.havehad.kr .

您的${SERVER_NAME}参数将在Dockerfile指令(ENV和RUN)中替换为docker.havehad.kr

如果需要在运行时(docker运行)更改sed命令,请将其放入外壳脚本中并用作ENTRYPOINT

热门问题
推荐问题
最新问题