docker
1.docker容器
[root@docker ~]# systemctl start docker[root@docker ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos latest 5d0da3dc9764 2 years ago 231MB# 容器执行完就退出了[root@docker ~]# docker run -it --name=a0 centos:latest echo "abc"abc# 没有正在运行的进程[root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES# 容器已经创建了,但是是exiting状态[root@docker ~]# docker ps --allCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESba5a978343c1 centos:latest "echo abc" 34 seconds ago Exited (0) 33 seconds ago a021d6b0b5e067 centos:latest "/bin/bash" 19 hours ago Exited (255) 17 minutes ago c1# 启动a0容器[root@docker ~]# docker start a0a0# a0容器没有启动成功[root@docker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES# 删除容器[root@docker ~]# docker rm a0a0[root@docker ~]# docker ps -allCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES21d6b0b5e067 centos:latest "/bin/bash" 19 hours ago Exited (255) 26 minutes ago c1[root@docker ~]# docker start c1c1[root@docker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES21d6b0b5e067 centos:latest "/bin/bash" 19 hours ago Up 4 seconds c1[root@docker ~]# docker attach c1[root@21d6b0b5e067 /]# # ctrl+p+q退出容器但不中断进程[root@21d6b0b5e067 /]# curl localhostread escape sequence# 进程一直在[root@docker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES21d6b0b5e067 centos:latest "/bin/bash" 19 hours ago Up 4 minutes c1# 容器进程运行的时候删不掉[root@docker ~]# docker rm c1Error response from daemon: cannot remove container "/c1": container is running: stop the container before removing or force remove# 帮助指令docker --help
2.镜像打包
# 查看现有镜像[root@docker ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos latest 5d0da3dc9764 2 years ago 231MB# 将镜像保存为一个tar包[root@docker ~]# docker save -o centos.tar centos:latest # 打包镜像成功[root@docker ~]# lsanaconda-ks.cfg centos.tar
3.删除镜像
[root@docker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES[root@docker ~]# docker ps --allCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES21d6b0b5e067 centos:latest "/bin/bash" 20 hours ago Exited (0) 8 seconds ago c162dd0916fd92 centos:latest "/bin/bash" 20 hours ago Exited (127) 20 hours ago c0# 如果有这个镜像生成的容器,就无法移除镜像[root@docker ~]# docker rmi centos:latest Error response from daemon: conflict: unable to remove repository reference "centos:latest" (must force) - container 62dd0916fd92 is using its referenced image 5d0da3dc9764
4.删除容器
# 删除容器,要退出容器up状态、[root@docker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES21d6b0b5e067 centos:latest "/bin/bash" 20 hours ago Up 44 minutes c1[root@docker ~]# docker attach c1[root@21d6b0b5e067 /]# exit exit# 使用docker ps --all确定没有镜像[root@docker ~]# docker ps --allCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES21d6b0b5e067 centos:latest "/bin/bash" 20 hours ago Exited (0) 6 minutes ago c162dd0916fd92 centos:latest "/bin/bash" 20 hours ago Exited (127) 20 hours ago c0[root@docker ~]# docker rm c1c1[root@docker ~]# docker rm c0c0[root@docker ~]# docker ps --allCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5.成功删除镜像
[root@docker ~]# docker rmi centos:latest Untagged: centos:latestUntagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59[root@docker ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZE
6.上传镜像
[root@docker ~]# lsanaconda-ks.cfg centos.tar[root@docker ~]# docker load -i centos.tar 74ddd0ec08fa: Loading layer 238.6MB/238.6MBLoaded image: centos:latest[root@docker ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos latest 5d0da3dc9764 2 years ago 231MB
7.打包配置好阿里云镜像的容器
[root@docker ~]# docker ps --allCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES[root@docker ~]# docker run -it --name c0 centos:latest /bin/bash[root@db931a8ef3c8 /]# cd /etc/yum.repos.d/[root@db931a8ef3c8 yum.repos.d]# lsCentOS-Linux-AppStream.repo CentOS-Linux-FastTrack.repoCentOS-Linux-BaseOS.repo CentOS-Linux-HighAvailability.repoCentOS-Linux-ContinuousRelease.repo CentOS-Linux-Media.repoCentOS-Linux-Debuginfo.repo CentOS-Linux-Plus.repoCentOS-Linux-Devel.repo CentOS-Linux-PowerTools.repoCentOS-Linux-Extras.repo CentOS-Linux-Sources.repo[root@db931a8ef3c8 yum.repos.d]# cd [root@db931a8ef3c8 ~]# rm -rf /etc/yum.repos.d/*[root@db931a8ef3c8 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed0 0 0 0 0 0 0 0 --:--:-- 0:01:06 --:--:-- 0^C[root@db931a8ef3c8 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed100 2495 100 2495 0 0 411 0 0:00:06 0:00:06 --:--:-- 520[root@db931a8ef3c8 ~]# ls /etc/yum.repos.d/CentOS-Base.repo[root@db931a8ef3c8 ~]# yum clean all && yum makecacheFailed to set locale, defaulting to C.UTF-80 files removedFailed to set locale, defaulting to C.UTF-8CentOS-8.5.2111 - Base - mirrors.aliyun.com 157 kB/s | 4.6 MB 00:29 CentOS-8.5.2111 - Extras - mirrors.aliyun.com 7.4 kB/s | 10 kB 00:01 CentOS-8.5.2111 - AppStream - mirrors.aliyun.com 140 kB/s | 8.4 MB 01:01 Metadata cache created.[root@db931a8ef3c8 ~]# yum -y install epel-release[root@db931a8ef3c8 ~]# # [root@docker ~]# [root@docker ~]# docker ps --allCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESdb931a8ef3c8 centos:latest "/bin/bash" 31 minutes ago Up 30 minutes c0[root@docker ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESdb931a8ef3c8 centos:latest "/bin/bash" 31 minutes ago Up 31 minutes c0# 从容器导出tar包[root@docker ~]# docker export -o centos_yum.tar c0 [root@docker ~]# lsanaconda-ks.cfg centos.tar centos_yum.tar
8.将带有阿里云镜像的容器的tar包导入成镜像
[root@docker ~]# lsanaconda-ks.cfg centos.tar centos_yum.tar# 导入镜像[root@docker ~]# docker import -m yum centos_yum.tar centos:yumsha256:d79f0f2ff79e8b04b1eab2167c84d1723721af8139d92961de6bd66024487779[root@docker ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos yum d79f0f2ff79e 20 seconds ago 260MBcentos latest 5d0da3dc9764 2 years ago 231MB
9.将配置好httpd服务的容器打包成镜像
[root@docker ~]# docker stop c0c0[root@docker ~]# docker ps --allCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESdb931a8ef3c8 centos:latest "/bin/bash" 45 minutes ago Exited (0) 8 seconds ago c0[root@docker ~]# docker rm c0c0[root@docker ~]# docker ps --allCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES[root@docker ~]# docker run -it --name c0 centos:yum /bin/bash[root@9a56387c522c /]# lsbin etc lib lost+found mnt proc run srv tmp vardev home lib64 media opt root sbin sys usr[root@9a56387c522c /]# ls /etc/yum.repos.d/CentOS-Base.repo epel-playground.repo epel-testing.repoepel-modular.repo epel-testing-modular.repo epel.repo[root@9a56387c522c /]# yum -y install httpd [root@9a56387c522c /]# yum -y install net-tools[root@9a56387c522c /]# yum list installed|grep httpdFailed to set locale, defaulting to C.UTF-8centos-logos-httpd.noarch 85.8-2.el8 @base httpd.x86_64 2.4.37-43.module_el8.5.0+1022+b541f3b1 @AppStreamhttpd-filesystem.noarch 2.4.37-43.module_el8.5.0+1022+b541f3b1 @AppStreamhttpd-tools.x86_64 2.4.37-43.module_el8.5.0+1022+b541f3b1 @AppStream[root@9a56387c522c /]# echo "docker_httpd_server" > /var/www/html/index.html[root@9a56387c522c /]# httpd -k startAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message[root@9a56387c522c /]# curl localhostdocker_httpd_server[root@9a56387c522c /]# [root@docker ~]# [root@docker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES9a56387c522c centos:yum "/bin/bash" 5 minutes ago Up 5 minutes c0[root@docker ~]# docker export -o yum_httpd.tar c0[root@docker ~]# lsanaconda-ks.cfg centos.tar centos_yum.tar yum_httpd.tar[root@docker ~]# docker import -m httpd yum_httpd.tar centos:httpdsha256:92dfb2054854eb00daf758c5c571dd89b458564afdf4039cc441f95d3aca64b9[root@docker ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos httpd 92dfb2054854 11 seconds ago 310MBcentos yum d79f0f2ff79e 15 minutes ago 260MBcentos latest 5d0da3dc9764 2 years ago 231MB
10.使用配置好httpd环境的容器(打包成镜像)创建一个容器
创建的容器里也有httpd服务
[root@docker ~]# docker run -it --name c2 centos:httpd /bin/bash[root@f71014720c36 /]# httpd -k startAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message[root@f71014720c36 /]# curl localhostdocker_httpd_server[root@f71014720c36 /]# ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255ether 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 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10<host>loop txqueuelen 1000 (Local Loopback)RX packets 12 bytes 1011 (1011.0 B)RX errors 0 dropped 0 overruns 0 frame 0TX packets 12 bytes 1011 (1011.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@f71014720c36 /]# [root@docker ~]# ping 172.17.0.3PING 172.17.0.3 (172.17.0.3) 56(84) bytes of data.64 bytes from 172.17.0.3: icmp_seq=1 ttl=64 time=0.208 ms64 bytes from 172.17.0.3: icmp_seq=2 ttl=64 time=0.138 ms^C--- 172.17.0.3 ping statistics ---2 packets transmitted, 2 received, 0% packet loss, time 999msrtt min/avg/max/mdev = 0.138/0.173/0.208/0.035 ms[root@docker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf71014720c36 centos:httpd "/bin/bash" 3 minutes ago Up 3 minutes c29a56387c522c centos:yum "/bin/bash" 3 hours ago Up 3 hours c0[root@docker ~]# docker ps -allCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf71014720c36 centos:httpd "/bin/bash" 3 minutes ago Up 3 minutes c2[root@docker ~]# curl 172.17.0.3docker_httpd_server
11.查看ip地址
(1)进入到容器,使用指令
[root@docker ~]# docker start c2c2[root@docker ~]# docker attach c2[root@f71014720c36 /]# yum -y istall net-tools,iprout[root@f71014720c36 /]# ip a s[root@f71014720c36 /]# ifconfigread escape sequence[root@f71014720c36 /]# ifconfig
(2)使用docker指令·inspect
[root@docker ~]# docker inspect c2
(3)在外部调用指令
[root@docker ~]# docker exec c2 ifconfig[root@docker ~]# docker exec c0 ip a
(4)联系测试 使用exec创建一个文件
[root@docker ~]# docker exec c2 touch /opt/test.txt[root@docker ~]# docker exec c2 ls /opttest.txt
12.端口映射
(1)指定端口映射
将容器中的80端口映射到宿主机(docker主机)80端口
[root@docker ~]# docker run -it --name c1 -p80:80/tcp centos:httpd /bin/bash[root@7d2073518aef /]# curl 172.17.0.2docker_httpd_server[root@7d2073518aef /]# curl localhost curl: (7) Failed to connect to localhost port 80: Connection refused[root@7d2073518aef /]# httpd -k startAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.4. Set the 'ServerName' directive globally to suppress this message[root@7d2073518aef /]# curl localhostdocker_httpd_server
[root@docker ~]# docker run -it --name c4 -p12345:80 centos:httpd /bin/bash[root@f1383acb6cb2 /]# curl localhostcurl: (7) Failed to connect to localhost port 80: Connection refused[root@f1383acb6cb2 /]# httpd -k startAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message[root@f1383acb6cb2 /]# curl localhostdocker_httpd_server[root@f1383acb6cb2 /]# [root@docker ~]# [root@docker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf1383acb6cb2 centos:httpd "/bin/bash" About a minute ago Up About a minute 0.0.0.0:12345->80/tcp, :::12345->80/tcp c4
(2).随机端口映射
[root@docker ~]# docker run -it --name c5 -p80 centos:httpd /bin/bash[root@40f35d8764f6 /]# httpd -k startAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.3. Set the 'ServerName' directive globally to suppress this message[root@40f35d8764f6 /]# curl localhostdocker_httpd_server[root@40f35d8764f6 /]# [root@docker ~]# [root@docker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES40f35d8764f6 centos:httpd "/bin/bash" 46 seconds ago Up 45 seconds 0.0.0.0:32768->80/tcp, :::32768->80/tcp c5f1383acb6cb2 centos:httpd "/bin/bash" 4 minutes ago Up 4 minutes 0.0.0.0:12345->80/tcp, :::12345->80/tcp c4
(3)通过其他的ip地址的端口映射容器的端口
[root@docker ~]# ifconfig ens33:0 192.168.2.31 broadcast 192.168.2.31 netmask 255.255.255.255 up[root@docker ~]# docker run -it --name c6 -p192.168.2.31::80 centos:httpd /bin/bash[root@e6177cbb3400 /]# httpd -k startAH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.4. Set the 'ServerName' directive globally to suppress this message[root@e6177cbb3400 /]# curl localhostdocker_httpd_server[root@e6177cbb3400 /]# [root@docker ~]# [root@docker ~]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESe6177cbb3400 centos:httpd "/bin/bash" 48 seconds ago Up 47 seconds 192.168.2.31:32769->80/tcp c640f35d8764f6 centos:httpd "/bin/bash" 7 minutes ago Up 7 minutes 0.0.0.0:32768->80/tcp, :::32768->80/tcp c5f1383acb6cb2 centos:httpd "/bin/bash" 11 minutes ago Up 11 minutes 0.0.0.0:12345->80/tcp, :::12345->80/tcp c4
13.卷的映射
[root@docker ~]# mkdir /source[root@docker ~]# ls /bin dev home lib64 mnt proc run source sys usrboot etc lib media opt root sbin srv tmp var[root@docker ~]# docker run -it --name c3 -v /source:/data centos:httpd /bin/bash[root@a41482a44fd1 /]# lsbin dev home lib64 media opt root sbin sys usrdata etc lib lost+found mnt proc run srv tmp var# ctrl +p +q 退出容器不中断容器的进程[root@a41482a44fd1 /]# [root@docker ~]# # 在宿主机器上的共享目录里创建文件[root@docker ~]# touch /source/wangchuqin# 宿主机中创建的文件会共享到容器的data目录中[root@docker ~]# docker exec c3 ls /datawangchuqin
14.总结:
1.镜像的迁移
(1).打包
docker save -o centos.tar centos:latest
(2).加载
docker load -i centos.tardocker images
(3).删除镜像
docker rmi centos:latest
2.创建镜像
(1).创建容器
docker run -it --name c0 centos:latest /bin/bash
(2).下载阿里云仓库
epelctrl p q
(3).容器打包
docker export -o centos_yum.tar c0
(4).引入镜像
docker import -m "update yum repo" centos_yum.tar centos:yumdocker image ls
3.ip网络
(1).交互式
docker start c0docker attach c0yum -y install iprouteip a
(2).inspect
docker inspect c0
(3).exec
docker exec c0 yum -y install net-toolsdocker exec c0 ifconfig
4.端口
(1).指定端口映射
docker run -it --name c0 -p70:80 centos:httpd /bin/bash
(2).随机端口映射
docker run -it --name c1 -p80 centos:httpd /bin/bash
(3).指定其他ip的端口映射
ifconfig ens33:0 192.168.2.71.51 broadcast 192.168.71.21 netmask 255.255.255.255 updocker run -it --name c2 -p192.168.71.51::80 centos:httpd /bin/bash
5.持久化
挂载
docker run -it --name c3 -v /adc:/def centos:httpd /bin/bashtouch /abc/aaa.txtdocker exec c3 ls /def