John Davidson

Entering docker container with exec losing PATH environment variable

0 comments
Message:


Here is my Dockerfile:



FROM ros:kinetic-ros-core-xenial
CMD ["bash"]


If I run docker build -t ros . && docker run -it ros, and then from within the container echo $PATH, I'll get:



/opt/ros/kinetic/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin


If I exec into the container (docker exec -it festive_austin bash) and run echo $PATH, I'll get:



/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin


Why are the environment variables different? How can I get a new bash process on the container with the same initial environment?



My Answer:
When you use `docker exec` to enter a running container, it does not run the CMD specified in the Dockerfile, which is why the PATH variable is different.

To get a new bash process on the container with the same initial environment as when the container was started, you can use the `docker run` command with the `--entrypoint` option to specify the command to run when starting the container. For example:


docker run -it --entrypoint bash ros


This will start a new bash process on the container with the same initial environment as when the container was started.

Rate this post

4 of 5 based on 4795 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us