Docker USES Dockerfile to create container images that enable ssh services to self start
- 2020-05-17 07:07:07
- OfStack
For your reference, this article Shared the example of Dockerfile creating a container image that supports ssh service startup. The details are as follows
1. First, create an Dockerfile file. The content of the file is as follows
# choose 1 An existing os Mirror image as the basis
FROM centos:centos6
# Author of mirror image
MAINTAINER Fanbin Kong "kongxx@hotmail.com"
# The installation openssh-server and sudo Package, and will sshd the UsePAM Set the parameter to no
RUN yum install -y openssh-server sudo
RUN sed -i 's/UsePAM yes/UsePAM no/g' /etc/ssh/sshd_config
# Add test users admin The password admin , and add this user to sudoers In the
RUN useradd admin
RUN echo "admin:admin" | chpasswd
RUN echo "admin ALL=(ALL) ALL" >> /etc/sudoers
# The following two sentences are special centos6 Must have on, otherwise create out of the container sshd Can not login
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
# Start the sshd Service and expose 22 port
RUN mkdir /var/run/sshd
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
The Dockerfile file has its own syntax and commands, and you can refer to the official Docker documentation for details.
2. Once you have the Dockerfile file, you can create the image file based on Dockerfile. From the directory where Dockerfile lives, run the following command
sudo docker build -t centos6-ssh .
When the command succeeds, an image named centos6-ssh is created, which can be viewed using "sudo docker images".
3. Now you can create your own container based on the image file created above. The following command will create 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 current launch container IP address, and then run the following command to test
ssh admin@ < Container IP >
4.2 alternatively, it can be accessed through the port mapping of docker, using "sudo docker port mytest 22" to view the port of the host machine corresponding to port 22 of the current container, and then through the following command
ssh admin@
<
Host machine IP
>
-p
<
Host machine port
>