Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Dockerfile to create a container image that supports ssh service self-startup

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to use Dockerfile to create a container image that supports ssh service self-startup". The content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use Dockerfile to create a container image that supports ssh service self-startup" can help you solve your doubts.

1. First create a dockerfile file with the following contents

# Select an existing os image as the author of the basic from centos:centos6# image maintainer fanbin kong "kongxx@hotmail.com" # install the openssh-server and sudo software packages, and set the usepam parameter of sshd to norun yum install-y openssh-server sudorun sed-I 's/usepam yes/usepam no/g' / etc/ssh/sshd_config # add test user admin, password admin And add this user to sudoers run useradd adminrun echo "admin:admin" | chpasswdrun echo "admin all= (all) all" > > / etc/sudoers# these two sentences are special and must be on centos6. Otherwise, the created container sshd cannot log in to run ssh-keygen-t dsa-f / etc/ssh/ssh_host_dsa_keyrun ssh-keygen-t rsa-f / etc/ssh/ssh_host_rsa_key# to start the sshd service and expose port 22 run mkdir / var/run/sshdexpose 22cmd ["/ usr/sbin/sshd", "- d"]

Dockerfile files have their own syntax and commands, which can be found in the official documentation of docker.

two。 Once you have the dockerfile file, you can create the image file according to dockerfile. In the directory where dockerfile is located, run the following command

Sudo docker build-t centos6-ssh.

When the command is successful, an image named centos6-ssh is created, which can be viewed using "sudo docker images".

3. At this point, you can create your own container based on the image file created above. The following command creates a container named "mytest".

Sudo docker run-d-p-- name=mytest centos6-ssh

4. With the container, we can test our ssh service.

4.1 run "sudo docker inspect mytest" to see the ip address of the current startup container, and then run the following command to test

Ssh admin@

4.2 alternatively, it can be accessed through the port mapping of docker. Use "sudo docker port mytest 22" to view the port of the host machine corresponding to port 22 of the current container, and then access it with the following command

Ssh admin@-p

After reading this, the article "how to use Dockerfile to create a container image that supports ssh service self-startup" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report