Create a docker image that supports SSH

Source: Internet
Author: User
Tags mkdir ssh centos docker run
Docker container run, generally cannot ssh, this container management brings trouble, below will explain how to create a docker image that supports SSH

Download the Ubuntu image from the dock hub first

Command: Docker pull Ubuntu

Create a new Ssh_ubuntu directory

Mkdir/ssh_ubuntu

Cd/ssh_ubuntu; Touch Dockerfile

VI Dockerfile

Dockerfile Content

From Ubuntu
Maintainer Lincoln_zhong
RUN apt-get Update
RUN apt-get install-y openssh-server sudo
RUN mkdir-p/var/run/sshd
RUN Sed-ri ' s/session required pam_loginuid.so/#session required pam_loginuid.so/g '/etc/pam.d/sshd
RUN Useradd Admin
RUN echo "Admin:admin" | chpasswd
RUN echo "admin all= (All) all" >>/etc/sudoers
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

Cd/ssh_ubuntu. Run Docker build-t Ssh_ubuntu. #ssh_ubuntu是镜像名称

Running Docker images

Run a Container test command: Docker run-d--name ssh_ubuntu_test-p 0.0.0.0:10000:22 Ssh_ubuntu

Command parsing

-D is started in daemon mode

--name Defining container Names

-P is a port that defines ports (above this means that 10000 ports on the local host (host) are mapped to the 22 ports of the container

Container ssh_ubuntu_test run successfully, SSH login to the container Test command: SSH admin@ host ip-p 10000 I'm here for 10.10.1.181.

SSH admin@10.10.1.181-p 10000

Test success

CentOS-built container dockerfile content for SSH support

# Select an existing OS image as the basis
From CentOS

# The author of the image
Maintainer LINCOLN ZHONG

# Install the Openssh-server and sudo packages and set the sshd usepam parameter to No
RUN Yum install-y openssh-server sudo
RUN sed-ir ' s/usepam yes/usepam no/g '/etc/ssh/sshd_config

# Add test user admin, Password Admin, and add this user to Sudoers
RUN Useradd Admin
RUN echo "Admin:admin" | chpasswd
RUN echo "admin all= (All) all" >>/etc/sudoers

# The following two sentences are more special, must have on the CENTOS6, otherwise created by the container sshd cannot log on
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 ports
RUN mkdir/var/run/sshd
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]


This article is from the "Last Night Rain Wind Sudden" blog, please be sure to keep this source http://zxx287856774.blog.51cto.com/3417296/1664682

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.