Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose adding to PATH

I need to add some paths to my PATH in docker-compose.yml

in docker-compose.yml I have tried

app:     ...     environment:         - PATH /code/project 

however that just overwrites the existing PATH - whereas I want to add to the existing PATH

like image 699
lukeaus Avatar asked Jan 08 '16 09:01

lukeaus


People also ask

How do I specify a file in Docker compose?

Specifying a path to a single Compose file You can use the -f flag to specify a path to a Compose file that is not located in the current directory, either from the command line or by setting up a COMPOSE_FILE environment variable in your shell or in an environment file.

Where do Docker compose files go?

The default path for a Compose file is ./docker-compose.yml .

How do I set an environment variable in docker?

Use -e or --env value to set environment variables (default []). If you want to use multiple environments from the command line then before every environment variable use the -e flag. Note: Make sure put the container name after the environment variable, not before that.

What is Depends_on in Docker compose?

depends_on is a Docker Compose keyword to set the order in which services must start and stop. For example, suppose we want our web application, which we'll build as a web-app image, to start after our Postgres container.


2 Answers

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 
like image 147
Thomasleveil Avatar answered Oct 01 '22 14:10

Thomasleveil


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.

like image 22
Rocksn17 Avatar answered Oct 01 '22 16:10

Rocksn17