docker的镜像添加ssh有两种方法:
- 通过现有容器安装ssh,然后通过commit生成新的镜像。
- 通过dockerfile直接建立新的ssh镜像
第一种的文章地址:《Docker 镜像添加SSH服务之commit(十八)》
我们现在开始使用dockerfile创建带有ssh的镜像。
1.使用dockerfile创建ssh镜像
1、下载centos镜像
docker pull centos
2、创建文件夹和dockerfile文件
mkdir -p /home/ssh
cd /home/ssh
touch Dockerfile
3、编辑dockerfile文件
#vim Dockerfile
#生成的新镜像以centos镜像为基础
FROM centos
#指定作者信息
MAINTAINER dongzao
#升级系统
RUN yum makecache && yum -y update glibc
RUN yum clean all
# 安装openssh-server
RUN yum -y install openssh-server
RUN mkdir /var/run/sshd
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_ecdsa_key
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_ed25519_key
#指定root密码
RUN /bin/echo 'root:123456'|chpasswd
#取消pam限制
RUN /bin/sed -i 's/.*session.*required.*pam_loginuid.so.*/session optional pam_loginuid.so/g' /etc/pam.d/sshd
RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" > /etc/default/local
#开放22端口
EXPOSE 22
#运行ssh
CMD /usr/sbin/sshd -D
4、创建镜像
docker build -t centos-ssh .
注意后面的小点
运行内容
[root@docker ssh]# docker build -t centos-ssh .
Sending build context to Docker daemon 2.56kB
Step 1/13 : FROM centos
---> 470671670cac
Step 2/13 : MAINTAINER dongzao
---> Using cache
---> b3edcb20370d
Step 3/13 : RUN yum makecache && yum -y update glibc
---> Running in aa081c57ed21
CentOS-8 - AppStream 933 kB/s | 6.5 MB 00:07
CentOS-8 - Base 2.7 MB/s | 5.0 MB 00:01
-----省略-----
Complete!
Removing intermediate container aa081c57ed21
---> 504f7fddd2ac
Step 4/13 : RUN yum -y install openssh-server
---> Running in 2c197a01aefb
Last metadata expiration check: 0:00:10 ago on Sat Mar 14 12:52:57 2020.
Dependencies resolved.
-----省略-----
Complete!
Removing intermediate container 2c197a01aefb
---> f7b9ee34f889
Step 5/13 : RUN mkdir /var/run/sshd
---> Running in 9a79474eee57
Removing intermediate container 9a79474eee57
---> c644fa611ea3
Step 6/13 : RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
---> Running in c52499d204f3
Enter passphrase (empty for no passphrase): Enter same passphrase again: Generating public/private rsa key
Your identification has been saved in /etc/ssh/ssh_host_rsa_key.
Your public key has been saved in /etc/ssh/ssh_host_rsa_key.pub.
The key fingerprint is:
SHA256:nY9DkV0qgcfNat7dPp38Vv/eQbWKTV0ZpFy6vssVoLI root@c52499d204f3
The key's randomart image is:
+---[RSA 3072]----+
| o.o o+ |
| . o++o+ o|
| .+.o= .o|
| .o+. + +|
| S++o + * |
| .+o* + +|
| Eo..+.+=|
| .. o+B|
| +.oO|
+----[SHA256]-----+
Removing intermediate container c52499d204f3
---> 2e159a3cbdf2
Step 7/13 : RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_ecdsa_key
---> Running in 954845f8084b
Enter passphrase (empty for no passphrase): Enter same passphrase again: Generating public/private rsa key
Your identification has been saved in /etc/ssh/ssh_host_ecdsa_key.
Your public key has been saved in /etc/ssh/ssh_host_ecdsa_key.pub.
The key fingerprint is:
SHA256:nzhVSC4jVijPqiaazHHDDmsc3elRwi0bp8fZXPsFaiQ root@954845f8084b
The key's randomart image is:
+---[RSA 3072]----+
| .. . |
| . .. o . |
| .++ o o . |
| *o= E + . |
| . ..@ S = o . |
| ....* + * = . |
|.o.=. o o + . . |
|++O .. . . |
|=* . |
+----[SHA256]-----+
Removing intermediate container 954845f8084b
---> 4d6859f4d77d
Step 8/13 : RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_ed25519_key
---> Running in 50d2727638b3
Enter passphrase (empty for no passphrase): Enter same passphrase again: Generating public/private rsa key
Your identification has been saved in /etc/ssh/ssh_host_ed25519_key.
Your public key has been saved in /etc/ssh/ssh_host_ed25519_key.pub.
The key fingerprint is:
SHA256:3XAAnZmcR7NjLw5GdMzBggyey1Uf9f4Gch/zcpzacsA root@50d2727638b3
The key's randomart image is:
+---[RSA 3072]----+
| .o.=+X*o. |
| . .ooO+=+ . |
| o . oo* .|
| . o o = o . |
| o S + = +o.|
| . o E +*|
| . o.B|
| .o= |
| .o. |
+----[SHA256]-----+
Removing intermediate container 50d2727638b3
---> 00890d064329
Step 9/13 : RUN /bin/echo 'root:123456'|chpasswd
---> Running in cb09a5ff39a8
Removing intermediate container cb09a5ff39a8
---> ba69809526ee
Step 10/13 : RUN /bin/sed -i 's/.*session.*required.*pam_loginuid.so.*/session optional pam_loginuid.so/g'
---> Running in 9adba06aa486
Removing intermediate container 9adba06aa486
---> b33b66b07987
Step 11/13 : RUN /bin/echo -e "LANG=\"en_US.UTF-8\"" > /etc/default/local
---> Running in f9b61cd5ac3a
Removing intermediate container f9b61cd5ac3a
---> ed406597c5d1
Step 12/13 : EXPOSE 22
---> Running in df54360984cc
Removing intermediate container df54360984cc
---> 8231ddd3de4c
Step 13/13 : CMD /usr/sbin/sshd -D
---> Running in e48d0eb4e9b9
Removing intermediate container e48d0eb4e9b9
---> ec61dea78f17
Successfully built ec61dea78f17
Successfully tagged centos-ssh:latest
5、创建容器
docker run -itd --name dongzao -p 8000:22 centos-ssh
6、测试登陆
登录成功
评论区