本章主要介绍使用 podman 管理容器
[root@node01 ~]# yum -y install podman
[root@node01 ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@node01 ~]#
[root@node01 ~]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@node01 ~]#
[root@node01 ~]# vim /etc/containers/registries.conf
unqualified-search-registries = ["registry.fedoraproject.org", "registry.access.redhat.com", "registry.centos.org", "docker.io"]
[[registry]]
prefix = "docker.io"
location = "6kivj4sd.mirror.aliyuncs.com"
[root@node01 ~]# podman pull docker.io/nginx
Trying to pull docker.io/library/nginx:latest...
Getting image source signatures
Copying blob a0bcbecc962e done
Copying blob b4df32aa5a72 done
Copying blob 186b1aaa4aa6 done
Copying blob a2abf6c4d29d done
Copying blob 589b7251471a done
Copying blob a9edb18cadd1 done
Copying config 605c77e624 done
Writing manifest to image destination
Storing signatures
605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
[root@node01 ~]#
[root@node01 ~]# podman pull hub.c.163.com/library/conetos
Trying to pull hub.c.163.com/library/conetos:latest...
Error: initializing source docker://hub.c.163.com/library/conetos:latest: reading manifest latest in hub.c.163.com/library/conetos: manifest unknown: manifest unknown
[root@node01 ~]# podman pull hub.c.163.com/library/centos
Trying to pull hub.c.163.com/library/centos:latest...
Getting image source signatures
Copying blob a3ed95caeb02 done
Copying blob 2409c3878ba1 done
Copying blob a3ed95caeb02 done
Writing manifest to image destination
Storing signatures
328edcd84f1bbf868bc88e4ae37afe421ef19be71890f59b4b2d8ba48414b84d
[root@node01 ~]#
前面讲了要想创建容器必须有镜像,本节主要讲解镜像的管理。
服务器IP:端口 /分类 /镜像名 :tag
[root@node01 ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/nginx latest 605c77e624dd 23 months ago 146 MB
hub.c.163.com/library/centos latest 328edcd84f1b 6 years ago 200 MB
[root@node01 ~]#
如果想给本地已经存在的镜像起一个新的名称,可以用tag来做,语法如下。
podman tag 旧的镜像名 新的镜像名
[root@node01 ~]# podman tag docker.io/library/nginx 192.168.182.200/rhce/nginx:v2
[root@node01 ~]#
[root@node01 ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/nginx latest 605c77e624dd 23 months ago 146 MB
192.168.182.200/rhce/nginx v2 605c77e624dd 23 months ago 146 MB
hub.c.163.com/library/centos latest 328edcd84f1b 6 years ago 200 MB
[root@node01 ~]#
podman rmi 镜像名:tag
[root@node01 ~]# podman rmi docker.io/library/nginx:latest
Untagged: docker.io/library/nginx:latest
[root@node01 ~]#
[root@node01 ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.182.200/rhce/nginx v2 605c77e624dd 23 months ago 146 MB
hub.c.163.com/library/centos latest 328edcd84f1b 6 years ago 200 MB
[root@node01 ~]#
[root@node01 ~]# podman rmi 192.168.182.200/rhce/nginx:v2
Untagged: 192.168.182.200/rhce/nginx:v2
Deleted: 605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
[root@node01 ~]#
podman history 镜像名
[root@node01 ~]# podman history hub.c.163.com/library/centos
ID CREATED CREATED BY SIZE COMMENT
328edcd84f1b 6 years ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B
6 years ago /bin/sh -c #(nop) LABEL name=CentOS Base ... 0 B
6 years ago /bin/sh -c #(nop) ADD file:63492ba809361c5... 200 MB
[root@node01 ~]#
podman save 镜像名 > file.tar
[root@node01 ~]# podman save docker.io/library/nginx > nginx.tar
[root@node01 ~]# podman rmi docker.io/library/nginx
Untagged: docker.io/library/nginx:latest
Deleted: 605c77e624ddb75e6110f997c58876baa13f8754486b461117934b24a9dc3a85
[root@node01 ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
hub.c.163.com/library/centos latest 328edcd84f1b 6 years ago 200 MB
[root@node01 ~]#
podman load -i nginx.tar
[root@node01 ~]# podman load -i nginx.tar
Getting image source signatures
Copying blob d874fd2bc83b done
Copying blob e379e8aedd4d done
Copying blob 2edcec3590a4 done
Copying blob f1db227348d0 done
Copying blob b8d6e692a25e done
Copying blob 32ce5f6a5106 done
Copying config 605c77e624 done
Writing manifest to image destination
Storing signatures
Loaded image(s): docker.io/library/nginx:latest
[root@node01 ~]#
[root@node01 ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/nginx latest 605c77e624dd 23 months ago 146 MB
hub.c.163.com/library/centos latest 328edcd84f1b 6 years ago 200 MB
[root@node01 ~]#
查看当前有多少正在运行的容器,命令如下。
[root@node01 ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@node01 ~]#
[root@node01 ~]# podman run hub.c.163.com/library/centos
[root@node01 ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@node01 ~]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d9947925c130 hub.c.163.com/library/centos:latest /bin/bash 26 seconds ago Exited (0) 27 seconds ago cool_gould
[root@node01 ~]#
podman rm 容器ID/容器名
podman rm -f 容器ID/容器名
[root@node01 ~]# podman rm d9947925c130
d9947925c130a919f51cfaeea4e9604a021eb8f59c320d65f58a3aae1023878f
[root@node01 ~]#
[root@node01 ~]# podman run -it hub.c.163.com/library/centos
[root@87c2c144e62a /]# exit
exit
[root@node01 ~]#
但是一旦退出容器,容器就不再运行了。
[root@node01 ~]# podman ps -q
[root@node01 ~]# podman ps -a -q
87c2c144e62a
[root@node01 ~]#
[root@node01 ~]# podman rm -f 87c2c144e62a
87c2c144e62a700904621c50ca15bf8eb60694231d8f0fe2cfd4ba337a990c05
[root@node01 ~]#
[root@node01 ~]# podman run -dit hub.c.163.com/library/centos
fe2206350aad5b3e9c67fe4badf952a3744ac59f02963e5436314475b671fddf
[root@node01 ~]#
[root@node01 ~]# podman ps -a -q
fe2206350aad
[root@node01 ~]# podman attach fe2206350aad
[root@fe2206350aad /]#
[root@node01 ~]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
fe2206350aad hub.c.163.com/library/centos:latest /bin/bash About a minute ago Exited (0) 11 seconds ago bold_mendeleev
[root@node01 ~]#
[root@node01 ~]# podman rm fe2206350aad
fe2206350aad5b3e9c67fe4badf952a3744ac59f02963e5436314475b671fddf
[root@node01 ~]#
[root@node01 ~]# podman run -dit --restart=always hub.c.163.com/library/centos
74d1b159e27c38bc990ad06ce493329211858a2a9319661af17c217eeb9a7776
[root@node01 ~]#
[root@node01 ~]# podman ps -q
74d1b159e27c
[root@node01 ~]# podman attach 74d1b159e27c
[root@74d1b159e27c /]# exit
exit
[root@node01 ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74d1b159e27c hub.c.163.com/library/centos:latest /bin/bash About a minute ago Up 51 seconds ago mystifying_chatterjee
[root@node01 ~]#
[root@node01 ~]# podman ps -q
74d1b159e27c
[root@node01 ~]# podman rm -f 74d1b159e27c
74d1b159e27c38bc990ad06ce493329211858a2a9319661af17c217eeb9a7776
[root@node01 ~]#
[root@node01 ~]# podman run -dit --restart=always --name=c1 hub.c.163.com/library/centos
479eb802ea13146c4ca20b6fbc28ac59f0ca0ed6f86ab30a1b05eb4c8f2edd68
[root@node01 ~]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
479eb802ea13 hub.c.163.com/library/centos:latest /bin/bash 18 seconds ago Up 18 seconds ago c1
[root@node01 ~]#
[root@node01 ~]# podman attach c1
[root@479eb802ea13 /]# exit
exit
[root@node01 ~]#
[root@node01 ~]# podman rm -f c1
479eb802ea13146c4ca20b6fbc28ac59f0ca0ed6f86ab30a1b05eb4c8f2edd68
[root@node01 ~]#
[root@node01 ~]# podman run -it --name=c1 --rm hub.c.163.com/library/centos
[root@d031b293f780 /]# exit
exit
[root@node01 ~]#
创建容器时加了 --rm选项,退出容器之后容器会被自动删除
[root@node01 ~]# podman ps -a -q
[root@node01 ~]#
[root@node01 ~]# podman run -it --name=c1 --rm hub.c.163.com/library/centos sh
sh-4.2# exit
exit
[root@node01 ~]#
[root@node01 ~]# podman run -it --name=c1 --rm -e aa=123 -e bb=456 hub.c.163.com/library/centos
[root@3d3922bee9f6 /]# echo $aa
123
[root@3d3922bee9f6 /]# echo $bb
456
[root@3d3922bee9f6 /]# exit
exit
[root@node01 ~]#
[root@node01 ~]# podman run -d --name=web --restart=always -p 80 docker.io/library/nginx
6c60809ed5e4a1585037f098d008406610e2b6ec4a9890635e285f76be939746
[root@node01 ~]#
[root@node01 ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c60809ed5e4 docker.io/library/nginx:latest nginx -g daemon o... 29 seconds ago Up 29 seconds ago 0.0.0.0:39281->80/tcp web
[root@node01 ~]#
[root@node01 ~]# firewall-cmd --add-port=39281/tcp
success
[root@node01 ~]#
删除此容器,命令如下。
[root@node01 ~]# podman rm -f web
6c60809ed5e4a1585037f098d008406610e2b6ec4a9890635e285f76be939746
[root@node01 ~]#
[root@node01 ~]# podman run -d --name=web --restart=always -p 8080:80 docker.io/library/nginx
38dc3d3248412873d0c568737255f5f60a307ba44d6ecc115b96cbb896db67ae
[root@node01 ~]#
删除此容器,命令如下。
[root@node01 ~]# podman rm -f web
38dc3d3248412873d0c568737255f5f60a307ba44d6ecc115b96cbb896db67ae
[root@node01 ~]#