获取最新的docker image
# docker pull centos:latest
运行image
# docker run -i -t centos /bin/bash
bash-4.2# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin selinux srv sys tmp usr var
bash-4.2# exit
主要参考
[1]https://docs.docker.com/installation/centos/
[2]Working with Docker Images
通过docker部署ssh
# cat Dockerfile
FROM centos
MAINTAINER dbyin
RUN yum install -y openssh-server
RUN echo "root:root" | chpasswd
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
构建镜象
# docker build --rm -t dbyin/sshd .
Sending build context to Docker daemon 74.75 kB
Sending build context to Docker daemon
Step 0 : FROM centos
---> 1a7dc42f78ba
Step 1 : MAINTAINER dbyin
---> Running in f40dd8c16027
---> ae21b4f05d98
Removing intermediate container f40dd8c16027
Step 2 : RUN yum install -y openssh-server
---> Running in a298b82f5fc1
Loaded plugins: fastestmirror
Determining fastest mirrors
* base: mirrors.cug.edu.cn
* extras: mirrors.cug.edu.cn
* updates: mirrors.cug.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package openssh-server.x86_64 0:6.4p1-8.el7 will be installed
--> Processing Dependency: openssh = 6.4p1-8.el7 for package: openssh-server-6.4p1-8.el7.x86_64
--> Running transaction check
---> Package openssh.x86_64 0:6.4p1-8.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
openssh-server x86_64 6.4p1-8.el7 base 367 k
Installing for dependencies:
openssh x86_64 6.4p1-8.el7 base 341 k
错误1
Error: Cannot start container f82d9d3548b4d19cfb4599bb3b27f74b6d605f167456d509df722c749fdf476f: port has already been allocated
重启docker服务,参见https://github.com/dotcloud/docker/issues/6476
错误2
# ssh root@172.16.213.129 -p 49153 -v
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 172.16.213.129 [172.16.213.129] port 49153.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/identity type -1
debug1: identity file /root/.ssh/identity-cert type -1
debug1: identity file /root/.ssh/id_rsa type -1
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: identity file /root/.ssh/id_dsa type -1
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.4
debug1: match: OpenSSH_6.4 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.3
debug1: SSH2_MSG_KEXINIT sent
Read from socket failed: Connection reset by peer
增加以下内容重新构建镜像
RUN ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key
RUN ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key
然后出现
。。。
debug1: Exit status 254
增加以下内容重新构建镜像,参考这里
RUN sed -ri 's/UsePAM yes/#UsePAM yes/g' /etc/ssh/sshd_config
RUN sed -ri 's/#UsePAM no/UsePAM no/g' /etc/ssh/sshd_config
访问
# ssh root@172.16.213.129 -p 49153
主要参考
[1]Dockerizing an SSH Daemon Service
[2]Docker: Using Linux Containers to Support Portable Application Deployment