Using the ENV instruction in my dockerfile doesn't seem to append the to the PATH. I've setup a minimal example below. Using Docker 26.0.0, build 2a3903e
I have the following dockerfile
FROM alpine
ENV PATH=/opt:$PATH
When I build the image:
docker build --no-cache -t alpine-path .
And then run:
docker run --rm -i -t alpine-path /bin/sh --login
Inside the shell:
# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
The /opt path hasn't been added to the PATH environment variable.
Clearly I'm doing something wrong somewhere, but I can't figure this out, even breaking it down to this very simple example.
From what you’ve shared, the issue is due to the way the login shell (/bin/sh --login
), which uses ash
in Alpine Linux, handles its environment settings.
The Alpine Linux shell (ash
) behaves differently depending on whether it is started as a login shell or a non-login shell.
If started as a login shell, it reads initialization scripts like /etc/profile
. However, it does not automatically import the ENV
settings from the Dockerfile unless explicitly stated in these scripts.
And if started as a non-login shell, it Inherits the environment directly from its parent process, in this case, Docker, which includes the ENV
settings from the Dockerfile.
To see the PATH
as you set it in the Dockerfile, you can run the container without the --login
option. This will start a non-login shell that directly inherits Docker’s ENV
settings:
docker run --rm -i -t alpine-path /bin/sh
Inside the shell, check the PATH
:
echo $PATH
This should reflect the PATH
setting from your Dockerfile.
If you need to use the shell as a login shell, you can update your Dockerfile to append the desired PATH
to /etc/profile
:
FROM alpine
ENV PATH="/opt:$PATH"
RUN echo "export PATH=$PATH" >> /etc/profile
which basically ensures that when the shell is launched as a login shell, it reads the updated PATH
from /etc/profile
.
After updating your Dockerfile, re-build the image and run the container.
Inside the container, check the PATH
again and it should now include /opt
in the PATH
variable.