Debian:11 イメージの /bin/sh の PATH 設定

Debian:11 イメージの /bin/sh の PATH 設定

背景

私は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

私が試したこと

PATHさまざまなStackExchange投稿に基づいて、次の設定を試しました。

  • /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    

ドッカーファイル

自分で試してみてください。

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/shDebian では、 ( dash)/etc/profileなどはログインシェルにのみ使用されます。非ログインシェルの場合は、環境変数で使用する起動スクリプトを指定できます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" ]

おすすめ記事