背景
我正在构建一个基于 Debian:11 的镜像。我正在尝试设置PATH
为/bin/sh
并确定动态值在图像构建时。 (这 ”在图像构建时“很重要。我不能简单地ENV PATH
在我的 Dockerfile 中包含一个静态。)
问题
当我跑步时:
docker build . --tag debian-with-chrome-driver
docker run -it --rm debian-with-chrome-driver
echo $PATH
实际结果是:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
期望的结果是:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/chromedriver/linux-119.0.6045.105/chromedriver-linux64
我尝试过的
根据各种 StackExchange 帖子,我尝试进行PATH
以下设置:
/etc/profile
/etc/login.defs
/etc/environment
上述文件的相关内容如下:
# 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
我已经成功设置了bash的路径(如下所示),但是我需要设置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
你可以自己尝试一下:
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 PATH\n" >> /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" ]
答案1
在/bin/sh
Debian 中 ( dash
)/etc/profile
等仅用于登录 shell。对于非登录 shell,您可以在环境变量中指定要使用的启动脚本ENV
。在你的末尾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" ]