2017-03-15 44 views
0

我知道如何將環境變量傳遞給docker容器。像在Ubuntu的運行級腳本中採用傳遞給Docker容器的環境變量

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

我能夠成功地選擇這些變量,如果我從泊塢窗容器內shell中運行的腳本。但docker實例的runlevel腳本無法看到傳遞給docker容器的環境變量。最終所有的服務/守護進程都以默認配置開始,我不想要。

請爲它提出一些解決方案。

回答

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. 
0

在您的Dockerfile中,您可以選擇在運行時指定環境變量。

Dockerfile

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

at the docker environment replacement docs獲取更多信息。

0

運行級腳本將無法讀取環境變量,我認爲這是一件好事。

您可以嘗試將環境變量放在主機上的文件中,將其掛載到您的泊塢窗容器中的文件中(例如在/etc/init.d/之下)。在運行腳本之前,更改docker鏡像的init腳本以獲取已安裝的文件。

相關問題