基于Docker制作centos7镜像,镜像支持开启sshd服务。
1.制作ssh Dockerfile,并保存
命令:
vim Dockerfile
[root@centos79 ~]# cat Dockerfile
FROM ansible/centos7-ansible
MAINTAINER ztj
RUN rpm --rebuilddb;yum install make wget tar gzip passwd openssh-server gcc -y
ENV REPOS_URL http://mirrors.163.com/.help/CentOS7-Base-163.repo
RUN rm -rf /etc/yum.repos.d/*;wget -P /etc/yum.repos.d/ ${REPOS_URL}
RUN yes|ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
RUN yes|ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
RUN yes|ssh-keygen -q -t ed25519 -f /etc/ssh/ssh_host_ed25519 -N ''
RUN echo 'root' | passwd --stdin root
EXPOSE 22
CMD /usr/sbin/sshd -D
[root@centos79 ~]# pwd
/root
[root@centos79 ~]# ls -l Dockerfile
-rw-r--r-- 1 root root 537 11月 28 17:21 Dockerfile
[root@centos79 ~]#
2.基于Dockerfile创建镜像
命令:
docker build -f Dockerfile -t centos-ssh:1.0 .
[root@centos79 ~]# docker build -f Dockerfile -t centos-ssh:1.0 .
[+] Building 1.0s (11/11) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 576B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/ansible/centos7-ansible:latest 0.0s
=> [1/7] FROM docker.io/ansible/centos7-ansible 0.0s
=> CACHED [2/7] RUN rpm --rebuilddb;yum install make wget tar gzip passwd openssh-server gcc -y 0.0s
=> CACHED [3/7] RUN rm -rf /etc/yum.repos.d/*;wget -P /etc/yum.repos.d/ http://mirrors.163.com/.help/CentOS7-Base-163.repo 0.0s
=> CACHED [4/7] RUN yes|ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N '' 0.0s
=> CACHED [5/7] RUN yes|ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N '' 0.0s
=> [6/7] RUN yes|ssh-keygen -q -t ed25519 -f /etc/ssh/ssh_host_ed25519 -N '' 0.3s
=> [7/7] RUN echo 'root' | passwd --stdin root 0.6s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:ead7db20adee6ab659915ade5341a69aaf4129e233b0c7fc5239ac9bd8a221d4 0.0s
=> => naming to docker.io/library/centos-ssh:1.0 0.0s
[root@centos79 ~]# docker images | grep centos-ssh
centos-ssh 1.0 ead7db20adee 9 seconds ago 796MB
[root@centos79 ~]#
3.基于镜像创建docker容器
命令:
docker run -itd -P --privileged --name centos-ssh centos-ssh:1.0
[root@centos79 ~]# docker run -itd -P --privileged --name centos-ssh centos-ssh:1.0
d623e4c469be290d88e800c1bc24fde9f65cb4a740dc5719e74fec90095520c2
[root@centos79 ~]# docker ps -a | grep centos-ssh
d623e4c469be centos-ssh:1.0 "/bin/sh -c '/usr/sb…" 18 seconds ago Up 17 seconds 0.0.0.0:32786->22/tcp, :::32786->22/tcp centos-ssh
[root@centos79 ~]#
4.进入容器
命令:
docker exec -it centos-ssh /bin/bash
[root@centos79 ssh]# docker exec -it d623 /bin/bash
[root@d623e4c469be ansible]# yum install net-tools
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
Package net-tools-2.0-0.25.20131004git.el7.x86_64 already installed and latest version
Nothing to do
[root@d623e4c469be ansible]# netstat -antulp | grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1/sshd
tcp6 0 0 :::22 :::* LISTEN 1/sshd
[root@d623e4c469be ansible]# hostname
d623e4c469be
[root@d623e4c469be ansible]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
[root@d623e4c469be ansible]#
5.xshell登录验证
ssh 172.20.10.12:32786