Docker containers ssh connection configuration

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/tladagio/article/details/91509807

Host: Centos 7

IP:192.168.14.57

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.6.1810 (Core) 

First, install docker

[root@localhost ~]# yum install -y docker 

Second, Mirror

1, the container acquires image centos

[root@localhost ~]# docker pull centos
Using default tag: latest
latest: Pulling from library/centos
8ba884070f61: Pull complete 
Digest: sha256:b5e66c4651870a1ad435cd75922fe2cb943c9e973a9673822d1414824a1d0475
Status: Downloaded newer image for centos:latest

2, which docker view mirror image

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              9f38484d220f        2 months ago        202MB

Third, container

1, start docker container

[root@localhost ~]# docker run -tdi --privileged centos init
38b8c5a053df39ef0a92c53cc3b038484c820c998b6e8b9a9ed88a7ddd3b370c
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
38b8c5a053df        centos              "init"              15 seconds ago      Up 11 seconds                           naughty_euclid
[root@localhost ~]# docker exec -it naughty_euclid /bin/bash

-i: keeping open the standard input, the default is false

-t: whether to allocate a dummy terminal, the default is false

Note: If you do not add --privileged, following sshd service can not start properly

[root@38b8c5a053df /]# systemctl start sshd
Failed to get D-Bus connection: Operation not permitted

2, change the root password

[root@38b8c5a053df /]# passwd root
Changing password for user root.
New password: 
BAD PASSWORD: The password is shorter than 8 characters
Retype new password: 
passwd: all authentication tokens updated successfully.

3, installation Openssh (ensure that the container can access the Internet)

[root@38b8c5a053df /]# yum install -y openssh-server openssh-clients

4, start the sshd service

[root@38b8c5a053df /]# systemctl start  sshd
[root@38b8c5a053df /]# systemctl status sshd
● sshd.service - OpenSSH server daemon
   Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2019-06-12 07:22:29 UTC; 1s ago
     Docs: man:sshd(8)
           man:sshd_config(5)
 Main PID: 2145 (sshd)
   CGroup: /docker/38b8c5a053df39ef0a92c53cc3b038484c820c998b6e8b9a9ed88a7ddd3b370c/system.slice/sshd.service
           └─2145 /usr/sbin/sshd -D
           ‣ 2145 /usr/sbin/sshd -D

Jun 12 07:22:29 38b8c5a053df systemd[1]: Starting OpenSSH server daemon...
Jun 12 07:22:29 38b8c5a053df sshd[2145]: Server listening on 0.0.0.0 port 22.
Jun 12 07:22:29 38b8c5a053df sshd[2145]: Server listening on :: port 22.
Jun 12 07:22:29 38b8c5a053df systemd[1]: Started OpenSSH server daemon.

5, install net-tools, use netstat -ntlp view the listening port

[root@38b8c5a053df /]# yum install -y net-tools

[root@38b8c5a053df /]# netstat -ntlp
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      2145/sshd           
tcp6       0      0 :::22                   :::*                    LISTEN      2145/sshd

6, exit container, remember container ID: 38b8c5a053df

[root@38b8c5a053df /]# exit

Fourth, save it as a new image

1, because the container has been changed, using the docker commit command to save a new sshd: centos mirror

[root@localhost ~]# docker commit -m 'install openss' -a 'Docker Newbee' 38b8c5a053df sshd:centos

2, to start a new container while adding the port mapping 10022-> 22. 10022 is the port where the host of 22 containers of ssh service listening port

[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
sshd                centos              3c6c67905ece        3 minutes ago       307MB
centos              latest              9f38484d220f        2 months ago        202MB
[root@localhost ~]# docker run -d -p 10022:22 sshd:centos /usr/sbin/sshd -D
48437032376b806b2df976ac6cd5b9457a6b7bd7d5d8013fbc24bb2a2a22c0ca
[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS              PORTS                   NAMES
48437032376b        sshd:centos         "/usr/sbin/sshd -D"   9 seconds ago       Up 5 seconds        0.0.0.0:10022->22/tcp   keen_mirzakhani
38b8c5a053df        centos              "init"                16 minutes ago      Up 16 minutes                               naughty_euclid

Fifth, remote login

1, ssh to log in to a remote host PC's host port 10022

[C:\Users]$ ssh [email protected] 10022


Connecting to 192.168.14.57:10022...
Connection established.
To escape to local shell, press Ctrl+Alt+].

WARNING! The remote SSH server rejected X11 forwarding request.
[root@48437032376b ~]# 

 

Guess you like

Origin blog.csdn.net/tladagio/article/details/91509807