docker-compose adding to PATH

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)