使用Dockfile创建带ssh服务的Ubuntu容器
- 环境信息
- OS:CentOS 7 64位
- docker版本
Clientversion: 1.3.2
ClientAPI version: 1.15
Goversion (client): go1.3.3
Gitcommit (client): 39fa2fa/1.3.2
OS/Arch(client): linux/amd64
Serverversion: 1.3.2
ServerAPI version: 1.15
Goversion (server): go1.3.3
Gitcommit (server): 39fa2fa/1.3.2
具体过程如下:
- 编辑Dockfile
点击(此处)折叠或打开
- FROM ubuntu:14.04
- MAINTAINER yuj <yuj@cn.fujitsu.com>
- ENV http_proxy http://IP:Port
- ENV https_proxy http://IP:Port
- RUN apt-get update && apt-get install -y openssh-server
- RUN mkdir /var/run/sshd
- RUN echo 'root:fnst1234' | chpasswd
- RUN sed -i 's/PermitRootLogin without-password/PermitRootLogin yes/' /etc/ssh/sshd_config
- # SSH login fix. Otherwise user is kicked off after login
- RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
- ENV NOTVISIBLE "in users profile"
- RUN echo "export VISIBLE=now" >> /etc/profile
- EXPOSE 22
- CMD ["/usr/sbin/sshd", "-D"]
- FROM ubuntu:14.04
- 执行安装
点击(此处)折叠或打开
- vi Dockerfile
- docker build -t ubuntu/ssh .
- docker run -d ubuntu/ssh /usr/sbin/sshd -D
- docker ps
- docker exec 0fd3722ea2a0 ifconfig
- ping 172.17.0.13
- ssh root@172.17.0.13
- vi Dockerfile
- 绑定主机端口
点击(此处)折叠或打开
- [root@localhost dockertest]# docker ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 0fd3722ea2a0 ubuntu/ssh:latest "/usr/sbin/sshd -D" About an hour ago Up About an hour 22/tcp sharp_pare
- [root@localhost dockertest]# docker kill 0fd3722ea2a0
- 0fd3722ea2a0
- [root@localhost dockertest]# docker run -p 2222:22 -d ubuntu/ssh /usr/sbin/sshd -D
- 2364be793b5ac8369c7695c0706a90bd6222dae8545c8a3944f6f1526d08c640
- [root@localhost dockertest]# docker ps
- CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
- 2364be793b5a ubuntu/ssh:latest "/usr/sbin/sshd -D" 3 seconds ago Up 1 seconds 0.0.0.0:2222->22/tcp cranky_wilson
- [root@localhost dockertest]# ssh root@localhost -p 2222
- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- @ WARNING: REMOTE HOST IDENTIFICATION HAS @
- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING
- Someone could be eavesdropping on you right now (man-in-the-middle attack)!
- It is also possible that a host key has just been changed.
- The fingerprint for the ECDSA key sent by the remote host is
- 85:14:5a:b5:c5:f8:7a:a3:6c:19:f0:13:6e:91:82:94.
- Please contact your system administrator.
- Add correct host key in /root/.ssh/known_hosts to get rid of this message.
- Offending ECDSA key in /root/.ssh/known_hosts:2
- ECDSA host key for [localhost]:2222 has changed and you have requested strict checking.
- Host key verification failed.
- [root@localhost dockertest]# rm /root/.ssh/known_hosts
- rm:是否?除普通文件 "/root/.ssh/known_hosts"?y
- [root@localhost dockertest]# ssh root@localhost -p 2222
- The authenticity of host '[localhost]:2222 ([::1]:2222)' can't be established.
- ECDSA key fingerprint is 85:14:5a:b5:c5:f8:7a:a3:6c:19:f0:13:6e:91:82:94.
- Are you sure you want to continue connecting (yes/no)? yes
- Warning: Permanently added '[localhost]:2222' (ECDSA) to the list of known hosts.
- root@localhost's password:
- Welcome to Ubuntu 14.04 LTS (GNU/Linux 3.2.0-61-generic x86_64)
- * Documentation: https://help.ubuntu.com/
- The programs included with the Ubuntu system are free software;
- the exact distribution terms for each program are described in the
- individual files in /usr/share/doc/*/copyright.
- Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
- applicable law.
- [root@localhost dockertest]# docker ps
- 其他:从容器导出tar文件并导入到新的Server上
点击(此处)折叠或打开
- # 导出服务器(from container)
- # root@localhost dockertest]# docker export 2364be793b5a > ubuntu_ssh
- # 导入服务器(from container)
- # scp root@10.167.133.136:/root/dockertest/ubuntu_ssh.tar /root/dockertest/
- # cat ubuntu_ssh.tar | docker import - aaaaa:11111
- # docker images
- # docker run -d aaaaa /usr/sbin/sshd -D
- # docker rm $(docker ps -q -a)
- # docker run -d aaaaa:11111 /usr/sbin/sshd -D
- # docker exec a77a5b51578a ifconfig
- # ping 172.17.0.64
- # ssh root@172.17.0.64
- # 导出服务器(from container)