Add ssh service to docker container

This article mainly introduces how to add ssh service to a container, so that you can directly ssh into a container from another server to achieve the purpose of directly accessing the internal environment of the container, avoiding the need to enter the container from the host docker exec or attach

Experimental environment: two server addresses 192.168.91.131 and 192.168.91.133 (the host where the container is located)

Experimental results: From 192.168.91.131, you can directly access the inside of the container through ssh 192.168.91.133 -p xxxx

experiment procedure:

1. Obtain the ubuntu system image and download it from the local image library. If not, you can download it from Docker Hub

docker images Check the existence of images in the server, if not, you need to download them from the docker pull in the remote warehouse

2. Create a container based on the image

docker run -it -d usshdtest:sshdtest //未指定容器名称随机生成

docker ps to see that the container is running

3. Enter the container to install the sshd service

         3.1 Access to the container

docker exec -it a8c65e013d27 bash

         3.2 install ssh service

apt-get install openssh-server

         3.3 After the ssh service is installed without any problems, edit the /etc/ssh/sshd_config configuration file, add the root user to allow access, and add the configuration as follows

       3.4 Start the sshd service after the configuration is added

/usr/sbin/sshd       //默认安装路径,启动sshd服务

Check that the sshd service is started 

        3.5 In the internal environment of the container, add a password for the root user. After the addition is complete, press Ctrl p+q to exit the container

passwd root

4. Recreate the image based on the running container

docker commit a8c65e013d27 ubuntu:sshdtest

After the creation is complete, you can check the docker images to see if the image is created successfully

5. Run the container according to the newly created image

docker run -it -p 11002:22 -d ubuntu:sshdtest    //-p参数配置端口映射,将容器内22端口(sshd服务占用端口,可在容器内部netstat命令查看)映射到宿主机11002端口

Check that the new container is running, and port 11002 and port 22 are mapped

 6. docker exec enters the container to run the sshd service, here can also be made into a script to execute the script to start the sshd service when the container is running, to ensure that the sshd service is running

 7. Access the container directly from another server 192.168.91.131 through the mapped 11002 port ssh 192.168.91.133 -p 11002, enter the root username and password in step 3.5, and the access is successful

Guess you like

Origin blog.csdn.net/m0_64496909/article/details/129896779