bash 在 Docker 中设置环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46834331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set environment variables in Docker
提问by wandadars
I'm having trouble with Docker creating a container that does not have environment variables set that I know I set in the image definition.
我在 Docker 创建一个容器时遇到问题,该容器没有设置我知道我在图像定义中设置的环境变量。
I have created a Dockerfile that generates an image of OpenSuse 42.3. I need to have some environment variables set up in the image so that anyone that starts a container from the image can use a code that I've compiled and placed in the image.
我创建了一个生成 OpenSuse 42.3 映像的 Dockerfile。我需要在映像中设置一些环境变量,以便从映像启动容器的任何人都可以使用我编译并放置在映像中的代码。
I have created a shell file called "image_env_setup.sh" that contains the necessary environment variable definitions. I also manually added those environment variable definitions to the Dockerfile.
我创建了一个名为“image_env_setup.sh”的 shell 文件,其中包含必要的环境变量定义。我还手动将这些环境变量定义添加到 Dockerfile。
USER codeUser
COPY ./docker/image_env_setup.sh /opt/MyCode
ENV PATH="$PATH":"/opt/MyCode/bin:/usr/lib64/mpi/gcc/openmpi/bin"
ENV LD_LIBRARY_PATH="/usr/lib64:/opt/MyCode/lib:"
ENV PS1="[\u@docker: \w]$ "
ENV TERM="xterm-256color"
ENV GREP_OPTIONS="--color=auto"
ENV EDITOR=/usr/bin/vim
USER root
RUN chmod +x /opt/MyCode/image_env_setup.sh
USER codeUser
RUN /opt/MyCode/image_env_setup.sh
RUN /bin/bash -c "source /opt/MyCode/image_env_setup.sh"
The command that I use to create the container is:
我用来创建容器的命令是:
docker run -it -d --name ${containerName} -u $userID:$groupID \
-e USER=$USER --workdir="/home/codeUser" \
--volume="${home}:/home/codeUser" ${imageName} /bin/bash \
The only thing that works is to pass the shell file to be run again when the container starts up.
唯一有效的方法是传递要在容器启动时再次运行的 shell 文件。
docker start $MyImageTag
docker exec -it $MyImageTag /bin/bash --rcfile /opt/MyCode/image_env_setup.sh
I didn't think it would be that difficult to just have the shell variables setup within the container so that any entry into it would provide a user with them already defined.
我不认为在容器中设置 shell 变量会那么困难,这样任何进入它的条目都会为用户提供已经定义的变量。
回答by AnoE
RUN
entries cannot modify environment variables (I assume you want to set more variables in image_env_setup.sh
). Only ENV
entries in the Dockerfile (and docker options like --rcfile
can change the environment).
RUN
条目不能修改环境变量(我假设您想在 中设置更多变量image_env_setup.sh
)。只有ENV
Dockerfile 中的条目(和 docker 选项等--rcfile
可以改变环境)。
You can also decide to source image_env_setup.sh
from the .bashrc
, of course.
当然,您也可以决定image_env_setup.sh
从.bashrc
.
For example, you could either pre-fabricate a .bashrc
and pull it in with COPY
, or do
例如,您可以预制 a.bashrc
并使用 将其拉入COPY
,或者执行
RUN echo '. /opt/MyCode/image_env_setup.sh' >> ~/.bashrc
回答by Vignajeth
you can put /opt/MyCode/image_env_setup.sh
in ~/.bash_profile or ~/.bashrc of the container so that everytime you get into the container you have the env's set
您可以放入/opt/MyCode/image_env_setup.sh
容器的 ~/.bash_profile 或 ~/.bashrc 中,以便每次进入容器时都设置了环境