?
This document uses PHP Chinese website manual Release
eg_sshd
圖像下面Dockerfile
在容器中設(shè)置一個SSHd服務(wù),您可以使用該服務(wù)來連接并檢查其他容器的卷,或者快速訪問測試容器。
FROM ubuntu:16.04RUN apt-get update && apt-get install -y openssh-server RUN mkdir /var/run/sshd RUN echo 'root:screencast' | chpasswd RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config # SSH login fix. Otherwise user is kicked off after login RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd ENV NOTVISIBLE "in users profile"RUN echo "export VISIBLE=now" >> /etc/profile EXPOSE 22CMD ["/usr/sbin/sshd", "-D"]
使用以下命令構(gòu)建圖像
$ docker build -t eg_sshd .
test_sshd
容器然后運行它。然后,您可以使用它docker port
來找出容器的端口22映射到的主機端口:
$ docker run -d -P --name test_sshd eg_sshd $ docker port test_sshd 220.0.0.0:49154
現(xiàn)在你可以root
在容器的IP地址(你可以找到它docker inspect
)或者49154
Docker守護進程的主機IP地址的端口上(ip address
或者ifconfig
可以告訴你)或者localhost
在Docker守護進程主機上ssh:
$ ssh root@192.168.1.2 -p 49154# The password is ``screencast``.root@f38c87f2a42d:/#
使用sshd
守護進程來生成shell會使得通過普通的Docker機制將環(huán)境變量傳遞到用戶的shell變得很復(fù)雜,因為sshd
它會在啟動shell之前清理環(huán)境。
如果你的在設(shè)定值Dockerfile
使用ENV
,你需要將它們推到像一個shell初始化文件/etc/profile
中的例子Dockerfile
以上。
如果您需要傳遞docker run -e ENV=value
值,則需要在開始之前編寫一個簡短的腳本來完成相同的操作sshd -D
,然后CMD
用該腳本替換。
最后,通過停止并取出容器,然后移除圖像,在測試后清理。
$ docker stop test_sshd $ docker rm test_sshd $ docker rmi eg_sshd