In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Many novices are not very clear about how to create a SSH service image in the docker container. In order to help you solve this problem, the following editor will explain it in detail. People with this need can come and learn. I hope you can learn something.
Create based on commit command mode
Installation of docker
[root@test01 ~] # yum install docker [root@test01 ~] # systemctl enable docker [root@test01 ~] # systemctl start docker
Download the local image
When using the docker run command, Docker will automatically look for the local image first, and if it does not find it, it will continue to find and download it on docker hub. I am used to downloading the latest version of the default system version of docker pull centos first. If you specify a version, add a colon and a version number.
[root@test01 ~] # docker pull centos:7.4.1708 [root@test01 ~] # docker images REPOSITORY TAG IMAGE ID CREATED SIZEdocker.io/centos 7.4.1708 3afd47092a0e 3 months ago 196.6 MB
Create an interactive container
[root@test01 ~] # docker run-it-- name= "ssh_server" centos:7.4.1708 / bin/bash [root@ffe61e183a6c /] #
Install the necessary services
Through yum installation, check whether the yum source configuration is correct. The default of centos7 is the same as that of the host.
Yum install openssh-server installs ssh service program yum install net-tools installs network tools to view ports, but does not install [root@ffe61e183a6c /] # yum install openssh-server net-tools
Configure the sshserver service
Use ssh-keygen to generate the necessary keys
[root@ffe61e183a6c /] # ssh-keygen-t rsa-f / etc/ssh/ssh_host_rsa_ key [root @ ffe61e183a6c /] # ssh-keygen-t ecdsa-f / etc/ssh/ssh_host_ecdsa_ key [root @ ffe61e183a6c /] # ssh-keygen-t ed25519-f / etc/ssh/ssh_host_ed25519_key
Start the ssh server and check to see if it started successfully
/ usr/sbin/sshd-D &
The-D parameter here is used to tell the SSH service that it does not run as a daemon, but is associated with the running terminal. With the running terminal, the container will not exit.
[root@ffe61e183a6c /] # / usr/sbin/sshd-D & [1] 82 [root@ffe61e183a6c /] # netstat-tunplaActive Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0. 0. 0. 0. 0. 0. 0. 0. 0. 0: 22: * LISTEN 82/sshd [root@ffe61e183a6c /] # pkill sshd
Write SSH run script
At this point, we can confirm that there is no problem with starting the ssh service, and then we write a startup script to run when the container is started, because the container can only run one command when it starts, which is usually used to start the script.
[root@ffe61e183a6c ~] # cat run.sh #! / bin/bash/usr/sbin/sshd-D [root@ffe61e183a6c ~] # chmod 775 run.sh
Submit the generated image
Use docker commit to submit the container as a new image
[root@ffe61e183a6c ~] # exitexit [root@test01 ~] # [root@test01 ~] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESffe61e183a6c centos:7.4.1708 "/ bin/bash" 19 minutes ago Exited (0) 8 seconds ago ssh_ Server [root@test01 ~] # docker commit ffe61e183a6c ssh:commitsha256:be55c135e6141481aff3218b7a269b27d8f0faa295ed244849bf8ccf7ad1c7b1 [root@test01 ~] # docker images REPOSITORY TAG IMAGE ID CREATED SIZEssh commit be55c135e614 11 seconds ago 296.5 MBdocker.io/centos 7.4.1708 3afd47092a0e 3 months ago 196.6 MB
Start mirroring
[root@test01 ~] # docker run-d-p 2022 ssh:commit / root/run.sh6d5628a2a336bc302fa45baf6e6a1d5ade2f6dd42a4697553c6e3dda1a0a3226 [root@test01 ~] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES6d5628a2a336 ssh:commit "/ root/run.sh" 8 seconds ago Up 6 seconds 0.0.0.0 ssh:commit 2022-> 22/tcp prickly_bell
Make up the leak
I forgot to set the password for the docker image just now. This time, I need to set the password.
[root@test01 ~] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES6d5628a2a336 ssh:commit "/ root/run.sh" 6 minutes ago Exited (137) 4 minutes ago prickly_bellffe61e183a6c centos:7.4.1708 " / bin/bash "29 minutes ago Exited (0) 9 minutes ago ssh_ server [root @ test01 ~] # docker run-it ssh:commit / bin/bash [root@0204e7257a24 /] # passwd rootChanging password for user root.New password: Retype new password: passwd: all authentication tokens updated successfully. [root@0204e7257a24 /] # exitexit [root@test01 ~] # docker ps-aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES0204e7257a24 ssh:commit "/ bin/bash" 29 seconds ago Exited (0) 4 seconds ago trusting_borg6d5628a2a336 ssh:commit "/ root/run.sh" 9 minutes ago Exited (137) 7 minutes ago Prickly_bellffe61e183a6c centos:7.4.1708 "/ bin/bash" 32 minutes ago Exited (0) 12 minutes ago ssh_ server [root @ test01 ~] # docker commit 0204e7257a24 ssh02:commitsha256:b92a3cde4c9162cf12ac9cf61a61ce0332d3755b7708e4037c4df09b4e794177
Start the image to be changed again
[root@test01] # docker run-d-p 2022 ssh02:commit / root/run.sh357ed4074c5d7f1ec1fe0df6af9c9a3162c70fa5624f7742bf59f309d9842247
Verify that it is successful
[root@test01 ~] # ssh root@192.168.1.60-p2022root@192.168.1.60's password: [root@357ed4074c5d ~] # exit [root@test01 ~] # docker stop 357ed4074c5d
Create based on Dockerfile mode
Prepare the document
Create a directory to store files related to the generated image
Two files need to be created in this directory: Dockerfile and run.sh. Dockerfile is used to build the image, and run.sh is the script that starts the SSH service
Mkdir ssh_dockerfile & & cd ssh_dockerfile
Write Dockerfile, run.sh
[root@test01 ssh_dockerfile] # cat Dockerfile # basic image FROM centos:7.4.1708# add author information MAINTAINER liuxin 842887233@qq.com# install SSH service RUN yum install-y openssh-server# add the necessary key RUN ssh-keygen-t rsa-f / etc/ssh/ssh_host_rsa_keyRUN ssh-keygen-t ecdsa-f / etc/ssh/ssh_host_ecdsa_keyRUN ssh-keygen-t ed25519-f / etc/ssh/ssh_host _ ed25519_key# add startup file ADD run.sh / root/run.shRUN chmod 775 / root/run.sh# export port EXPOSE 2boot setting default startup command CMD ["/ root/run.sh"] [root@test01 ssh_dockerfile] # cat run.sh#! / bin/bash/usr/sbin/sshd-D
Create a mirror
[root@test01 ssh_dockerfile] # docker build. / Sending build context to Docker daemon 3.072 kBStep 1: FROM centos:7.4.1708-> 3afd47092a0eStep 2: MAINTAINER liuxin 842887233@qq.com-> Using cache-> bd64810df0bcStep 3: RUN yum install-y openssh-server-- > Using cache-- > 5dc6301a0304Step 4: RUN ssh-keygen-t rsa-f / etc/ssh/ssh_host_rsa_key-- > Using cache-> 0ce92e5baa9fStep 5: RUN ssh-keygen- T ecdsa-f / etc/ssh/ssh_host_ecdsa_key-- > Using cache-- > fcb2bcf78ea0Step 6: RUN ssh-keygen-t ed25519-f / etc/ssh/ssh_host_ed25519_key-- > Using cache-- > 7eae01e47ee2Step 7: ADD run.sh / root/run.sh-- > 4d07a723ffcfRemoving intermediate container 0b137a9274beStep 8: RUN chmod 775 / root/run.sh-- > Running in 1d5a9524da86-> 324868eb5780Removing intermediate container 1d5a9524da86Step 9: EXPOSE 22-> Running in ada62bb87978-- -> a0b3df156e21Removing intermediate container ada62bb87978Step 10: CMD / root/run.sh-- > Running in 4f5031577ff4-> 8679c00088efRemoving intermediate container 4f5031577ff4Successfully built 8679c00088ef [root@test01 ssh_dockerfile] # docker images REPOSITORY TAG IMAGE ID CREATED SIZE8679c00088ef About a minute ago 295.9 MBssh02 commit b92a3cde4c91 55 minutes ago 296.5 MBssh Commit be55c135e614 About an hour ago 296.5 MBdocker.io/centos 7.4.1708 3afd47092a0e 3 months ago 196.6 MB
Run Mirror
[root@test01 ssh_dockerfile] # docker run-d-p 2022 8679c00088efe73a441afc8df35f42a30974c8697278fe6d35c1ac711d13ec817e74ffbf4008 [root@test01 ssh_dockerfile] # docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe73a441afc8d 8679c00088ef "/ root/run.sh" 14 seconds ago Up 12 seconds 0.0.0.0 8679c00088efe73a441afc8df35f42a30974c8697278fe6d35c1ac711d13ec817e74ffbf4008 2022-> 22/tcp fervent_yonath Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 212
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.