我有一個 Dockerfile,它的基礎層包括 git,配置 git 的全域user.name并且user.email啟動openssh-server.
Dockerfile 是這樣的(簡化以消除感知的不相關性):
FROM debian as base
RUN apt-get update && \
apt-get -qy full-upgrade && \
apt-get install -qy git && \
apt-get install -qy openssh-server && \
sed -i 's|session required pam_loginuid.so|session optional pam_loginuid.so|g' /etc/pam.d/sshd && \
mkdir -p /var/run/sshd && \
groupadd builders -g 1111111112 && \
useradd -l -u 1111111111 -g 1111111112 -m -s /bin/bash bob && \
echo "bob ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers && \
echo "bob:youruncle" | chpasswd && \
git config --global user.name "bob" && \
git config --global user.email "[email protected]"
EXPOSE 22
CMD /usr/sbin/sshd -D
當我構建并運行這個容器時:
$ docker build -t tmp:tmp .
[ ] Building 59.2s (6/6) FINISHED
=> [internal] load .dockerignore 0.1s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 692B 0.0s
=> [internal] load metadata for docker.io/library/debian:latest 0.0s
=> CACHED [1/2] FROM docker.io/library/debian 0.0s
=> [2/2] RUN apt-get update && apt-get -qy full-upgrade && 55.4s
=> exporting to image 3.6s
=> => exporting layers 3.5s
=> => writing image sha256:cceaae2883b393ccb7dc0d977d846e5df1... 0.0s
=> => naming to docker.io/library/tmp:tmp 0.0s
$ docker run tmp:tmp
...并附上它,我看到了 bob 的預期 git 配置:
$ docker exec -it peaceful_einstein bash
root@3ca48a22fe98:/# git config --list
user.name=bob
[email protected]
root@3ca48a22fe98:/#
...但是當我以 bob 身份 ssh 到容器時,我看不到預期的 git 配置:
$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' peaceful_einstein
172.17.0.223
$ ssh [email protected]
The authenticity of host '172.17.0.223 (172.17.0.223)' can't be established.
ECDSA key fingerprint is SHA256:mIyf7TvG0nDSo3fWDipWGGPxFipb6THmoYt7dwtR77w.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.0.223' (ECDSA) to the list of known hosts.
[email protected]'s password:
Linux 3ca48a22fe98 4.9.0-8-amd64 #1 SMP Debian 4.9.144-3.1 (2019-02-19) x86_64
The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.
Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
bob@3ca48a22fe98:~$ git config --list
bob@3ca48a22fe98:~$
為什么當 ssh'ing 到容器時 bob 的 git config 資訊不可用?
有沒有辦法讓git configDockerfile 中的陳述句“應用”到 bob 的 ssh 會話?
uj5u.com熱心網友回復:
Runningdocker exec使用 Dockerfile 中的目錄 (the WORKDIR),除非您覆寫它,并且在這種情況下更重要的是 --u選項中的用戶或 Dockerfile 中的用戶。(另請參閱docker exec 的默認用戶是什么?)在您的情況下,它們是/和root(uid 0)。
在容器中運行 ssh 會啟動一個登錄 shell,它使用用戶的主目錄,從登錄中獲取用戶。在這種情況下bob,可能是這樣/home/bob。
Git 使用或設定當前用戶的--global配置,因此您可以使用 ssh 獲取 root 的配置,docker exec并使用 ssh 獲取 bob 的配置。
uj5u.com熱心網友回復:
我能夠使用@torek 的解釋來拼湊這個解決方案,該解決方案在 Dockerfilegit config的USER bob“部分”中完成作業:
FROM debian as base
RUN apt-get update && \
apt-get -qy full-upgrade && \
apt-get install -qy git && \
apt-get install -qy openssh-server && \
sed -i 's|session required pam_loginuid.so|session optional pam_loginuid.so|g' /etc/pam.d/sshd && \
mkdir -p /var/run/sshd && \
groupadd builders -g 1111111112 && \
useradd -l -u 1111111111 -g 1111111112 -m -s /bin/bash bob && \
echo "bob ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers && \
echo "bob:youruncle" | chpasswd
EXPOSE 22
USER bob
RUN git config --global user.name "bob" && \
git config --global user.email "[email protected]"
USER root
CMD /usr/sbin/sshd -D
我不知道該解決方案與流行的做法有多干凈/適當/符合,但它確實滿足了原始帖子的需要。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/473640.html
標籤:混帐 码头工人 dockerfile 打开sh 混帐配置
