1. First create a Dockerfile file, as follows
# Select an existing image as the basis os
FROM CentOS: centos6
# Mirrored author
MAINTAINER Fanbin Kong "firstname.lastname@example.org"
# Sudo and install openssh-server package, and the sshd of UsePAM parameter is set to no
RUN yum install -y openssh-server sudo
RUN sed -i 's / UsePAM yes / UsePAM no / g' / etc / ssh / sshd_config
# Add a test user admin, password admin, and add the user to sudoers Lane
RUN useradd admin
RUN echo "admin: admin" | chpasswd
RUN echo "admin ALL = (ALL) ALL" >> / etc / sudoers
# Below these two rather special, in centos6 must be, or else create a container sshd can not log out
RUN ssh-keygen -t dsa -f / etc / ssh / ssh_host_dsa_key
RUN ssh-keygen -t rsa -f / etc / ssh / ssh_host_rsa_key
# Service sshd start and exposed to 22 ports
RUN mkdir / var / run / sshd
CMD [ "/ usr / sbin / sshd", "-D"]
Dockerfile file has its own syntax and commands, specifically refer to the official documentation of Docker.
2. With Dockerfile after file, you can create image files according to Dockerfile, located in the directory Dockerfile, run the following command
sudo docker build -t centos6-ssh.
After the command is successful, it will create a name for centos6-ssh the image, you can use the "sudo docker images" to see.
3. At this point you can create your own container according to create image files out above, the following command creates a named "mytest" containers.
sudo docker run -d -P --name = mytest centos6-ssh
4. With a container, you can test our service for ssh.
4.1 run "sudo docker inspect mytest", view the current IP address of the starting container, and then run the following command to test
ssh admin @ < container IP>
4.2 In addition, can also be accessed through the port mapping docker using "sudo docker port mytest 22" view of the current container port 22 corresponding to the host machine's port, and then accessed via the following command
ssh admin @ < host machine IP> -p < port host machine>