为什么当我切换到其他用户环境变量时丢失? [英] Why when I switch to a different user environment variable is lost?
问题描述
第一个容器为用户docker设置PATH
1st container sets the PATH for the user docker
FROM ubuntu:15.10
USER root
RUN groupadd -r docker && useradd -r -g docker docker
USER docker
ENV PATH /hello-world:$PATH
第二个容器
FROM step_1
USER root
RUN echo $PATH
当我进入第二个容器并切换到用户docker PATH变量被重置。如果在第二个容器中,我没有切换到root用户,则变量保持保存状态。
为什么会这样?我如何为所有用户docker保存变量PATH?
When I go into the second container and switch to user docker PATH variable is reset. If in the second container, I do not switch to the root user, variable stay saved. Why is this happening? How do I for all users docker save variable PATH?
命令日志:
docker build -t step_1 step_1/
docker build -t step_2 step_2/
docker run -it step_2 bash
root@0784c73a84e2:/# echo $PATH
/hello-world:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
su docker
echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
推荐答案
FROM ubuntu:15.10
USER root
不做任何事情,您已经在容器中为根了。
Doesn't doesn't do anything, you're already root inside the container.
根据文档, ENV
设置的变量在图像之间确实存在:
Per the docs, ENV
variables set do persist between images:
使用ENV设置的环境变量将在以下情况下保持存在从结果图像运行容器。您可以使用docker inspect查看值。
The environment variables set using ENV will persist when a container is run from the resulting image. You can view the values using docker inspect...
忽略以上所有内容,我无法复制此问题,效果很好为了我。您可以粘贴完整的Dockerfile和正在运行的用于构建的命令等吗?
Ignoring all the above, I can't replicate this issue, it works fine for me. Can you paste your full Dockerfiles and the commands you're running to build etc...?
这篇关于为什么当我切换到其他用户环境变量时丢失?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!