环境准备:
[root@coker1 ~]# docker rm $(docker ps -qa)
de7658cf81e8
9f9d049bacf3
0bf9b1d0da01
89a4fdf6bbf4
64a3084c0680
自定义镜像:
mkdir oo
cd oo
ls
vim Dockerfile
FROM centos:latest
RUN rm -f /etc/yum.repo.d/*.repo
ADD local.repo /etc/yum.repo.d/r.repo
RUN yum install -y net-tools psmisc vim-enhanced
:wq
cp /etc/yum.repos.d/dvd.repo local.repo
[root@coker1 oo]# docker build -t centos:latest . //必须加 .
基于自定以的镜像,封装应用:
[root@coker1 oo]# cd ~
[root@coker1 ~]# mkdir xx
[root@coker1 ~]# cd xx
[root@coker1 xx]# ls
[root@coker1 xx]# docker images
[root@coker1 xx]# vim Dockerfile
FROM centos:latest
CMD ["/usr/bin/python"]
:wq
[root@coker1 xx]# docker build -t centos:python .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM centos:latest
---> 72e49259bbd0
Step 2 : CMD /usr/bin/python
---> Running in 8b6218b82c85
---> 1da1e1ac94f8
Removing intermediate container 8b6218b82c85
Successfully built 1da1e1ac94f8
[root@coker1 xx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos python 1da1e1ac94f8 About a minute ago 346.3 MB
centos latest 72e49259bbd0 11 minutes ago 346.3 MB
docker 封装应用(手动操作):
[root@coker1 ooxx]# docker run -it centos
[root@f35180402068 /]# yum search openssh //发现软件
[root@f35180402068 /]# yum -y install openssh-server //安装软件
[root@f35180402068 /]# echo 11 | passwd --stdin root
Changing password for user root.
passwd: all authentication tokens updated successfully.
[root@f35180402068 /]# systemctl start sshd
Failed to get D-Bus connection: Operation not permitted
[root@f35180402068 /]# cd /usr/lib/systemd/system
[root@f35180402068 system]# cat sshd.server
cat: sshd.server: No such file or directory
[root@f35180402068 system]# cat sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
[root@f35180402068 system]# echo $OPTIONS
[root@f35180402068 system]# /usr/sbin/sshd -D
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.
[root@f35180402068 system]# cd /etc/ssh/
[root@f35180402068 ssh]# ls
moduli sshd_config
[root@f35180402068 ssh]# sshd-keygen
/usr/sbin/sshd-keygen: line 10: /etc/rc.d/init.d/functions: No such file or directory
Generating SSH2 RSA host key: /usr/sbin/sshd-keygen: line 63: success: command not found
Generating SSH2 ECDSA host key: /usr/sbin/sshd-keygen: line 105: success: command not found
Generating SSH2 ED25519 host key: /usr/sbin/sshd-keygen: line 126: success: command not found
[root@f35180402068 ssh]# ls
moduli ssh_host_ed25519_key ssh_host_rsa_key.pub
ssh_host_ecdsa_key ssh_host_ed25519_key.pub sshd_config
ssh_host_ecdsa_key.pub ssh_host_rsa_key
[root@f35180402068 ssh]# /usr/sbin/sshd -D
^C
[root@f35180402068 ssh]# ifconfig
eth0: flags=4163 mtu 1500
inet 172.17.0.2 netmask 255.255.0.0 broadcast 0.0.0.0
inet6 fe80::42:acff:fe11:2 prefixlen 64 scopeid 0x20
ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet)
RX packets 506 bytes 1135916 (1.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 508 bytes 40748 (39.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@4ecd2c26a020 ssh]# /usr/sbin/sshd -D //另外一个终端连接容器必须启动服务:
从另外一个终端连接服务:
[root@room9pc01 ~]# ssh 192.168.1.50
[email protected]'s password:
Last login: Tue Aug 21 22:27:02 2018 from 192.168.1.254
[root@coker1 ~]# ssh 172.17.0.2
The authenticity of host '172.17.0.2 (172.17.0.2)' can't be established.
ECDSA key fingerprint is SHA256:s6AUXgR6Tv0QDtvNuKOgE32dyJ4lPKNJtXlFnsyBttM.
ECDSA key fingerprint is MD5:70:c6:3c:60:ac:9d:5e:1e:dd:3c:e8:a4:d5:44:ed:44.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.17.0.2' (ECDSA) to the list of known hosts.
[email protected]'s password:
[root@4ecd2c26a020 ~]# logout
Connection to 172.17.0.2 closed.
自动封装程序:
[root@coker1 ooxx]# vim Dockerfile
FROM centos
RUN yum -y install openssh-server
RUN echo 11 | passwd --stdin root
RUN sshd-keygen
ENV EnvironmentFile=/etc/sysconfig/sshd
CMD [" /usr/sbin/sshd"," -D"]
:wq
[root@coker1 ooxx]# docker build -t centos:sshd .
Successfully built a550c02ecc83
[root@coker1 ooxx]# docker run -d centos:sshd
2ee4a3650a26052ff95ac3475036484c146bd5f7440a2d48128215a7291a8d5e
[root@coker1 ooxx]# docker inspect 2ee4a3
[root@coker1 ooxx] ssh 172.17.0.2