Set PATH for /bin/sh in Debian:11 image


I am building an image based on Debian:11. I am trying to set the PATH for the /bin/sh with a dynamic value determined at image build-time. (The "at image build time" is important. I cannot simply include a static ENV PATH in my Dockerfile.)

The Problem

When I run:

docker build . --tag debian-with-chrome-driver
docker run -it --rm debian-with-chrome-driver
echo $PATH

The actual result is:


The desired result is:


What I have tried

Based on a variety of StackExchange posts, I have tried setting the PATH in the following:

  • /etc/profile
  • /etc/login.defs
  • /etc/environment

The following shows the relevant contents of the above files:

# tail -n 3 /etc/profile
export PATH

# cat /etc/login.defs | grep chromedriver
ENV_SUPATH      PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/chromedriver/linux-119.0.6045.105/chromedriver-linux64
ENV_PATH        PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/chromedriver/linux-119.0.6045.105/chromedriver-linux64

# cat /etc/environment

I have successfully set the path for bash (as shown in the following.) But I need to set the path for the sh.

# bash  
root@4e159cfc9fe5:/# echo $PATH


You can try it yourself:

docker build . --tag debian-with-chrome-driver
FROM debian:11

RUN apt-get update && apt-get install -y curl

# from
RUN curl -SLO 
    && chmod 500 
    && ./ 21 
    && apt-get -y install nodejs

# from
RUN npx -y @puppeteer/browsers install chrome@stable 
    && npx -y @puppeteer/browsers install chromedriver@stable

# add the chromedriver to the PATH
RUN export CHROME_DRIVER_PATH=$(find /chromedriver -name "chromedriver" -type f -executable -printf "%h" -quit) 
    && echo "export ${APPEND_CHROME_DRIVER_PATH}n" >> /etc/bash.bashrc 
    && echo "${APPEND_CHROME_DRIVER_PATH}nexport PATHn" >> /etc/profile 
    && export CHROME_DRIVER_PATH_ESCAPED=$(echo ${CHROME_DRIVER_PATH} | sed 's///\//g') 
    && sed -i '/^ENV_SUPATH.*/ s/$/:'"${CHROME_DRIVER_PATH_ESCAPED}"'/' /etc/login.defs 
    && sed -i '/^ENV_PATH.*/ s/$/:'"${CHROME_DRIVER_PATH_ESCAPED}"'/' /etc/login.defs 
    && echo "PATH=${PATH}:${CHROME_DRIVER_PATH}n" >> /etc/environment 
    && echo ${PATH}

ENTRYPOINT [ "bin/sh" ]
Asked By: Wallace Kelly


For /bin/sh in Debian (dash), /etc/profile and so on are only used for login shells. For non-login shells, you can specify a startup script to use in the ENV environment variable. At the end of your Dockerfile:

    && echo "PATH=${PATH}:${CHROME_DRIVER_PATH}n" >> /etc/environment 
    && echo "PATH=${PATH}:${CHROME_DRIVER_PATH}n" >> /etc/shinit 
    && chmod 755 /etc/shinit 
    && echo ${PATH}

ENV ENV=/etc/shinit

ENTRYPOINT [ "bin/sh" ]
Answered By: Stephen Kitt