简体   繁体   中英

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

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

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 .

@Thomasleveil's answer works only for containers built directly from the docker-compose file (via the build ). And you have no control over the command executed.

I needed this functionality for containers downloaded from (our) repository where this does not quite work.

I have found solution using the entrypoint and command .

Lets have some base container base and another one, java7 , that is based upon it. And finaly some docker-compose using the java7 container to run some stuff.

Probably the most important file here, entrypoint.sh

$ cat base/script/entrypoint.sh
#!/bin/bash

export PATH="$PATH_ADD:$PATH"
echo "Path modified to $PATH"
exec $@

Dockerfile for base container

$ cat base/Dockerfile
FROM xxx
# copy entrypoint script that extends current PATH variable by PATH_ADD
COPY script/entrypoint.sh /usr/sbin

ENTRYPOINT ["/usr/sbin/entrypoint.sh"]

Dockerfile for java7 container

$ cat java7/Dockerfile
FROM base
# download java7
curl ... /opt/java/jdk7
ENV JAVA_HOME /opt/java/jdk7

Commands run by docker-compose

$ cat sbin/run-app1.sh
exec $JAVA_HOME/bin/java -version

$ cat sbin/run-app2.sh
exec $JAVA_HOME/bin/java -version

Docker-compose using these:

$ cat docker-compose.yml
version: '3'
services:
  app1:
    image: java7
    command: run-app1.sh
    environment:
      PATH_ADD: /app/sbin
    volumes:
      - "./sbin:/app/sbin:cached"
  app2:
    image: java7
    command: run-app2.sh
    environment:
      PATH_ADD: /app/sbin
    volumes:
      - "./sbin:/app/sbin:cached"

File structure

$ tree
.
├── base
│   ├── script
│   │   └── entrypoint.sh
│   └── Dockerfile
├── java7
│   └── Dockerfile
├── sbin
│   ├── run-app1.sh
│   └── run-app2.sh
└── docker-compose.yml

to add a single location to PATH in your docker-compose.yml file:

app
    environment:
        - PATH=/code/project:$PATH

to add multiple locations to your PATH in your docker-compose.yml file

app
    environment:
        - PATH=/code/project:/code/lib:/foo/bar:$PATH

to add to your PYTHONPATH

app
    environment:
        - PYTHONPATH=/code/project:/code/lib:/foo/bar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM