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

Background

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:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

The desired result is:

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/chromedriver/linux-119.0.6045.105/chromedriver-linux64

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
PATH=${PATH}:/chromedriver/linux-119.0.6045.105/chromedriver-linux64
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
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/chromedriver/linux-119.0.6045.105/chromedriver-linux64

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
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/chromedriver/linux-119.0.6045.105/chromedriver-linux64    

Dockerfile

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 https://github.com/nodesource/distributions#deb
RUN curl -SLO https://deb.nodesource.com/nsolid_setup_deb.sh 
    && chmod 500 nsolid_setup_deb.sh 
    && ./nsolid_setup_deb.sh 21 
    && apt-get -y install nodejs

# from https://developer.chrome.com/blog/chrome-for-testing/#how-can-i-get-chrome-for-testing-binaries
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) 
    && export APPEND_CHROME_DRIVER_PATH="PATH="'${PATH}'":${CHROME_DRIVER_PATH}" 
    # 
    && 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