我知道如何将环境变量传递给docker容器。喜欢
sudo docker run -e ENV1='ENV1_VALUE' -e ENV2='ENV2_VALUE1' ....如果我从docker容器中的shell中运行脚本,我就能够成功地选择这些变量。但是,docker实例的运行级脚本无法看到传递给docker容器的环境变量。最终,所有服务/守护进程都是以默认配置启动的,而我不想要这种配置。
请提出一些解决办法。
发布于 2017-03-28 10:47:33
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.发布于 2017-03-15 20:43:47
在Dockerfile中,您可以选择在运行时之前指定环境变量。
Dockerfile
FROM ubuntu:16.04
ENV foo=bar
ENV eggs=spam
RUN <some runtime command>
CMD <entrypoint>查看在码头环境替换文档以获得更多信息。
发布于 2017-03-18 05:25:46
运行级脚本将无法读取环境变量,我认为这是一件好事。
您可以尝试将环境变量放在主机上的文件中,然后将其挂载到您的停靠容器中的文件中(例如,在/etc/init.d/下)。并在运行脚本之前,将docker映像的init脚本更改为源已装入的文件。
https://stackoverflow.com/questions/42817793
复制相似问题