In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to build a SSH image in Docker". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to build a SSH image in Docker.
Create docker installation based on commit command [root@test01 ~] # yum install docker [root@test01 ~] # systemctl enable docker [root@test01 ~] # systemctl start docker download 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 imagesREPOSITORY 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 running 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.shroud run.sh run.sh submit the resulting 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 imagesREPOSITORY 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 boot image [root@test01 ~] # Docker run-d-p 2022 root@test01 22 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 root/run.sh6d5628a2a336bc302fa45baf6e6a1d5ade2f6dd42a4697553c6e3dda1a0a3226 2022-> 22/tcp prickly_bell leak repair
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 starts the image that needs to be changed again [root@test01 ~] # docker run-d-p 2022 minutes ago ssh_ 22 ssh02:commit / root/run.sh357ed4074c5d7f1ec1fe0df6af9c9a3162c70fa5624f7742bf59f309d9842247 verify whether [root ~] # ssh root@ is successful 192.168.1.60-p2022root@192.168.1.60's password: [root@357ed4074c5d ~] # exit [root@test01 ~] # docker stop 357ed4074c5d creates a preparation file based on Dockerfile
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 to write Dockerfile, Basic image FROM centos:7.4.1708# used by run.sh [root@test01 ssh_dockerfile] # cat Dockerfile# 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 2commands set the default startup command CMD ["/ root/run.sh"] [root@test01 ssh_dockerfile] # cat run.shrun.Bin.bash.usr.sbin.sshd-D create an image [root@test01 ssh_dockerfile] # docker build. / Sending build context to Docker daemon 3.072 kBStep 1: FROM centos:7. 4.1708Murray-> 3afd47092a0eStep 2: MAINTAINER liuxin 842887233MQ.comMurray-> 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 1d5a9524da86Kuhr-> 324868eb5780Removing intermediate container 1d5a9524da86Step 9: EXPOSE 22Maur-> Running in ada62bb87978--- > a0b3df156e21Removing intermediate container ada62bb87978Step 10: CMD / root/run.sh--- > Running in 4f5031577ff4Muhran-> 8679c00088efRemoving intermediate container 4f5031577ff4Successfully Built 8679c00088ef [root@test01 ssh_dockerfile] # docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE 8679c00088ef 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 running Image [root@test01 ssh_dockerfile] # docker run-d-p 2022 months ago 22 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 seconds 2022-> 22/tcp fervent_yonath so far I believe you have a deeper understanding of "how to build a SSH image in Docker", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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: 0
*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.