一、 Docker安装
快速安装Docker
curl https://download.docker.com/linux/centos/docker-ce.repo -o /etc/yum.repos.d/docker.repo yum install -y docker-ce
检查Docker安装版本
[root@Docker ~]# docker -v Docker version 19.03.13, build 4484c46d9d
启用Docker服务与开机启动
[root@Docker ~]# systemctl start docker [root@Docker ~]# systemctl enable docker Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service. [root@Docker ~]# systemctl status docker ● docker.service - Docker Application Container Engine Loaded: loaded (/usr/lib/systemd/system/docker.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2020-11-02 11:19:41 CST; 13s ago Docs: https://docs.docker.com Main PID: 4190 (dockerd) CGroup: /system.slice/docker.service └─4190 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock Nov 02 11:19:39 Docker dockerd[4190]: time="2020-11-02T11:19:39.792639487+08:00" level=info msg="scheme \"unix\" not registered, fallb…ule=grpc Nov 02 11:19:39 Docker dockerd[4190]: time="2020-11-02T11:19:39.793114923+08:00" level=info msg="ccResolverWrapper: sending update to …ule=grpc Nov 02 11:19:39 Docker dockerd[4190]: time="2020-11-02T11:19:39.793153439+08:00" level=info msg="ClientConn switching balancer to \"pi…ule=grpc Nov 02 11:19:39 Docker dockerd[4190]: time="2020-11-02T11:19:39.966020891+08:00" level=info msg="Loading containers: start." Nov 02 11:19:41 Docker dockerd[4190]: time="2020-11-02T11:19:41.087451796+08:00" level=info msg="Default bridge (docker0) is assigned …address" Nov 02 11:19:41 Docker dockerd[4190]: time="2020-11-02T11:19:41.456084302+08:00" level=info msg="Loading containers: done." Nov 02 11:19:41 Docker dockerd[4190]: time="2020-11-02T11:19:41.487859006+08:00" level=info msg="Docker daemon" commit=4484c46d9d grap…19.03.13 Nov 02 11:19:41 Docker dockerd[4190]: time="2020-11-02T11:19:41.488088145+08:00" level=info msg="Daemon has completed initialization" Nov 02 11:19:41 Docker dockerd[4190]: time="2020-11-02T11:19:41.765544592+08:00" level=info msg="API listen on /var/run/docker.sock" Nov 02 11:19:41 Docker systemd[1]: Started Docker Application Container Engine. Hint: Some lines were ellipsized, use -l to show in full.
二、 Docker镜像管理
查找镜像: [root@Docker ~]# docker search centos NAME DESCRIPTION STARS OFFICIAL AUTOMATED centos The official build of CentOS. 6263 [OK] ansible/centos7-ansible Ansible on Centos7 132 [OK] consol/centos-xfce-vnc Centos container with "headless" VNC session… 123 [OK] jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 115 [OK] centos/systemd systemd enabled base container. 86 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 84 imagine10255/centos6-lnmp-php56 centos6-lnmp-php56 58 [OK] tutum/centos Simple CentOS docker image with SSH access 46 centos/postgresql-96-centos7 PostgreSQL is an advanced Object-Relational … 46 kinogmt/centos-ssh CentOS with SSH 29 [OK] pivotaldata/centos-gpdb-dev CentOS image for GPDB development. Tag names… 13 guyton/centos6 From official centos6 container with full up… 10 [OK] centos/tools Docker image that has systems administration… 6 [OK] drecom/centos-ruby centos ruby 6 [OK] pivotaldata/centos Base centos, freshened up a little with a Do… 5 pivotaldata/centos-mingw Using the mingw toolchain to cross-compile t… 3 mamohr/centos-java Oracle Java 8 Docker image based on Centos 7 3 [OK] darksheer/centos Base Centos Image -- Updated hourly 3 [OK] pivotaldata/centos-gcc-toolchain CentOS with a toolchain, but unaffiliated wi… 3 indigo/centos-maven Vanilla CentOS 7 with Oracle Java Developmen… 1 [OK] blacklabelops/centos CentOS Base Image! Built and Updates Daily! 1 [OK] mcnaughton/centos-base centos base image 1 [OK] smartentry/centos centos with smartentry 0 [OK] pivotaldata/centos7-dev CentosOS 7 image for GPDB development 0 pivotaldata/centos6.8-dev CentosOS 6.8 image for GPDB development 0 下载镜像: [root@Docker ~]# docker pull centos Using default tag: latest latest: Pulling from library/centos 3c72a8ed6814: Pull complete Digest: sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd Status: Downloaded newer image for centos:latest docker.io/library/centos:latest 查看镜像: [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 0d120b6ccaa8 2 months ago 215MB 删除镜像: [root@Docker ~]# docker rmi centos{镜像名称或镜像ID} Untagged: centos:latest Untagged: centos@sha256:76d24f3ba3317fa945743bb3746fbaf3a0b752f10b10376960de01da70685fbd Deleted: sha256:0d120b6ccaa8c5e149176798b3501d4dd1885f961922497cd0abef155c869566 Deleted: sha256:291f6e44771a7b4399b0c6fb40ab4fe0331ddf76eda11080f052b003d96c7726 镜像标签(类似VMware链接快照): [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 0d120b6ccaa8 2 months ago 215MB leon_centos latest 0d120b6ccaa8 2 months ago 215MB leon_centos leontag 0d120b6ccaa8 2 months ago 215MB 取消标签: [root@Docker ~]# docker rmi leon_centos Untagged: leon_centos:latest [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE leon_centos leontag 0d120b6ccaa8 2 months ago 215MB centos latest 0d120b6ccaa8 2 months ago 215MB [root@Docker ~]# docker rmi leon_centos:leontag Untagged: leon_centos:leontag [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 0d120b6ccaa8 2 months ago 215MB
三、 运行容器
运行容器: -i 表示让容器的标准输入打开,-t表示分配一个伪终端,-d表示后台启动 [root@Docker ~]# docker run -itd centos bash b628807fbe4026bfb403d9c7ef0968aa01f285c0ba339e1fdccd159cdd0e0ed9 查看运行的容器(ps -a查看所有容器): [root@Docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b628807fbe40 centos "bash" 2 minutes ago Up 2 minutes festive_lederberg 进入容器: [root@Docker ~]# docker exec -it b628807fbe40 bash [root@b628807fbe40 /]# 使用容器: [root@b628807fbe40 /]# ifconfig //没有网络 bash: ifconfig: command not found [root@b628807fbe40 /]# yum install -y net-tools Failed to set locale, defaulting to C.UTF-8 CentOS-8 - AppStream 637 kB/s | 5.8 MB 00:09 CentOS-8 - Base 461 kB/s | 2.2 MB 00:04 CentOS-8 - Extras 13 kB/s | 8.1 kB 00:00 Dependencies resolved. Package Architecture Version Repository Size Installing: net-tools x86_64 2.0-0.51.20160912git.el8 BaseOS 323 k Transaction Summary Install 1 Package Total download size: 323 k Installed size: 1.0 M Downloading Packages: net-tools-2.0-0.51.20160912git.el8.x86_64.rpm 248 kB/s | 323 kB 00:01 Total 185 kB/s | 323 kB 00:01 warning: /var/cache/dnf/BaseOS-f6a80ba95cf937f2/packages/net-tools-2.0-0.51.20160912git.el8.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 8483c65d: NOKEY CentOS-8 - Base 1.6 MB/s | 1.6 kB 00:00 Importing GPG key 0x8483C65D: Userid : "CentOS (CentOS Official Signing Key) security@centos.org" Fingerprint: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D From : /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial Key imported successfully Running transaction check Transaction check succeeded. Running transaction test Transaction test succeeded. Running transaction Preparing : 1/1 Installing : net-tools-2.0-0.51.20160912git.el8.x86_64 1/1 Running scriptlet: net-tools-2.0-0.51.20160912git.el8.x86_64 1/1 Verifying : net-tools-2.0-0.51.20160912git.el8.x86_64 1/1 Installed: net-tools-2.0-0.51.20160912git.el8.x86_64 Complete! [root@b628807fbe40 /]# ifconfig //网络已安装 eth0: flags=4163 mtu 1500 inet 172.17.0.2 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:02 txqueuelen 0 (Ethernet) RX packets 6188 bytes 9218989 (8.7 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6192 bytes 458478 (447.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 loop txqueuelen 1000 (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@b628807fbe40 /]# uname -a //和宿主机一样 Linux b628807fbe40 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
Docker网络:
[root@b628807fbe40 /]# ping 114.114.114.114 PING 114.114.114.114 (114.114.114.114) 56(84) bytes of data. 64 bytes from 114.114.114.114: icmp_seq=1 ttl=65 time=1.08 ms ^C --- 114.114.114.114 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 1.075/1.075/1.075/0.000 ms [root@Docker ~]# ifconfig docker0: flags=4163 mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 inet6 fe80::42:1dff:fe79:7ac0 prefixlen 64 scopeid 0x20 ether 02:42:1d:79:7a:c0 txqueuelen 0 (Ethernet) RX packets 6194 bytes 371902 (363.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6182 bytes 9218481 (8.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 # 容器桥接到Docker0网卡: veth3341691: flags=4163 mtu 1500 inet6 fe80::c80c:6dff:feac:b6ad prefixlen 64 scopeid 0x20 ether ca:0c:6d:ac:b6:ad txqueuelen 0 (Ethernet) RX packets 6194 bytes 458618 (447.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 6190 bytes 9219129 (8.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
再次进入容器及停止容器:
[root@Docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b628807fbe40 centos "bash" 2 hours ago Up 2 hours festive_lederberg [root@Docker ~]# docker exec -it festive_lederberg bash [root@b628807fbe40 /]# exit exit [root@Docker ~]# docker stop festive_lederberg festive_lederberg [root@Docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@Docker ~]# docker ps -a //可以查看到退出的容器 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b628807fbe40 centos "bash" 2 hours ago Exited (0) 2 minutes ago festive_lederberg [root@Docker ~]# docker start festive_lederberg festive_lederberg [root@Docker ~]# docker exec -it festive_lederberg bash
删除容器:
[root@Docker ~]# docker rm b628807fbe40 Error response from daemon: You cannot remove a running container b628807fbe4026bfb403d9c7ef0968aa01f285c0ba339e1fdccd159cdd0e0ed9. Stop the container before attempting removal or force remove [root@Docker ~]# docker rm -f b628807fbe40 //强行删除容器 b628807fbe40 [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
命名创建容器:
[root@Docker ~]# docker run --name leon_centos -idt centos 52a3b4e00eac0d764074eb0dfbd4c359fe8bb40ffb2d9f152566080cdb1d53fc [root@Docker ~]# [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 52a3b4e00eac centos "/bin/bash" 8 seconds ago Up 6 seconds leon_centos [root@Docker ~]# docker exec -it leon_centos bash [root@52a3b4e00eac /]#
创建新的容器(快照):
[root@Docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 52a3b4e00eac centos "/bin/bash" 2 minutes ago Up 2 minutes leon_centos [root@Docker ~]# docker commit -m 'install net-tools' -a 'leon' leon_centos centos_installed_net-tools sha256:126143b509649cf2b60f88b9465c0ff2d3a01e7a5c3e3e2f3a4dd3075e29ebc7 [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_installed_net-tools latest 126143b50964 11 seconds ago 246MB centos latest 0d120b6ccaa8 2 months ago 215MB 使用此镜像运行该容器: [root@Docker ~]# docker run --name new_centos -idt centos_installed_net-tools 7d8f1a6144d11a77e1a9912e2a1cd254fa9ec3d01306ef74ac1e04c7e7e34f2c [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7d8f1a6144d1 centos_installed_net-tools "/bin/bash" 6 seconds ago Up 4 seconds new_centos 52a3b4e00eac centos "/bin/bash" 41 minutes ago Up 41 minutes leon_centos 进入容器: [root@Docker ~]# docker exec -it new_centos bash [root@7d8f1a6144d1 /]# ifconfig //天生net-tools eth0: flags=4163 mtu 1500 inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet) RX packets 8 bytes 648 (648.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 lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (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@7d8f1a6144d1 /]# ping 114.114.114.114 PING 114.114.114.114 (114.114.114.114) 56(84) bytes of data. 64 bytes from 114.114.114.114: icmp_seq=1 ttl=79 time=1.14 ms 64 bytes from 114.114.114.114: icmp_seq=2 ttl=86 time=1.07 ms ^C --- 114.114.114.114 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 2ms rtt min/avg/max/mdev = 1.072/1.103/1.135/0.045 ms [root@7d8f1a6144d1 /]# ping 172.17.0.2 //另一个在运行的容器IP PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data. 64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.149 ms ^C --- 172.17.0.2 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms rtt min/avg/max/mdev = 0.149/0.149/0.149/0.000 ms
导出容器:
[root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7d8f1a6144d1 centos_installed_net-tools "/bin/bash" 5 minutes ago Up 5 minutes new_centos 52a3b4e00eac centos "/bin/bash" 46 minutes ago Up 46 minutes leon_centos [root@Docker ~]# docker export -o centos_installed_net-tools.tar new_centos{也可以是容器ID} [root@Docker ~]# ls anaconda-ks.cfg install.sh.1 lnmp1.5.tar.gz mysql-data-dir-backup20200629220120 zabbix-2.2.10.tar.gz centos_installed_net-tools.tar lnmp1.5 lnmp-install.log zabbix-2.2.10 zabbix.sh
导入容器:
[root@Docker ~]# docker stop 7d8f1a6144d1 7d8f1a6144d1 [root@Docker ~]# docker stop 52a3b4e00eac 52a3b4e00eac [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7d8f1a6144d1 centos_installed_net-tools "/bin/bash" 9 minutes ago Exited (0) 12 seconds ago new_centos 52a3b4e00eac centos "/bin/bash" 50 minutes ago Exited (0) 4 seconds ago leon_centos [root@Docker ~]# [root@Docker ~]# docker rm 7d8f1a6144d1 7d8f1a6144d1 [root@Docker ~]# docker rm 52a3b4e00eac 52a3b4e00eac [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_installed_net-tools latest 126143b50964 47 minutes ago 246MB centos latest 0d120b6ccaa8 2 months ago 215MB [root@Docker ~]# docker rmi centos_installed_net-tools Untagged: centos_installed_net-tools:latest Deleted: sha256:126143b509649cf2b60f88b9465c0ff2d3a01e7a5c3e3e2f3a4dd3075e29ebc7 Deleted: sha256:ec297815fd5ee93f31067d7885ff2fac2fd2e276b4c6c4c2151320c46f767c98 [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos latest 0d120b6ccaa8 2 months ago 215MB 导入容器: [root@Docker ~]# docker import centos_installed_net-tools.tar centos_installed_net-tools sha256:bb32efa808bd0df8859314d99462e97025b5af4e6d72f399ec5df49417595581 [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_installed_net-tools latest bb32efa808bd 5 seconds ago 236MB centos latest 0d120b6ccaa8 2 months ago 215MB 运行导入的容器: [root@Docker ~]# docker run --name leon_centos -idt centos_installed_net-tools bash 805c9919066dac92c37234d5711fa2801c08ed5161e351b60263536d1d79ce2e [root@Docker ~]# [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 805c9919066d centos_installed_net-tools "bash" 11 seconds ago Up 9 seconds leon_centos 进入导入的容器: [root@Docker ~]# docker exec -it 805c9919066d bash [root@805c9919066d /]#
四、 Docker仓库管理
[root@Docker ~]# docker pull registry [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_installed_net-tools latest bb32efa808bd 15 minutes ago 236MB centos latest 0d120b6ccaa8 2 months ago 215MB registry latest 2d4f4b5309b1 4 months ago 26.2MB [root@Docker ~]# docker run -d -p 5000:5000 registry ce6ef5f79d6e81eb30c24da904bd4d63f9a386999fde96ed6581b178a6e11828 [root@Docker ~]# curl 127.0.0.1:5000/v2/_catalog {"repositories":[]} [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE centos_installed_net-tools latest bb32efa808bd 18 minutes ago 236MB centos latest 0d120b6ccaa8 2 months ago 215MB registry latest 2d4f4b5309b1 4 months ago 26.2MB [root@Docker ~]# docker tag centos_installed_net-tools 172.26.132.240:5000/leon_centos //IP地址为宿主机的IP地址,端口号为容器仓库映射端口号 [root@Docker ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 172.26.132.240:5000/leon_centos latest bb32efa808bd 19 minutes ago 236MB centos_installed_net-tools latest bb32efa808bd 19 minutes ago 236MB centos latest 0d120b6ccaa8 2 months ago 215MB registry latest 2d4f4b5309b1 4 months ago 26.2MB
修改私有仓库配置文件:
[root@Docker ~]# vim /etc/docker/daemon.json { "insecure-registries":["172.26.132.240:5000"] }
之前运行的容器会停止,重新启动容器:
[root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ce6ef5f79d6e registry "/entrypoint.sh /etc…" 9 minutes ago Exited (2) About a minute ago elastic_hodgkin [root@Docker ~]# docker start ce6ef5f79d6e ce6ef5f79d6e
推送镜像到仓库:
[root@Docker ~]# docker push 172.26.132.240:5000/leon_centos The push refers to repository [172.26.132.240:5000/leon_centos] f5fc2dcc3dc9: Pushed latest: digest: sha256:d164f0fab01b70b9f1075b3d0fc6694518ed85d8d47bb466a3be4ee70236cbad size: 528 [root@Docker ~]# curl 127.0.0.1:5000/v2/_catalog {"repositories":["leon_centos"]} 其它宿主机可以pull该仓库镜像: [root@Docker ~]# docker push 172.26.132.240:5000/leon_centos
五、 Docker数据管理
Docker操作数据无法保存保存在宿主机硬盘内:
[root@d238e359604b /]# uname -a Linux d238e359604b 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux [root@d238e359604b /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@d238e359604b /]# mkdir /data [root@d238e359604b /]# cd /data [root@d238e359604b data]# ls [root@d238e359604b data]# vi leon.txt [root@d238e359604b data]# ls leon.txt [root@d238e359604b data]# cat leon.txt test [root@d238e359604b data]# exit 宿主机: [root@Docker ~]# ls / bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
重启并不会丢失: [root@Docker ~]# docker stop d238e359604b d238e359604b [root@Docker ~]# docker start d238e359604b d238e359604b [root@Docker ~]# docker exec -it leon_centos bash [root@d238e359604b /]# ls bin data dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@d238e359604b /]# cat /data/leon.txt test 删除该容器: [root@Docker ~]# docker stop d238e359604b d238e359604b [root@Docker ~]# docker rm d238e359604b d238e359604b 重新启动并进入该容器文件丢失: [root@Docker ~]# docker run --name leon_centos -idt centos_installed_net-tools bash c5084513b07e57297d29f113ad42eabe503c46a8f3f17611d4e6a22b6363bf3c [root@Docker ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c5084513b07e centos_installed_net-tools "bash" 8 seconds ago Up 6 seconds leon_centos ce6ef5f79d6e registry "/entrypoint.sh /etc…" 24 minutes ago Up 14 minutes 0.0.0.0:5000->5000/tcp elastic_hodgkin [root@Docker ~]# docker exec -it leon_centos bash [root@c5084513b07e /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
挂在本地目录到容器:
[root@Docker ~]# docker rm -f c5084513b07e c5084513b07e 使用-v指定挂载目录,映射本地目录/leon_data到容器目录/data [root@Docker ~]# docker run --name leon_centos -idt -v /leon_data:/data centos_installed_net-tools bash c301fedfdd0cf725face92e43923a97d3f18b2391fd96d4b0ee759e1b41711d3 [root@Docker ~]# docker exec -it leon_centos bash [root@c301fedfdd0c /]# ls bin data dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@c301fedfdd0c /]# cd data/ [root@c301fedfdd0c data]# ls [root@c301fedfdd0c data]# vi leon.txt [root@c301fedfdd0c data]# cat leon.txt test by leon! 宿主机已写入文件: [root@Docker /]# cat /leon_data/leon.txt test by leon!
创建数据卷容器:
创建名为sharevol_centos的容器,并将宿主机leon_data挂载到容器/data目录下: [root@Docker /]# docker run --name sharevol_centos -idt -v /leon_data:/data centos_installed_net-tools bash ede761a92f73127f71bed5d38efc959fda7a7be38f8b9a0ff811237493a7b13c [root@Docker /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ede761a92f73 centos_installed_net-tools "bash" 2 minutes ago Up 2 minutes sharevol_centos ce6ef5f79d6e registry "/entrypoint.sh /etc…" 40 minutes ago Up 30 minutes 0.0.0.0:5000->5000/tcp elastic_hodgkin 进入容器: [root@Docker /]# docker exec -it sharevol_centos bash [root@ede761a92f73 /]# ls bin data dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var 查看容器内文件内容: [root@ede761a92f73 /]# cd data/ [root@ede761a92f73 data]# ls leon.txt [root@ede761a92f73 data]# cat leon.txt test by leon! 创建名为new_centos的容器,并挂载sharevol_centos中的卷: [root@Docker /]# docker run --name new_centos -itd --volumes-from sharevol_centos centos_installed_net-tools bash d30baf088f58d2bfac8279e961681a2f7bc461c7da21c976c3371a7c2a25d82f [root@Docker /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d30baf088f58 centos_installed_net-tools "bash" 39 seconds ago Up 37 seconds new_centos ede761a92f73 centos_installed_net-tools "bash" 25 minutes ago Up 25 minutes sharevol_centos ce6ef5f79d6e registry "/entrypoint.sh /etc…" About an hour ago Up 53 minutes 0.0.0.0:5000->5000/tcp elastic_hodgkin [root@Docker /]# docker exec -it new_centos bash 查看挂载目录: [root@d30baf088f58 /]# ls bin data dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@d30baf088f58 /]# cd data/ [root@d30baf088f58 data]# ls leon.txt [root@d30baf088f58 data]# cat leon.txt test by leon!
六、 Docker网络
下载apache镜像: [root@Docker /]# docker pull httpd Using default tag: latest latest: Pulling from library/httpd bb79b6b2107f: Pull complete 26694ef5449a: Pull complete 7b85101950dd: Pull complete da919f2696f2: Pull complete 3ae86ea9f1b9: Pull complete Digest: sha256:b82fb56847fcbcca9f8f162a3232acb4a302af96b1b2af1c4c3ac45ef0c9b968 Status: Downloaded newer image for httpd:latest docker.io/library/httpd:latest [root@Docker /]# [root@Docker /]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE 172.26.132.240:5000/leon_centos latest bb32efa808bd 2 hours ago 236MB centos_installed_net-tools latest bb32efa808bd 2 hours ago 236MB httpd latest 3dd970e6b110 2 weeks ago 138MB centos latest 0d120b6ccaa8 2 months ago 215MB registry latest 2d4f4b5309b1 4 months ago 26.2MB 启动apache镜像为80端口(80:80,前为宿主机端口,后为容器端口): [root@Docker /]# docker run -itd -p 80:80 httpd bash b8100cec5c9357c9e3d2b3d7e7974225ade3cc144e2244a1d0d28de44db6a83d [root@Docker /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b8100cec5c93 httpd "bash" About a minute ago Up About a minute 0.0.0.0:80->80/tcp happy_nobel d30baf088f58 centos_installed_net-tools "bash" About an hour ago Up About an hour new_centos ede761a92f73 centos_installed_net-tools "bash" 2 hours ago Up 2 hours sharevol_centos ce6ef5f79d6e registry "/entrypoint.sh /etc…" 2 hours ago Up 2 hours 0.0.0.0:5000->5000/tcp elastic_hodgkin # 进入apache容器并启用服务: [root@Docker /]# docker exec -it b8100cec5c93 bash root@b8100cec5c93:/usr/local/apache2# root@b8100cec5c93:/usr/local/apache2# httpd -k start AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.5. Set the 'ServerName' directive globally to suppress this message
Host模式(直接使用宿主机端口)
[root@Docker /]# docker rm -f b8100cec5c93 b8100cec5c93 [root@Docker /]# docker run -idt --net=host httpd bash 527f9b1eec8a1cef759dfc06365b0ffdb0cdee68343645681655ff12dcdce7ce [root@Docker /]# [root@Docker /]# docker exec -it 527f9b1ee bash root@Docker:/usr/local/apache2# root@Docker:/usr/local/apache2# httpd -k start AH00557: httpd: apr_sockaddr_info_get() failed for Docker AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
容器new_centos配置: [root@Docker /]# docker exec -it d30baf088f58 bash [root@d30baf088f58 /]# ifconfig eth0: flags=4163 mtu 1500 inet 172.17.0.4 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:04 txqueuelen 0 (Ethernet) RX packets 9 bytes 690 (690.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 lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (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@d30baf088f58 /]# exit exit 运行容器new_centos_cont,网络与new_centos相同: [root@Docker /]# docker run --name new_centos_cont -itd --net=container:d30baf088f58 centos_installed_net-tools bash e69ee83d4336c337a3b80197b612766deb92736e36d47278f0bd28a048e9f304 [root@Docker /]# [root@Docker /]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e69ee83d4336 centos_installed_net-tools "bash" 5 seconds ago Up 4 seconds new_centos_cont d30baf088f58 centos_installed_net-tools "bash" 2 hours ago Up 2 hours new_centos ede761a92f73 centos_installed_net-tools "bash" 2 hours ago Up 2 hours sharevol_centos ce6ef5f79d6e registry "/entrypoint.sh /etc…" 3 hours ago Up 3 hours 0.0.0.0:5000->5000/tcp elastic_hodgkin 进入docker new_centos_cont: [root@Docker /]# docker exec -it e69ee83d4336 bash [root@d30baf088f58 /]# ifconfig eth0: flags=4163 mtu 1500 inet 172.17.0.4 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:04 txqueuelen 0 (Ethernet) RX packets 9 bytes 690 (690.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 lo: flags=73 mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 loop txqueuelen 1000 (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@d30baf088f58 /]#
[root@Docker ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0 TYPE=Bridge BOOTPROTO=none IPADDR=172.26.132.240 NETMASK=255.255.255.0 GATEWAY=172.26.132.1 DNS1=172.20.3.44 DNS2=172.20.3.45 DEFROUTE=yes PEERDNS=yes PEERROUTES=yes IPV4_FAILURE_FATAL=no #IPV6INIT=yes #IPV6_AUTOCONF=yes NAME=br0 DEVICE=br0 ONBOOT=yes #check_link_down() { #return 1; #} [root@Docker ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens160 TYPE=Ethernet BRIDGE=br0 BOOTPROTO=none #IPADDR=172.26.132.240 #NETMASK=255.255.255.0 #GATEWAY=172.26.132.1 #DNS1=172.20.3.44 #DNS2=172.20.3.45 #PEERDNS=no IPV4_FAILURE_FATAL=no #USERCTL=no #IPV6INIT=yes #IPV6_AUTOCONF=yes NAME=ens160 DEVICE=ens160 ONBOOT=yes #check_link_down() { #return 1; #} [root@Docker ~]# docker run -itd --net=none --name bridge centos_installed_net-tools bash [root@Docker ~]# pipework br0 bridge 172.26.132.243/24@172.26.132.1 [root@Docker ~]# docker exec -it bridge bash [root@13904b8dae8d /]# ifconfig eth1: flags=4163 mtu 1500 inet 172.26.132.243 netmask 255.255.255.0 broadcast 172.26.132.255 ether 46:3f:56:de:fb:0e txqueuelen 1000 (Ethernet) RX packets 99 bytes 6755 (6.5 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1 bytes 42 (42.0 B) 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 loop txqueuelen 1000 (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@13904b8dae8d /]# ping 172.26.132.1 PING 172.26.132.1 (172.26.132.1) 56(84) bytes of data. 64 bytes from 172.26.132.1: icmp_seq=1 ttl=255 time=4.15 ms 64 bytes from 172.26.132.1: icmp_seq=2 ttl=255 time=34.7 ms 64 bytes from 172.26.132.1: icmp_seq=3 ttl=255 time=3.02 ms