Board logo

标题: CentOS8_使用Docker安装Python3 [打印本页]

作者: admin    时间: 2022-12-11 11:42     标题: CentOS8_使用Docker安装Python3

实验中,宿主机的操作系统的版本信息: [root@centos8 ~]# cat /etc/redhat-release CentOS Linux release 8.2.2004 (Core) [root@centos8 ~]# [root@centos8 ~]# uname -r 4.18.0-193.el8.x86_64 先安装依赖软件包: [root@centos8 ~]# yum -y install yum-utils 配置阿里云的Docker镜像地址: [root@centos8 ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo 添加仓库自:http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo [root@centos8 ~]# 生成缓存: [root@centos8 ~]# yum makecache CentOS-8.5.2111 - Base - mirrors.aliyun.com 19 kB/s | 3.9 kB 00:00 CentOS-8.5.2111 - Extras - mirrors.aliyun.com 4.9 kB/s | 1.5 kB 00:00 CentOS-8.5.2111 - AppStream - mirrors.aliyun.com 15 kB/s | 4.3 kB 00:00 Docker CE Stable - x86_64 19 kB/s | 31 kB 00:01 元数据缓存已建立。 [root@centos8 ~]# 安装最新版的Docker: [root@centos8 ~]# yum -y install docker-ce docker-ce-cli containerd.io 启动Docker: [root@centos8 ~]# systemctl start docker [root@centos8 ~]# 启动Docker后,宿主机的网卡会发生变化: [root@centos8 ~]# ifconfig -bash: ifconfig: 未找到命令 [root@centos8 ~]# [root@centos8 ~]# yum -y install net-tools [root@centos8 ~]# ifconfig docker0: flags=4163 mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 inet6 fe80::42:37ff:fe06:3fef prefixlen 64 scopeid 0x20 ether 02:42:37:06:3f:ef txqueuelen 0 (Ethernet) RX packets 8056 bytes 325711 (318.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8451 bytes 17672646 (16.8 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens160: flags=4163 mtu 1500 inet 192.168.168.154 netmask 255.255.255.0 broadcast 192.168.168.255 inet6 fe80::92ab:5fb4:5373:ad53 prefixlen 64 scopeid 0x20 ether 00:0c:29:cb:1e:40 txqueuelen 1000 (Ethernet) RX packets 357979 bytes 519205713 (495.1 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 128916 bytes 8255043 (7.8 MiB) 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 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@centos8 ~]# 查看Docker的版本信息: [root@centos8 ~]# docker -v Docker version 20.10.21, build baeda1f [root@centos8 ~]# 查看Docker相关软件的版本信息: [root@centos8 ~]# docker version Client: Docker Engine - Community Version: 20.10.21 API version: 1.41 Go version: go1.18.7 Git commit: baeda1f Built: Tue Oct 25 18:02:19 2022 OS/Arch: linux/amd64 Context: default Experimental: true Server: Docker Engine - Community Engine: Version: 20.10.21 API version: 1.41 (minimum version 1.12) Go version: go1.18.7 Git commit: 3056208 Built: Tue Oct 25 18:00:24 2022 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.6.10 GitCommit: 770bd0108c32f3fb5c73ae1264f7e503fe7b2661 runc: Version: 1.1.4 GitCommit: v1.1.4-0-g5fd4c4d docker-init: Version: 0.19.0 GitCommit: de40ad0 [root@centos8 ~]# 查看Docker的相关信息: [root@centos8 ~]# docker info Client: Context: default Debug Mode: false Plugins: app: Docker App (Docker Inc., v0.9.1-beta3) buildx: Docker Buildx (Docker Inc., v0.9.1-docker) scan: Docker Scan (Docker Inc., v0.21.0) Server: Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 20.10.21 Storage Driver: overlay2 Backing Filesystem: xfs Supports d_type: true Native Overlay Diff: true userxattr: false Logging Driver: json-file Cgroup Driver: cgroupfs Cgroup Version: 1 Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog Swarm: inactive Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc Default Runtime: runc Init Binary: docker-init containerd version: 770bd0108c32f3fb5c73ae1264f7e503fe7b2661 runc version: v1.1.4-0-g5fd4c4d init version: de40ad0 Security Options: seccomp Profile: default Kernel Version: 4.18.0-193.el8.x86_64 Operating System: CentOS Linux 8 (Core) OSType: linux Architecture: x86_64 CPUs: 1 Total Memory: 1.758GiB Name: centos8.zhuohua.store ID: 5BLP:IZCR:6SS2:2U26:73K7:U7HR:RIFK:NDPD:76V4:Q674:QNTX:GRWQ Docker Root Dir: /var/lib/docker Debug Mode: false Registry: https://index.docker.io/v1/ Labels: Experimental: false Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false [root@centos8 ~]# 注释:Docker的镜像、容器默认是存放在目录/var/lib/docker下 查看宿主机的所有镜像:( Docker安装完毕后,默认是没有任何镜像 ) [root@centos8 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE [root@centos8 ~]# 从公网下载镜像:( 以下是下载Python3.11的镜像 ) [root@centos8 ~]# docker pull python:3.11 3.11: Pulling from library/python a8ca11554fce: Pull complete e4e46864aba2: Pull complete c85a0be79bfb: Pull complete 195ea6a58ca8: Pull complete 157f16ed0a0c: Pull complete 884b144bec28: Pull complete 1c469643b609: Pull complete 4c0ac982aa89: Pull complete 049db2c7eb8a: Pull complete Digest: sha256:edd5164e98f8925a78216fc35841041dea32f00b1668329aa3cee6bf0e53c03b Status: Downloaded newer image for python:3.11 docker.io/library/python:3.11 [root@centos8 ~]# 再次,查看宿主机的所有镜像: [root@centos8 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE python 3.11 ee4e7a0f1c35 2 weeks ago 932MB [root@centos8 ~]# 使用镜像“python:3.11”创建并启动容器“Python3.11”: [root@centos8 ~]# docker run -itd --name Python3.11 -v /opt/python_home:/home/python_home python:3.11 0a2ea514887e4221c7774ab06409743c03bf704a71d433eebdeab9a7b9410594 [root@centos8 ~]# 注释: -itd : 以交互模式情况下后台运行。 --name : 指定容器名称。 -v 挂载目录 : 这里是把宿主机的目录/opt/python_home挂载到容器的目录/home/python_home;假如目录不存在,就会自动创建。 python:3.11 : 镜像名称:版本号 在宿主机查看正在运行的容器: [root@centos8 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0a2ea514887e python:3.11 "python3" 27 seconds ago Up 25 seconds Python3.11 [root@centos8 ~]# ###### 实验中,宿主机中没有安装Python3: [root@centos8 ~]# python3 -bash: python3: 未找到命令 [root@centos8 ~]# 宿主机的硬盘分区情况: [root@centos8 ~]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 7.7G 0 rom nvme0n1 259:0 0 80G 0 disk ├─nvme0n1p1 259:1 0 200M 0 part /boot └─nvme0n1p2 259:2 0 79.8G 0 part ├─cl-root 253:0 0 75.8G 0 lvm / └─cl-swap 253:1 0 4G 0 lvm [SWAP] [root@centos8 ~]# [root@centos8 ~]# df -hTP 文件系统 类型 容量 已用 可用 已用% 挂载点 devtmpfs devtmpfs 883M 0 883M 0% /dev tmpfs tmpfs 901M 0 901M 0% /dev/shm tmpfs tmpfs 901M 17M 884M 2% /run tmpfs tmpfs 901M 0 901M 0% /sys/fs/cgroup /dev/mapper/cl-root xfs 76G 3.2G 73G 5% / /dev/nvme0n1p1 ext4 190M 141M 36M 80% /boot tmpfs tmpfs 181M 0 181M 0% /run/user/0 overlay overlay 76G 3.2G 73G 5% /var/lib/docker/overlay2/b50fc1a2a2e51da58936e98390dccb14301cf16b080b13ed5f85693ce83dce12/merged [root@centos8 ~]# ### 进入容器“Python3.11”: [root@centos8 ~]# docker exec -it Python3.11 /bin/bash root@0a2ea514887e:/# 查看容器“Python3.11”的系统版本信息: root@0a2ea514887e:/# cat /etc/issue Debian GNU/Linux 11 \n \l root@0a2ea514887e:/# root@0a2ea514887e:/# more /etc/debian_version 11.5 root@0a2ea514887e:/# root@0a2ea514887e:/# uname -r 4.18.0-193.el8.x86_64 root@0a2ea514887e:/# 查看容器“Python3.11”的IP地址: root@0a2ea514887e:~# ifconfig bash: ifconfig: command not found root@0a2ea514887e:~# root@0a2ea514887e:~# apt-get update root@0a2ea514887e:~# apt-get -y install net-tools root@0a2ea514887e:~# 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 8538 bytes 17928024 (17.0 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8137 bytes 443152 (432.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@0a2ea514887e:~# 备注: 默认情况下,容器每次重启后,都会自行分配IP地址,且不允许自行指定或更改; 正常的话,容器“Python3.11”是可以Ping通宿主机: root@0a2ea514887e:~# ping 172.17.0.1 bash: ping: command not found root@0a2ea514887e:~# root@0a2ea514887e:~# apt-get -y install inetutils-ping root@0a2ea514887e:~# ping -c 4 172.17.0.1 PING 172.17.0.1 (172.17.0.1): 56 data bytes 64 bytes from 172.17.0.1: icmp_seq=0 ttl=64 time=0.108 ms 64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.103 ms 64 bytes from 172.17.0.1: icmp_seq=2 ttl=64 time=0.097 ms 64 bytes from 172.17.0.1: icmp_seq=3 ttl=64 time=0.127 ms --- 172.17.0.1 ping statistics --- 4 packets transmitted, 4 packets received, 0% packet loss round-trip min/avg/max/stddev = 0.097/0.109/0.127/0.000 ms root@0a2ea514887e:~# root@0a2ea514887e:/# ping -c 4 192.168.168.154 PING 192.168.168.154 (192.168.168.154): 56 data bytes 64 bytes from 192.168.168.154: icmp_seq=0 ttl=64 time=0.110 ms 64 bytes from 192.168.168.154: icmp_seq=1 ttl=64 time=0.398 ms 64 bytes from 192.168.168.154: icmp_seq=2 ttl=64 time=0.098 ms 64 bytes from 192.168.168.154: icmp_seq=3 ttl=64 time=0.082 ms --- 192.168.168.154 ping statistics --- 4 packets transmitted, 4 packets received, 0% packet loss round-trip min/avg/max/stddev = 0.082/0.172/0.398/0.131 ms root@0a2ea514887e:/# 正常的话,容器“Python3.11”是可以访问公网: root@0a2ea514887e:/# ping -c 5 blog.zhuohua.store PING blog.zhuohua.store (47.75.39.177): 56 data bytes 64 bytes from 47.75.39.177: icmp_seq=0 ttl=127 time=29.955 ms 64 bytes from 47.75.39.177: icmp_seq=1 ttl=127 time=20.810 ms 64 bytes from 47.75.39.177: icmp_seq=2 ttl=127 time=18.695 ms 64 bytes from 47.75.39.177: icmp_seq=3 ttl=127 time=20.560 ms 64 bytes from 47.75.39.177: icmp_seq=4 ttl=127 time=17.836 ms --- blog.zhuohua.store ping statistics --- 5 packets transmitted, 5 packets received, 0% packet loss round-trip min/avg/max/stddev = 17.836/21.571/29.955/4.339 ms root@0a2ea514887e:/# 容器“Python3.11”的硬盘分区情况:(与宿主机的硬盘分区是不同的) root@0a2ea514887e:/# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sr0 11:0 1 7.7G 0 rom nvme0n1 259:0 0 80G 0 disk ├─nvme0n1p1 259:1 0 200M 0 part └─nvme0n1p2 259:2 0 79.8G 0 part root@0a2ea514887e:/# root@0a2ea514887e:/# df -hTP Filesystem Type Size Used Avail Use% Mounted on overlay overlay 76G 3.2G 73G 5% / tmpfs tmpfs 64M 0 64M 0% /dev tmpfs tmpfs 901M 0 901M 0% /sys/fs/cgroup shm tmpfs 64M 0 64M 0% /dev/shm /dev/mapper/cl-root xfs 76G 3.2G 73G 5% /home/python_home tmpfs tmpfs 901M 0 901M 0% /proc/asound tmpfs tmpfs 901M 0 901M 0% /proc/acpi tmpfs tmpfs 901M 0 901M 0% /proc/scsi tmpfs tmpfs 901M 0 901M 0% /sys/firmware root@0a2ea514887e:/# ###### 容器“Python3.11”已经安装好Python3: root@0a2ea514887e:/# python3 Python 3.11.0 (main, Nov 15 2022, 19:58:01) [GCC 10.2.1 20210110] on linux Type "help", "copyright", "credits" or "license" for more information. >>> print('Welcome to zhuohua.') Welcome to zhuohua. >>> 退出当前的容器: root@0a2ea514887e:/# exit exit [root@centos8 ~]# 在宿主机查看正在运行的容器: [root@centos8 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0a2ea514887e python:3.11 "python3" 21 minutes ago Up 21 minutes Python3.11 [root@centos8 ~]# 停止容器“Python3.11”: [root@centos8 ~]# docker stop Python3.11 Python3.11 [root@centos8 ~]# 再次,在宿主机查看正在运行的容器: [root@centos8 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@centos8 ~]# 在宿主机查看所有的容器:(无论是否正在运行) [root@centos8 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0a2ea514887e python:3.11 "python3" 23 minutes ago Exited (137) About a minute ago Python3.11 [root@centos8 ~]# 启动容器“Python3.11”:(启动已经存在的容器,要用这个方式) [root@centos8 ~]# docker start Python3.11 Python3.11 [root@centos8 ~]# 再次,在宿主机查看所有的容器: [root@centos8 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0a2ea514887e python:3.11 "python3" 25 minutes ago Up 12 seconds Python3.11 [root@centos8 ~]# ### 容器“Python3.11”在宿主机的存放目录: [root@centos8 ~]# cd /var/lib/docker/ [root@centos8 docker]# ls buildkit containers image network overlay2 plugins runtimes swarm tmp trust volumes [root@centos8 docker]# [root@centos8 docker]# cd containers/ [root@centos8 containers]# ls 0a2ea514887e4221c7774ab06409743c03bf704a71d433eebdeab9a7b9410594 [root@centos8 containers]# [root@centos8 containers]# pwd /var/lib/docker/containers [root@centos8 containers]# [root@centos8 containers]# cd 0a2ea514887e4221c7774ab06409743c03bf704a71d433eebdeab9a7b9410594 [root@centos8 0a2ea514887e4221c7774ab06409743c03bf704a71d433eebdeab9a7b9410594]# ls 0a2ea514887e4221c7774ab06409743c03bf704a71d433eebdeab9a7b9410594-json.log hostconfig.json mounts checkpoints hostname resolv.conf config.v2.json hosts resolv.conf.hash [root@centos8 0a2ea514887e4221c7774ab06409743c03bf704a71d433eebdeab9a7b9410594]# 容器“Python3.11”的文件config.v2.json中的一些信息: 容器ID : "Config":{"Hostname":"0a2ea514887e", Python相关软件的版本: "PYTHON_VERSION=3.11.0","PYTHON_PIP_VERSION=22.3","PYTHON_SETUPTOOLS_VERSION=65.5.0", ###### 在宿主机的目录/opt/python_home里创建一个脚本“file_01.py”: [root@centos8 ~]# cd /opt/python_home [root@centos8 python_home]# [root@centos8 python_home]# pwd /opt/python_home [root@centos8 python_home]# cat file_01.py #coding=utf-8 print('Welcome to zhuohua.') [root@centos8 python_home]# 进入容器“Python3.11”: root@0a2ea514887e:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@0a2ea514887e:/# root@0a2ea514887e:/# cd home/ root@0a2ea514887e:/home# ls python_home root@0a2ea514887e:/home# cd python_home/ root@0a2ea514887e:/home/python_home# ls file_01.py root@0a2ea514887e:/home/python_home# cat file_01.py #coding=utf-8 print('Welcome to zhuohua.') root@0a2ea514887e:/home/python_home# 注释:因为在创建容器“Python3.11”的时候,把宿主机的目录/opt/python_home挂载到容器的目录/home/python_home,所以这两个目录里的数据是双向同步的。 在容器“Python3.11”中运行脚本“file_01.py”: root@0a2ea514887e:/home/python_home# cd root@0a2ea514887e:~# root@0a2ea514887e:~# pwd /root root@0a2ea514887e:~# python3 /home/python_home/file_01.py Welcome to zhuohua. root@0a2ea514887e:~# ###### 在容器“Python3.11”中安装第三方库(psutil): root@0a2ea514887e:~# pip3 freeze root@0a2ea514887e:~# root@0a2ea514887e:~# pip3 install psutil -i http://mirrors.aliyun.com/pypi/simple --trusted-host=mirrors.aliyun.com root@0a2ea514887e:~# pip3 freeze psutil==5.9.4 root@0a2ea514887e:~# 在容器“Python3.11”的目录/home/python_home里创建一个脚本“file_02.py”: root@0a2ea514887e:~# vim /home/python_home/file_02.py bash: vim: command not found root@0a2ea514887e:~# 在容器“Python3.11”中安装命令vim: root@0a2ea514887e:~# apt-get -y install vim 在容器“Python3.11”中查看创建好的脚本“file_02.py”: root@0a2ea514887e:~# cat /home/python_home/file_02.py #coding=utf-8 import psutil CPU_use_percent = psutil.cpu_percent(interval=2) print(CPU_use_percent) print(type(CPU_use_percent)) print(f"CPU使用率: {CPU_use_percent}%") print("-" * 10) Mem_total = psutil.virtual_memory().total Mem_total = round(Mem_total / 1024 / 1024,2) print(f"总内存量: {Mem_total}MB") Mem_used = psutil.virtual_memory().used Mem_used = round(Mem_used / 1024 / 1024,2) print(f"已使用内存量: {Mem_used}MB") print("-" * 10) Mem_use_percent = psutil.virtual_memory().percent print(Mem_use_percent) print(f"内存使用率: {Mem_use_percent}%") root@0a2ea514887e:~# 在容器“Python3.11”中运行脚本“file_02.py”: root@0a2ea514887e:~# find / -name "python3" /etc/python3 /usr/bin/python3 /usr/lib/python3 /usr/local/bin/python3 /usr/share/doc/python3 /usr/share/lintian/overrides/python3 /usr/share/python3 root@0a2ea514887e:~# root@0a2ea514887e:~# /usr/local/bin/python3 /home/python_home/file_02.py 1.5 CPU使用率: 1.5% ---------- 总内存量: 1800.62MB 已使用内存量: 434.42MB ---------- 34.8 内存使用率: 34.8% root@0a2ea514887e:~# 在宿主机中也可以看到脚本“file_02.py”: [root@centos8 python_home]# pwd /opt/python_home [root@centos8 python_home]# ls file_01.py file_02.py [root@centos8 python_home]# [root@centos8 python_home]# cat file_02.py #coding=utf-8 import psutil CPU_use_percent = psutil.cpu_percent(interval=2) print(CPU_use_percent) print(type(CPU_use_percent)) print(f"CPU使用率: {CPU_use_percent}%") print("-" * 10) Mem_total = psutil.virtual_memory().total Mem_total = round(Mem_total / 1024 / 1024,2) print(f"总内存量: {Mem_total}MB") Mem_used = psutil.virtual_memory().used Mem_used = round(Mem_used / 1024 / 1024,2) print(f"已使用内存量: {Mem_used}MB") print("-" * 10) Mem_use_percent = psutil.virtual_memory().percent print(Mem_use_percent) print(f"内存使用率: {Mem_use_percent}%") [root@centos8 python_home]# ############ ############ 在宿主机移动容器“Python3.11”: 先停止容器“Python3.11”: [root@centos8 ~]# docker stop Python3.11 Python3.11 [root@centos8 ~]# 要把容器“Python3.11”创建成一个新镜像: [root@centos8 ~]# docker commit Python3.11 newimage_python3 sha256:4ef2acde4a35fd4f6f435f3be7d1c1af7b7eac90f4b6868cb79fa838d02e3ed3 [root@centos8 ~]# 新镜像“newimage_python3:latest”创建成功: [root@centos8 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE newimage_python3 latest 4ef2acde4a35 16 seconds ago 1GB python 3.11 ee4e7a0f1c35 2 weeks ago 932MB [root@centos8 ~]# 把新镜像“newimage_python3:latest”另外备份好: [root@centos8 ~]# docker save newimage_python3:latest > /tmp/newimage_python3.tar [root@centos8 ~]# [root@centos8 ~]# du -sh /tmp/newimage_python3.tar 980M /tmp/newimage_python3.tar [root@centos8 ~]# 备注:新镜像可以放到其他宿主机上使用了,新宿主机无需有原始镜像“python:3.11” 在宿主机删除镜像“newimage_python3:latest”: [root@centos8 ~]# docker rmi -f newimage_python3:latest Untagged: newimage_python3:latest Deleted: sha256:4ef2acde4a35fd4f6f435f3be7d1c1af7b7eac90f4b6868cb79fa838d02e3ed3 Deleted: sha256:90b802c6cda11fe25cde6be460afc4afdd272cad662ea4a3d90745b3a469da98 [root@centos8 ~]# 新镜像“newimage_python3:latest”删除成功: [root@centos8 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE python 3.11 ee4e7a0f1c35 2 weeks ago 932MB [root@centos8 ~]# 从备份文件“/tmp/newimage_python3.tar”还原镜像“newimage_python3:latest”: [root@centos8 ~]# docker load < /tmp/newimage_python3.tar 5354e9f5b03b: Loading layer [==================================================>] 72.78MB/72.78MB Loaded image: newimage_python3:latest [root@centos8 ~]# 镜像“newimage_python3:latest”还原成功: [root@centos8 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE newimage_python3 latest 4ef2acde4a35 5 minutes ago 1GB python 3.11 ee4e7a0f1c35 2 weeks ago 932MB [root@centos8 ~]# 修改镜像“newimage_python3:latest”的版本号: [root@centos8 ~]# docker tag newimage_python3:latest newimage_python3:8.11 [root@centos8 ~]# 版本号修改成功: [root@centos8 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE newimage_python3 8.11 4ef2acde4a35 6 minutes ago 1GB newimage_python3 latest 4ef2acde4a35 6 minutes ago 1GB python 3.11 ee4e7a0f1c35 2 weeks ago 932MB [root@centos8 ~]# 再次,删除镜像“newimage_python3:latest”: [root@centos8 ~]# docker rmi -f newimage_python3:latest Untagged: newimage_python3:latest [root@centos8 ~]# 再次,查看宿主机的所有镜像: [root@centos8 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE newimage_python3 8.11 4ef2acde4a35 7 minutes ago 1GB python 3.11 ee4e7a0f1c35 2 weeks ago 932MB [root@centos8 ~]# 在宿主机查看正在运行的容器: [root@centos8 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0a2ea514887e python:3.11 "python3" 18 hours ago Up About a minute Python3.11 [root@centos8 ~]# 使用镜像“newimage_python3:8.11”创建并启动容器“Python811”: [root@centos8 ~]# docker run -itd --name Python811 -v /opt/python_home:/home/python_home newimage_python3:8.11 f7c959f81303cec97bd6af948623afb4959acf68e6ddb0be4d9e63e416aa9c2b [root@centos8 ~]# 注释: -itd : 以交互模式情况下后台运行。 --name : 指定容器名称。 -v 挂载目录 : 这里是把宿主机的目录/opt/python_home挂载到容器的目录/home/python_home;假如宿主机的目录已经存在,也不会覆盖原有数据;可以把宿主机的一个目录同时挂载到多个容器的目录。 newimage_python3:8.11 : 镜像名称:版本号 再次,在宿主机查看正在运行的容器: [root@centos8 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f7c959f81303 newimage_python3:8.11 "python3" About a minute ago Up About a minute Python811 0a2ea514887e python:3.11 "python3" 18 hours ago Up 3 minutes Python3.11 [root@centos8 ~]# ### 进入容器“Python811”: [root@centos8 ~]# docker exec -it Python811 /bin/bash root@f7c959f81303:/# 查看容器“Python811”的IP地址: root@f7c959f81303:/# ifconfig 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 11 bytes 866 (866.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@f7c959f81303:/# 备注: 默认情况下,容器每次重启后,都会自行分配IP地址,且IP地址是不会重复的; 正常的话,容器“Python811”是可以Ping通宿主机: root@f7c959f81303:/# ping -c 4 172.17.0.1 PING 172.17.0.1 (172.17.0.1): 56 data bytes 64 bytes from 172.17.0.1: icmp_seq=0 ttl=64 time=0.241 ms 64 bytes from 172.17.0.1: icmp_seq=1 ttl=64 time=0.089 ms 64 bytes from 172.17.0.1: icmp_seq=2 ttl=64 time=0.082 ms 64 bytes from 172.17.0.1: icmp_seq=3 ttl=64 time=0.091 ms --- 172.17.0.1 ping statistics --- 4 packets transmitted, 4 packets received, 0% packet loss round-trip min/avg/max/stddev = 0.082/0.126/0.241/0.067 ms root@f7c959f81303:/# root@f7c959f81303:/# ping -c 4 192.168.168.154 PING 192.168.168.154 (192.168.168.154): 56 data bytes 64 bytes from 192.168.168.154: icmp_seq=0 ttl=64 time=0.133 ms 64 bytes from 192.168.168.154: icmp_seq=1 ttl=64 time=0.236 ms 64 bytes from 192.168.168.154: icmp_seq=2 ttl=64 time=0.108 ms 64 bytes from 192.168.168.154: icmp_seq=3 ttl=64 time=0.161 ms --- 192.168.168.154 ping statistics --- 4 packets transmitted, 4 packets received, 0% packet loss round-trip min/avg/max/stddev = 0.108/0.160/0.236/0.048 ms root@f7c959f81303:/# 正常的话,容器“Python811”是可以Ping通宿容器“Python3.11”: root@f7c959f81303:/# ping -c 4 172.17.0.2 PING 172.17.0.2 (172.17.0.2): 56 data bytes 64 bytes from 172.17.0.2: icmp_seq=0 ttl=64 time=0.100 ms 64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.154 ms 64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.099 ms 64 bytes from 172.17.0.2: icmp_seq=3 ttl=64 time=0.107 ms --- 172.17.0.2 ping statistics --- 4 packets transmitted, 4 packets received, 0% packet loss round-trip min/avg/max/stddev = 0.099/0.115/0.154/0.023 ms root@f7c959f81303:/# ### 查看容器“Python811”的目录/home/python_home: root@f7c959f81303:/# cd /home/python_home root@f7c959f81303:/home/python_home# ls file_01.py file_02.py root@f7c959f81303:/home/python_home# 在容器“Python811”中运行脚本“file_02.py”: root@f7c959f81303:/home/python_home# python3 file_02.py 0.5 CPU使用率: 0.5% ---------- 总内存量: 1800.62MB 已使用内存量: 355.64MB ---------- 29.0 内存使用率: 29.0% root@f7c959f81303:/home/python_home# 注释:因为在创建容器“Python811”的时候,也把宿主机的目录/opt/python_home挂载到自己的目录/home/python_home,所以宿主机的目录/opt/python_home、容器“Python3.11”的目录/home/python_home、容器“Python811”的目录/home/python_home的数据是共享的。 ### 容器“Python811”在宿主机的存放目录: [root@centos8 ~]# cd /var/lib/docker/containers/ [root@centos8 containers]# ls 0a2ea514887e4221c7774ab06409743c03bf704a71d433eebdeab9a7b9410594 f7c959f81303cec97bd6af948623afb4959acf68e6ddb0be4d9e63e416aa9c2b [root@centos8 containers]# [root@centos8 containers]# cd f7c959f81303cec97bd6af948623afb4959acf68e6ddb0be4d9e63e416aa9c2b/ [root@centos8 f7c959f81303cec97bd6af948623afb4959acf68e6ddb0be4d9e63e416aa9c2b]# ls checkpoints hostconfig.json mounts config.v2.json hostname resolv.conf f7c959f81303cec97bd6af948623afb4959acf68e6ddb0be4d9e63e416aa9c2b-json.log hosts resolv.conf.hash [root@centos8 f7c959f81303cec97bd6af948623afb4959acf68e6ddb0be4d9e63e416aa9c2b]# 容器“Python811”的文件config.v2.json中的一些信息: 容器ID : "Config":{"Hostname":"f7c959f81303", 挂载目录的信息:( 把宿主机的目录/opt/python_home挂载到容器的目录/home/python_home ) "MountPoints":{"/home/python_home":{"Source":"/opt/python_home","Destination":"/home/python_home","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"/opt/python_home","Target":"/home/python_home"}, 以下是重新把宿主机的目录/home挂载到容器“Python811”的目录/home/python_home: [root@centos8 ~]# docker stop Python811 [root@centos8 ~]# systemctl stop docker "MountPoints":{"/home/python_home":{"Source":"/home","Destination":"/home/python_home","RW":true,"Name":"","Driver":"","Type":"bind","Propagation":"rprivate","Spec":{"Type":"bind","Source":"/home","Target":"/home/python_home"}, [root@centos8 ~]# systemctl start docker [root@centos8 ~]# docker start Python811 确认已经把宿主机的目录/home挂载到容器的目录/home/python_home: 查看宿主机的目录/home: [root@centos8 ~]# du -ah /home/ 22M /home/Python-3.6.8.tgz 22M /home/ [root@centos8 ~]# 重新进入容器“Python811”: [root@centos8 ~]# docker exec -it Python811 /bin/bash root@f7c959f81303:/# 查看容器“Python811”的目录/home/python_home: root@f7c959f81303:~# du -ah /home/python_home/ 22M /home/python_home/Python-3.6.8.tgz 22M /home/python_home/ root@f7c959f81303:~# ###### 在宿主机查看所有的容器: [root@centos8 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f7c959f81303 newimage_python3:8.11 "python3" About an hour ago Up 8 minutes Python811 0a2ea514887e python:3.11 "python3" 20 hours ago Up 12 seconds Python3.11 [root@centos8 ~]# 停止容器“Python811”:(可以使用容器名称,也可以使用容器ID) [root@centos8 ~]# docker stop f7c959f81303 f7c959f81303 [root@centos8 ~]# [root@centos8 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES f7c959f81303 newimage_python3:8.11 "python3" About an hour ago Exited (137) 29 seconds ago Python811 0a2ea514887e python:3.11 "python3" 20 hours ago Up 2 minutes Python3.11 [root@centos8 ~]# 删除容器“Python811”:(可以使用容器名称,也可以使用容器ID;要先停止对应容器) [root@centos8 ~]# docker rm Python811 Python811 [root@centos8 ~]# [root@centos8 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0a2ea514887e python:3.11 "python3" 20 hours ago Up 4 minutes Python3.11 [root@centos8 ~]# 会连对应容器的存放目录也删除掉: [root@centos8 ~]# cd /var/lib/docker/containers/ [root@centos8 containers]# ls 0a2ea514887e4221c7774ab06409743c03bf704a71d433eebdeab9a7b9410594 [root@centos8 containers]# 相关文章: CentOS8安装Python3 CentOS8使用psutil+新浪邮箱使用465端口发送邮件 CentOS8_在Docker中安装Flask CentOS8_在Docker中使用Nginx的反向代理 CentOS8_在Docker中安装MySQL CentOS8_在Docker中安装MariaDB CentOS8_在Docker中安装Tomcat+MariaDB CentOS8_在Docker中限制容器可用的CPU个数和内存量




欢迎光临 blog.zhuohua.store (http://blog.zhuohua.store/) Powered by Discuz! 7.2