Docker Community Forums

Share and learn in the Docker community.

Question about PATH


I’m trying to change PATH within my node and it works fine but only if I’m directly in the node, see examples:

$ docker run -it frontiersco/simpleweb echo $PATH
$ docker run -it frontiersco/simpleweb sh
/opt/node_app/app # echo $PATH
/opt/node_app/app #

Why the PATH is different…? Shouldn’t that be the same?

    FROM node:alpine

    WORKDIR /opt/node_app
    COPY package.json package-lock.json* ./
    RUN npm install --no-optional && npm cache clean --force
    ENV PATH /opt/node_app/node_modules/.bin:$PATH
    WORKDIR /opt/node_app/app
    COPY . .

Here is my version:
Docker version 19.03.13, build cd8016b6bc


You need to be careful with the run command when running on your local system:

docker run -it frontiersco/simpleweb echo $PATH

The $PATH hasn’t been escaped properly and as a result has been replaced by the PATH variable for your computer/laptop instead. You are actually telling docker to run:

docker run -it frontiersco/simpleweb echo "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"

When running:

$ docker run -it frontiersco/simpleweb sh
/opt/node_app/app # echo $PATH

The echo command is returning the actual PATH variable for your container as it doesn’t get replaced by the PATH variable for your local filesystem while in docker’s interactive shell.

1 Like