Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating PATH environment variable permanently in Docker container

I tried adding to the PATH in the files ~/.profile and /etc/profile as follow.

PATH = $PATH:/required/path

However, it does not work. Then I tried with adding the line show, which did not work either.

export PATH

It did not work even after restarting the container and the host both.

like image 372
user859375 Avatar asked Feb 25 '15 15:02

user859375


People also ask

How do I change docker environment variables?

To set an environment variable you should use flag -e while using docker run or docker-compose command. Environment file - If the environment variable is not overridden by docker-compose. yml, shell environment the variable then the environment file will get the precedence.

How do I pass environment variables to docker containers?

With a Command Line Argument The command used to launch Docker containers, docker run , accepts ENV variables as arguments. Simply run it with the -e flag, shorthand for --env , and pass in the key=value pair: sudo docker run -e POSTGRES_USER='postgres' -e POSTGRES_PASSWORD='password' ...

Are changes in docker container persistent?

If you want to make changes inside the container and want those changes to persist, you can use the Docker commit command. This will create a new image with all the changes made to the previous container committed to it.


3 Answers

If you want to include a /new/path in the Dockerfile, adding the line:

ENV PATH "$PATH:/new/path"

in Dockerfile should work.

like image 80
Jianxin Gao Avatar answered Sep 28 '22 05:09

Jianxin Gao


1. The correct answer

The best voted answer suggests to add ENV PATH "$PATH:/new/path" to the Dockerfile, and this should indeed work.

2. So why doesn't it work for me?

As noted in some comments/answers, the solution 1. does not work for some people.

The reason is that the PATH can be overwritten by some script like .bashrc when running the docker container, giving thus the impression that the ENV PATH... did not work, but it theoretically did.

To solve the issue, you need to append to the .bashrc the correct PATH by adding the below command to your Dockerfile:

RUN echo "export PATH=/new/path:${PATH}" >> /root/.bashrc

like image 39
BiBi Avatar answered Sep 28 '22 04:09

BiBi


Put in your Dockerfile a line ENV PATH xxx see an example in this Dockerfile https://gist.github.com/deepak/5933685

like image 40
user2915097 Avatar answered Sep 28 '22 04:09

user2915097