Create a docker container that supports SSH

Source: Internet
Author: User
Tags docker ps

Operating System: VM centos6.5 x64 192.168.240.148

Docker container: centos6.5 x64 172.17.0.3


1. Download the original image


[[Email protected] ~] # Docker pull centos ### generally pull the latest centos7 and centos6.5. Here we use centos6.5 ###


2. install OpenSSH

[[Email protected] ~] # Docker run-I-t kw1073/centos-new4/bin/bashbash-4.1 # Yum-y install OpenSSH-server openssh-clientsbash-4.1 # chkconfig sshd onbash-4.1 # service sshd start ##### display as follows # ####
Generating SSH1 RSA host key:                              [  OK  ]Generating SSH2 RSA host key:                              [  OK  ]Generating SSH2 DSA host key:                              [  OK  ]Starting sshd:                                             [  OK  ]


3. SSH to the host

bash-4.1#ssh 192.168.240.148 -p61618[email protected]‘s password: Last login: Thu Jul 31 22:39:38 2014 from 192.168.240.1[[email protected] ~]#


4. Install passwd

Bash-4.1 # Yum install passwd ### original image centos6.5 on the official docker website does not have passwd, used to modify the password of the root user ### bash-4.1 # passwd rootbash-4.1 # useradd kw1073


5. Use the commit command to save the image

[[Email protected] ~] # Docker PS-l ### view docker ID ###

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/43/5D/wKiom1PZ7TqiK8bWAADDsE7t8k0901.jpg "Title =" 1.png" alt = "wkiom1pz7tqik8bwaaddse7t8k0901.jpg"/>

[[Email protected] ~] # Docker commit 6b84dba2a9bb kw1073/centos-new4 ### making docker images that support SSH ###


6. Port ing on the host machine

[[Email protected] ~] # Docker run-u-t-P 127.0.0.1: 222: 22 kw1073/centos-new4/bin/bash # Do a Good Job of port ing between the docker image and the host, enter the bash environment ###


7. Start the SSH service in the docker container

bash-4.1#service sshd startbash-4.1#ifconfig
eth0 Link encap:Ethernet HWaddr 82:E2:4F:F5:12:E9          inet addr:172.17.0.3 Bcast:0.0.0.0 Mask:255.255.0.0          inet6 addr: fe80::80e2:4fff:fef5:12e9/64 Scope:Link          UP BROADCAST RUNNING MTU:1500 Metric:1          RX packets:4882 errors:0 dropped:0 overruns:0 frame:0          TX packets:4107 errors:0 dropped:0 overruns:0 carrier:0          collisions:0 txqueuelen:1000          RX bytes:27592541 (26.3 MiB) TX bytes:342979 (334.9 KiB)


8. SSH connection on the host machine

[[email protected] ~]#ssh 172.0.0.3The authenticity of host ‘172.17.0.3 (172.17.0.3)‘ can‘t be established.RSA key fingerprint is 40:4f:f2:9a:55:07:9e:a0:cb:42:60:4f:92:fa:90:e7.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added ‘172.17.0.3‘ (RSA) to the list of known hosts.[email protected]‘s password: Last login: Thu Jul 31 10:26:24 2014 from 172.17.42.1Connection to 172.17.0.3 closed.


9. Modify the SSH configuration in the container

#### From the previous step, we can see that the SSH connection is incorrect. you can log on, but it will be disconnected immediately upon login ####
Bash-4.1 # sed-I's/usepam Yes/usepam no/'/etc/ssh/sshd_config # modify the sshd configuration file bash-4.1 #/etc/init. d/sshd reload bash-4.1 #/etc/init. d/sshd restart # Restart sshd service stopping sshd: [OK] Starting sshd: [OK]


10. Retry an SSH connection on the host machine.

[[Email protected] Dist] # SSH 172.17.0.3 [email protected]'s password: Last login: Thu Jul 31 11:48:51 2014 from 172.17.42.1-Bash-4.1 # ls or the following connection: [[email protected] Dist] # SSH [email protected]-p222 [email protected]'s password: Last login: thu Jul 31 15:36:56 2014 from 172.17.42.1 #### because of port ing, you can directly log on from the mapped port ###


11. Set the environment variables in the docker container

####### Copy a copy directly from the host ######## [[email protected] Dist] # SCP/root /. bash * 172.17.0.3:/root/


12. ressh to the container on the host machine

[[email protected] dist]# ssh 172.17.0.3 [email protected]‘s password: Last login: Thu Jul 31 15:07:00 2014 from 172.17.42.1 [[email protected] ~]#


13. Save the configured docker container as an image

####### View the currently running docker container ###### [email protected] ~] # Docker PS-

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/43/5E/wKioL1PZ8WGQEfVEAAFkJJ6Wx5s434.jpg "Title =" 2.png" alt = "wkiol1pz8wgqefveaafkjj6wx5s434.jpg"/>

####### Save the container as an image ###### [[email protected] ~] # Docker commit 89741e6f8e61 kw1073/centos-new517a270866d48429d582aae50b6f3183a4e76c5b4813b1f6e96b5684cb222b100
##### View the current image ######## [[email protected] ~] # Docker Images

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/43/5E/wKioL1PZ8oeiGVA1AAKPBPYmO90349.jpg "Title =" 3.png" alt = "wkiol1pz8oeigva1aakpbpymo90349.jpg"/>

This article is from the "O & M Pawn" blog and will not be reposted!

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.