Docker-更新特定文件中的ENV变量 [英] Docker - Update ENV variable in specific file

查看:74
本文介绍了Docker-更新特定文件中的ENV变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 Dockerfile :

FROM ubuntu:18.04
USER root
WORKDIR /root/

ENV PORT 3000

COPY ./start.sh /root/
COPY ./pubsubserver /root/

RUN mkdir -p public
ADD public /root/public

CMD ["/root/start.sh"]

start.sh :

sed -i 's/"port":""/"port":"$PORT"' public/port.json

./pubsubserver --port $PORT

port.json (在构建映像并运行容器之前):

and port.json(before build image and run container):

{
"port":""
}

基本上,当我构建映像时, pubsubserver start.sh 将被复制到 Docker 的 root 文件夹中.code>,整个 public/文件夹也将被转移到 Docker root 文件夹中.

Basically, when I build the image, pubsubserver, start.sh will be copied to root folder at Docker, and the whole public/ folder will be transferred into root folder at Docker too.

但是当我运行容器时:

docker run --name demo -e "PORT=5000"

我可以让我的 pubsubserver 获得端口5000,但我的 port.json 将获得:

I can get my pubsubserver getting the port 5000, but my port.json will get:

"port":"$PORT"

我期望:

"port":"5000"

我该如何解决?

我读到有关 ENTRYPOINT 的信息,这也许将解决该问题,但是我不确定如何使它工作.

I read about ENTRYPOINT that might be about to solve this, but I am not sure how to get it to work.

推荐答案

问题出在您的sed行中.如果最外面的引号是单引号,则不会进行参数扩展.我认为您想要的是这样的:

The problem is in your sed line. If your outermost quotes are single quotes, it won't do parameter expansion. I think what you want is something like:

sed -i 's/"port":""/"port":'$PORT'/' public/port.json

但是,与其完全替换字符串"port":" ,不如使其更灵活,更模板化,而仅替换自制的 {{PORT}} 令牌:

However, instead of replacing exactly the string "port":"", why not make it a little more flexible and templatey and replace just a homemade {{PORT}} token:

port.json:

port.json:

{
"port":"{{PORT}}"
}

sed行:

sed -i "s/{{PORT}}/$PORT/" public/port.json

这样,如果您的json格式更改不会中断.

That way if your json formatting changes it won't break.

这篇关于Docker-更新特定文件中的ENV变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆