Docker | Docker install and run and connect Centos7 container

One, install Docker

See article: https://blog.csdn.net/y1534414425/article/details/107872715

Second, pull the Centos7 image

docker pull centos:7

Three, run Centos7

docker run -itd --name myCentos -p 8081:22 -p 8082:80 centos:7 /bin/bash

Fourth, enter the container

docker attach [容器ID]

5. According to your own needs, first install some basic (container, the default is root user)

yum install -y net-tools

Six, then install openssl, openssh-server

yum install -y openssl openssh-server

Seven, then start ssh

/usr/sbin/sshd -D

An error will be reported here
Insert picture description here
, the following settings are required

[root@68e7598797d7 /]# ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''  
[root@68e7598797d7 /]# ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
[root@68e7598797d7 /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ''

Eight, then modify the sshd_config file configuration information

The configuration file path is /etc/ssh/sshd_config

1. Remove the comment in front of Port 22 (open port 22)
Insert picture description here

2. Change the value of PermitRootLogin to yes (here is to set whether to allow root user login, you can decide whether to enable it according to your needs)
Insert picture description here
3. Restart ssh

/usr/sbin/sshd -D &

Note that if the settings are all right, add an'&' at the end of the command, it will run in the background automatically, and the process number will be returned if it starts successfully.

Nine, add a password to root, you can skip this step if you have added it

[root@68e7598797d7 /]# yum install passwd
[root@68e7598797d7 /]# passwd

10. Exit and close the container just now, you can use the exit command, and then save the image

docker commit [容器ID] [镜像名称]

11. Restart a container based on the new image

docker run -itd --name newCentos -p 8081:22 -p 8082:80 [新镜像名称] /bin/bash

PS: Enter the container and restart ssh, it will not start automatically

/usr/sbin/sshd -D &

Then you can use xshell to connect remotely

Guess you like

Origin blog.csdn.net/y1534414425/article/details/108030323