Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to create a Docker image that supports SSH services

Shulou Source: shulou.com Published: 2022-06-01 13:32:15 10月04日 Update

This article mainly shows you "how to create a Docker image that supports SSH services". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how to create a Docker image that supports SSH services".

(1) first run our local ubuntu container interactively

Docker run-it ubuntu:14.04 / bin/bash

(2) if you try to use the sshd command, you will find that the service is not installed in the container.

Sshdbash: sshd: command not found

(3) use apt package Manager to install openssh-server

Apt-get updateapt-get install openssh-server

(4) install and configure SSH service

To start the SSH service normally, you need the directory / var/run/sshd to exist, create it manually, and start the service:

Mkdir-p / var/run/sshd/usr/sbin/sshd-D &

At this point, check port 22 of the container (the default listening port of the SSH server), and it is already listening:

(5) modify the secure login configuration of SSH service

Execute the command ssh-keygen-t rsa on the remote client machine (that is, the non-container machine) to generate a public-private key pair for rsa, and then copy the public key information into the authorization file of the ubuntu container machine:

Mkdir / root/.sshvi / root/.ssh/authorized_keys # paste the client's public key information

(6) create an executable file run.sh that starts the SSH service

Vi / run.shchmod + x / run.sh

The script reads as follows:

#! / bin/bash/usr/sbin/sshd-D

(7) finally exit the container and save the current modified container as a new image

Execute the command docker ps-an on the host to check the running status of the container, and you can get the container id that just exited the container. Remember the first three bits of id.

Then execute the submit command to save the new image:

Docker commit $(top three of container id) ubuntu_sshd_gerry:14.04

After this command is submitted, a new image id* (image id) and a new image ubuntu_sshd_gerry:14.04 (ubuntu image that supports ssh service) will be generated.

Finally, you can use docker images to see if the local image already contains our newly created mirror ubuntu_sshd_gerry:14.04.

(8) use new mirroring and configure port mapping

Start the container and add the port map 10022 (host host port)-- > 22 (SSH service listening port inside the container):

Docker run-p 10022 virtual 22-d ubuntu_sshd_gerry:14.04 / run.sh

After startup, you can see the details of the container running on the host host:

(9) in the host host or other remote host, use SSH to access port 10022 to log in to the container and test the OK

Supplementary note:

The method of typing a new tag for an image (only the tag is different, it is actually the same image, because the image id is the same):

Docker tag $(image id) ubuntu_sshd_gerry:14.04

Then delete the old tag:

Docker rm $(container id) above is all the content of the article "how to create a Docker image that supports SSH services". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Container image service port host command support content host run information machine article monitor configuration public key client file status learning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Apple Shulou Tech Info NVidia Shulou Technology