在Ubuntu中的运行级别脚本中将环境variables传递给docker容器

我知道如何将环境variables传递给Docker容器。 喜欢

sudo docker run -e ENV1='ENV1_VALUE' -e ENV2='ENV2_VALUE1' .... 

我能够成功地select这些variables,如果我从docker容器内的shell运行脚本。 但是docker实例的runlevel脚本无法看到传递给docker容器的环境variables。 最终所有的服务/守护进程都以默认configuration开始,我不想要。

请为此提出一些解决scheme。

 You can manage your run level services to pick environment variables passed to Docker instance. For example here is my docker file(Dockerfile): .... .... # Adding my_service into docker file system and enabling it. ADD my_service /etc/init.d/my_service RUN update-rc.d my_service defaults RUN update-rc.d my_service enable # Adding entrypoint script COPY ./entrypoint.sh /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] # Set environment variables. ENV HOME /root # Define default command. CMD ["bash"] .... Entrypoint file can save the environment variables to a file,from which you service(started from Entrypoint script) can source the variables. entrypoint.sh contains: #!/bin/bash -x set -e printenv | sed 's/^\(.*\)$/export \1/g' > /root/project_env.sh service my_service start exec /bin/bash Inside my_service, I am sourcing the variables from /root/project_env.sh like: #!/bin/bash set -e . /root/project_env.sh I hope it solve your problem. This way you need NOT to depend on external file system, provide your are passing variables to your docker instance at the time of running it. 

在Dockerfile中,您可以select在运行时指定环境variables。

Dockerfile

 FROM ubuntu:16.04 ENV foo=bar ENV eggs=spam RUN <some runtime command> CMD <entrypoint> 

查看docker环境replace文档了解更多信息。

运行级脚本将无法读取环境variables,我认为这是一件好事。

您可以尝试将环境variables放在主机上的一个文件中,将其挂载到docker容器中的一个文件中(例如在/etc/init.d/下)。 在运行脚本之前,更改docker镜像的init脚本以获取已安装的文件。

Interesting Posts