docker-compose adding to PATH
I know this is an old thread, but I think there are a couple of things that can be clarified.
Through docker-compose
file one can only address variables from the host machine, therefore it is NOT possible to extend image's PATH from docker-compose.yml:
app:
...
environment:
- PATH=/code/project:$PATH
On the other hand, using RUN or CMD EXPORT directive will not suffice due to EXPORTED variables not persisting through images. Since every Dockerfile directive generates an intermediate image, these values will be reflected in them and not in the main image where you actually need them.
The best option would be to use build option in docker-compose.yml
:
app:
build: .
and adding ENV option to a Dockerfile:
ENV PATH /path/to/bin/folder:$PATH
This is suggested in issue #684 and I would also suggest looking at an answer: docker ENV vs RUN export.
A docker-compose.yml
does not offer you any mean to extend an environment variable which would already be set in a Docker image.
The only way I see to do such things is to have a Docker image which expects some environment variable (let's say ADDITONAL_PATH
) and extends at run time its own PATH
environment variable with it.
Let's take the following Dockerfile:
FROM busybox
ENV PATH /foo:/bar
CMD export PATH=$PATH:$ADDITIONAL_PATH; /bin/echo -e "ADDITIONAL_PATH is $ADDITIONAL_PATH\nPATH is $PATH"
and the following docker-compose.yml file (in the same directory as the Dockerfile):
app:
build: .
Build the image: docker-compose build
And start a container: docker-compose up
, you will get the following output:
app_1 | ADDITIONAL_PATH is
app_1 | PATH is /foo:/bar:
Now change the docker-compose.yml file to:
app:
build: .
environment:
- ADDITIONAL_PATH=/code/project
And start a container: docker-compose up
, you will now get the following output:
app_1 | ADDITIONAL_PATH is /code/project
app_1 | PATH is /foo:/bar:/code/project
Also note a syntax error in your docker-compose.yml file: there must be an equal sign (=
) character between the name of the environment variable and its value.
environment:
- PATH=/code/project
instead of
environment:
- PATH /code/project
You can add your value. To do so you need to know name or ID of the container, run it to know:
docker ps
This will print details of all running containers. Look for your container and copy its ID or name. Then run this:
docker inspect <container ID>
It will print all values of specified container. Look for ENV section and find PATH environment variable. Then copy its value, add your changes and extend it with your new values then set it again in your docker-compose.yml "environment" section.
app
environment:
- PATH=value-you-copied:new-value:new-value:etc
Note that you shouldn't remove anything from initial value of PATH, just extend it and add new value.