Because the official website images are generally installed with minimal installation and ssh is not installed, we will use a centos image on the official docker website as an example to install ssh today.
1. Check whether the image has centos. If so, perform subsequent operations. If not, use pull.
2. Create an ssh image.
Create a container and map host port 22000 to port 22 of the container:
Docker run-d-ti-name ssh-centos-p 22000:22 centos/bin/bash
View the container startup status:
[Root @ 21 yunwei ~] # Docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
A955391bd0b4 centos "/bin/bash" About a minute ago Up About a minute 0.0.0.0: 22000 & gt; 22/tcp ssh-centos
Enter the container:
Docker exec-ti ssh-centos/bin/bash
Install ssh when entering:
Yum install openssh *-y
After the installation, start the ssh service and check the port listening status:
[Root @ a955391bd0b4/] #/usr/sbin/sshd
[Root @ a955391bd0b4/] # netstat-tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
Tcp 0 0 0.0.0.0: 22 0.0.0.0: * LISTEN 27/sshd
Tcp6 0 0: 22: * LISTEN 27/sshd
All right, start successfully. Remote test:
1
All right, login successful:
[Root @ a955391bd0b4 ~] # Ifconfig
Eth0: flags = 4163 & lt; UP, BROADCAST, RUNNING, MULTICAST & gt; mtu 1500
Inet 172.17.0.6 netmask 255.255.0.0 broadcast 0.0.0.0
Inet6 fe80: 42: acff: fe11: 6 prefixlen 64 scopeid 0x20
Ether 02: 42: ac: 11: 00: 06 txqueuelen 0 (Ethernet)
RX packets 125 bytes 14200 (13.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 94 bytes 12693 (12.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
For ease of use, use the ssh-centos container to submit a new image, for example:
[Root @ 21 yunwei ~] # Docker commit ssh-centos centos7-ssh
[Root @ 21 yunwei ~] # Docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
Centos7-ssh latest 1aa38242b4ec 22 minutes ago 330.2 MB
After creating a centos7-ssh-based container with ssh, you can map the port to manage it.