我知道如何将环境变量传递给docker容器。像
sudo docker run -e ENV1='ENV1_VALUE' -e ENV2='ENV2_VALUE1' ....
如果我从docker容器中的shell运行脚本,我能够成功选择这些变量。但是docker实例的运行级脚本无法看到传递给docker容器的环境变量。最终所有服务/守护进程都以我不想要的默认配置开始。
请为此提出一些解决方案。
答案 0 :(得分:0)
在Dockerfile中,您可以选择在运行时之前指定环境变量。
<强> Dockerfile 强>
FROM ubuntu:16.04
ENV foo=bar
ENV eggs=spam
RUN <some runtime command>
CMD <entrypoint>
答案 1 :(得分:0)
运行级别脚本将无法读取环境变量,我认为这是一件好事。
您可以尝试将环境变量放在主机上的文件中,将其安装到docker容器中的文件中(例如,在/etc/init.d/
下)。并在运行脚本之前更改docker镜像的init脚本以获取已安装的文件。
答案 2 :(得分:0)
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.