podman的常用基本操作
podman常用操作
什麼是Podman?
Podan 是一個開源的容器執行時專案,可在大多數 Linux 平臺上使用。Podman 提供與 Docker 非常相似的功能。正如前面提到的那樣,它不需要在你的系統上執行任何守護程序,並且它也可以在沒有 root 許可權的情況下執行。
Podman 可以管理和執行任何符合 OCI(Open Container Initiative)規範的容器和容器映象。Podman 提供了一個與 Docker 相容的命令列前端來管理 Docker 映象。
Podman的使用與docker有什麼區別?
podman的定位也是與docker相容,因此在使用上面儘量靠近docker。在使用方面,可以分成兩個方面來說,一是系統構建者的角度,二是使用者的角度。
在系統構建者方面,用podman的預設軟體,與docker的區別不大,只是在程序模型、程序關係方面有所區別。如果習慣了docker幾個關聯程序的除錯方法,在podman中則需要適應。可以通過pstree命令檢視程序的樹狀結構。總體來看,podman比docker要簡單。由於podman比docker少了一層daemon,因此重啟的機制也就不同了。
在使用者方面,podman與docker的命令基本相容,都包括容器執行時(run/start/kill/ps/inspect),本地映象(images/rmi/build)、映象倉庫(login/pull/push)等幾個方面。因此podman的命令列工具與docker類似,比如構建映象、啟停容器等。甚至可以通過alias
docker=podman可以進行替換。因此,即便使用了podman,仍然可以使用http://docker.io作為映象倉庫,這也是相容性最關鍵的部分。
Podman常用命令
容器
podman run 建立並啟動容器 podman start 啟動容器 podman ps 檢視容器 podman stop 終止容器 podman restart 重啟容器 podman attach 進入容器 podman exec 進入容器 podman export 匯出容器 podman import 匯入容器快照 podman rm 刪除容器 podman logs 檢視日誌
映象
podman search 檢索映象
podman pull 獲取映象
podman images 列出映象
podman image Is 列出映象
podman rmi 刪除映象
podman image rm 刪除映象
podman save 匯出映象
podman load 匯入映象
podmanfile 定製映象(三個)
podman build 構建映象
podman run 執行映象
podmanfile 常用指令(四個)
COPY 複製檔案
ADD 高階複製
CMD 容器啟動命令
ENV 環境變數
EXPOSE 暴露埠
部署 Podman
//安裝podman
[root@localhost ~]# yum -y install podman
Podman 加速器
vim /etc/containers/registries.conf
#unqualified-search-registries = ["registry.fedoraproject.org", "registry.access.redhat.com", "registry.centos.org", "docker.io"]
unqualified-search-registries = ["docker.io"]
[[registry]]
prefix = "docker.io"
location = "ay24c8ru.mirror.aliyuncs.com"
建立映象容器
[root@localhost containers]# podman run -d --name web -p 8080:80 httpd
[root@localhost containers]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d92bc2ac42cd docker.io/library/httpd:latest httpd-foreground 11 seconds ago Up 10 seconds ago 0.0.0.0:8080->80/tcp web
執行容器
[root@localhost ~]# podman inspect -l | grep IPAddress\":
"IPAddress": "192.168.142.132",
"IPAddress": "192.168.142.132",
[root@localhost containers]# curl 192.168.142.132
<html><body><h1>It works!</h1></body></html>
//檢視容器的日誌
[root@localhost ~]# podman logs -l
10.88.0.1 - - [09/May/2022:06:30:11 +0000] "GET / HTTP/1.1" 200 45
192.168.80.1 - - [09/May/2022:06:31:18 +0000] "GET / HTTP/1.1" 200 45
192.168.80.1 - - [09/May/2022:06:31:18 +0000] "GET /favicon.ico HTTP/1.1" 404 196
192.168.80.1 - - [09/May/2022:06:32:10 +0000] "-" 408 -
192.168.80.1 - - [09/May/2022:06:35:56 +0000] "GET / HTTP/1.1" 304 -
192.168.80.1 - - [09/May/2022:06:36:48 +0000] "-" 408 -
//刪除容器
[root@localhost ~]# podman rm -f -l
d92bc2ac42cdb59528e6dd406d9c7036edbf90f05c6d8c37f7ffd6e85522d43c
[root@localhost ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
檢視一個執行容器中的程序資源使用情況
[root@localhost ~]# podman run -d httpd
10906a9bc0cbb97fac29dc428f7a3348f7cc88a528cbda8f041807b3583c4d22
[root@localhost ~]# podman top -l
USER PID PPID %CPU ELAPSED TTY TIME COMMAND
root 1 0 0.000 18.884993458s ? 0s httpd -DFOREGROUND
www-data 7 1 0.000 18.885149751s ? 0s httpd -DFOREGROUND
www-data 8 1 0.000 18.885214332s ? 0s httpd -DFOREGROUND
www-data 9 1 0.000 18.885253876s ? 0s httpd -DFOREGROUND
停止的容器
[root@localhost ~]# podman stop --latest
10906a9bc0cbb97fac29dc428f7a3348f7cc88a528cbda8f041807b3583c4d22
[root@localhost ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
映象上傳
[root@localhost ~]# mkdir test
[root@localhost ~]# cd test/
[root@localhost test]# vim Podmanfile
[root@localhost test]# podman build -f Podmanfile -t test:v0.1 .
[root@localhost test]# podman run -it test:v0.1 /bin/sh
/ # echo $a
10
//如果指定名稱的話加上-f
[root@localhost ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/test v0.1 93401fffb1a9 3 minutes ago 1.46 MB
docker.io/library/busybox latest beae173ccac6 4 months ago 1.46 MB
docker.io/library/httpd latest dabbfbe0c57b 4 months ago 148 MB
//前面是localhost必須改名上傳映象
[root@localhost ~]# podman tag localhost/test:v0.1 docker.io/mingzi540/busybox:xiaoxin
[root@localhost ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
localhost/test v0.1 93401fffb1a9 6 minutes ago 1.46 MB
docker.io/mingzi540/busybox xiaoxin 93401fffb1a9 6 minutes ago 1.46 MB
docker.io/library/busybox latest beae173ccac6 4 months ago 1.46 MB
docker.io/library/httpd latest dabbfbe0c57b 4 months ago 148 MB
[root@localhost ~]# podman login docker.io
Username: mingzi540
Password:
Login Succeeded!
//登入的時候要加上docker.io選擇docker官方倉庫
[root@localhost ~]# podman push docker.io/mingzi540/busybox:xiaoxin
Getting image source signatures
Copying blob 01fd6df81c8e done
Copying config 93401fffb1 done
Writing manifest to image destination
Storing signatures
配置別名
如果習慣了使用 Docker 命令,可以直接給 Podman 配置一個別名來實現無縫轉移。你只需要在 .bashrc 下加入以下行內容即可
[root@localhost ~]# alias docker="podman"
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost ~]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
使用者操作
在允許沒有root特權的使用者執行Podman之前,管理員必須安裝或構建Podman並完成以下配置。
cgroup V2Linux核心功能允許使用者限制普通使用者容器可以使用的資源,如果使用cgroupV2啟用了執行Podman的Linux發行版,則可能需要更改預設的OCI執行時。某些較舊的版本runc不適用於cgroupV2,必須切換到備用OCI執行時crun。
[root@localhost ~]# yum -y install crun //centos8系統自帶
[root@localhost ~]# cd /usr/share/containers/
[root@localhost containers]# ls
containers.conf mounts.conf seccomp.json selinux
[root@localhost containers]# vim containers.conf
runtime = "crun"
#runtime = "runc"
//取消註釋並將runc改為crun
[root@localhost ~]# podman inspect -l|grep -i runtime
"OCIRuntime": "crun",
"--runtime",
"Runtime": "oci",
"CpuRealtimeRuntime": 0,
安裝slirp4netns和fuse-overlayfs
在普通使用者環境中使用Podman時,建議使用fuse-overlayfs而不是VFS檔案系統,至少需要版本0.7.6。現在新版本預設。
[root@localhost ~]# yum -y install slirp4netns
[root@localhost ~]# yum -y install fuse-overlayfs
[root@localhost ~]# vi /etc/containers/storage.conf
77 mount_program = "/usr/bin/fuse-overlayfs" //取消註釋
/ etc / subuid和/ etc / subgid配置
Podman要求執行它的使用者在/ etc / subuid和/ etc / subgid檔案中列出一系列UID,shadow-utils或newuid包提供這些檔案
[root@localhost ~]# yum -y install shadow-utils
可以在/ etc / subuid和/ etc / subgid檢視,每個使用者的值必須唯一且沒有任何重疊。
[root@localhost ~]# useradd xiaoxin
[root@localhost ~]# useradd xiaoying
[root@localhost ~]# cat /etc/subuid
xiaoxin:100000:65536
xiaoying:165536:65536
// 啟動非特權ping
[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ping_group_range=0 200000
[root@localhost ~]# sysctl -p
net.ipv4.ping_group_range = 0 200000
檔案的格式是 USERNAME:UID:RANGE
- 中/etc/passwd或輸出中列出的使用者名稱getpwent。
- 為使用者分配的初始 UID。
- 為使用者分配的 UID 範圍的大小。
該usermod程式可用於為使用者分配 UID 和 GID,而不是直接更新檔案。
root@localhost ~]# usermod --add-subuids 200000-201000 --add-subgids 200000-201000 hh
grep hh /etc/subuid /etc/subgid
/etc/subuid:hh:200000:1001
/etc/subgid:hh:200000:1001
使用者配置檔案
三個主要的配置檔案是container.conf、storage.conf
和registries.conf
。使用者可以根據需要修改這些檔案。
- container.conf
// 使用者配置檔案
[root@localhost ~]# cat /usr/share/containers/containers.conf
[root@localhost ~]# cat /etc/containers/containers.conf
[root@localhost ~]# cat ~/.config/containers/containers.conf //優先順序最高
如果它們以該順序存在。每個檔案都可以覆蓋特定欄位的前一個檔案。
- 配置storage.conf檔案
1./etc/containers/storage.conf
2.$HOME/.config/containers/storage.conf
//在普通使用者中/etc/containers/storage.conf的一些欄位將被忽略
[root@localhost ~]# vi /etc/containers/storage.conf
[storage]
# Default Storage Driver, Must be set for proper operation.
driver = "overlay" #此處改為overlay
.......
mount_program = "/usr/bin/fuse-overlayfs" #取消註釋
[root@localhost ~]# sysctl user.max_user_namespaces=15000 #如果版本為8以下,則需要做以下操作:
[root@localhost containers]# sysctl -p
net.ipv4.ping_group_range = 0 200000
user.max_user_namespaces = 15000
//在普通使用者中這些欄位預設
graphroot="$HOME/.local/share/containers/storage"
runroot="$XDG_RUNTIME_DIR/containers"
- registries.conf
配置按此順序讀入,這些檔案不是預設建立的,可以從/usr/share/containers或複製檔案/etc/containers並進行修改。
1./etc/containers/registries.conf
2./etc/containers/registries.d/*
3.HOME/.config/containers/registries.conf
授權檔案
此檔案裡面寫了docker賬號的密碼,以加密方式顯示
[root@localhost ~]# podman login
Username: 1314444
Password:
Login Succeeded!
[root@localhost ~]# cat /run/user/0/containers/auth.json
{
"auths": {
"registry.fedoraproject.org": {
"auth": "MTMxNDQ0NDpIMjAxNy0xOA=="
}
}
}
普通使用者是無法看見root使用者的映象的
[www@localhost ~]$ podman pull busybox
Resolved "busybox" as an alias (/etc/containers/registries.conf.d/000-shortnames.conf)
Trying to pull docker.io/library/busybox:latest...
Getting image source signatures
Copying blob 5cc84ad355aa done
Copying config beae173cca done
Writing manifest to image destination
Storing signatures
beae173ccac6ad749f76713cf4440fe3d21d1043fe616dfbe30775815d1d0f6a
[www@localhost ~]$ podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/busybox latest beae173ccac6 4 months ago 1.46 MB
[www@localhost ~]$ podman run -it --rm busybox /bin/sh
/ #
//xiaoying使用者建立映象容器
[root@localhost ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3014a85ad421 docker.io/library/httpd:latest httpd-foreground 32 minutes ago Up 32 minutes ago great_mahavira
//切換到root使用者查不出xioaying使用者建立的容器
卷
容器與root使用者一起執行,則root容器中的使用者實際上就是主機上的使用者。
UID GID是在/etc/subuid和/etc/subgid等中使用者對映中指定的第一個UID GID。
如果普通使用者的身份從主機目錄掛載到容器中,並在該目錄中以根使用者身份建立檔案,則會看到它實際上是你的使用者在主機上擁有的。
[www@localhost ~]$ podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[www@localhost ~]$ ls
[www@localhost ~]$ mkdir data
[www@localhost ~]$ ls
data
[www@localhost ~]$ ll
total 0
drwxrwxr-x. 2 xiaoying xiaoying 6 May 9 17:55 data
[www@localhost ~]$ pwd
/home/xiaoying
[www@localhost ~]$ podman run -it --rm -v "$(pwd)"/data:/data:Z busybox /bin/sh
/ # ls
bin data dev etc home proc root run sys tmp usr var
/ # cd data/
/data # ls
/data #
//加上-Z就可以有許可權檢視
[www@localhost ~]$ echo 'hello world' > data/abc
//容器外建立檔案內容
/data # cat abc
hello world
//去容器裡檢視
//普通使用者可以對映>= 1024的埠
[www@localhost ~]$ podman run -d --name web1 -p 80:80 httpd
eaca84d0e2cc21634d5b28e81f5fe25efbbeeb89c74e1ed05fd0645a8d1c8236
[www@localhost ~]$ podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eaca84d0e2cc docker.io/library/httpd:latest httpd-foreground 2 minutes ago Up 2 minutes ago 0.0.0.0:80->80/tcp web1
配置echo ‘net.ipv4.ip_unprivileged_port_start=80’ >> /etc/sysctl.conf
後可以對映大於等於80的埠
[www@localhost ~]$ podman run -d -p 1024:80 httpd
[root@localhost ~]# echo 'net.ipv4.ip_unprivileged_port_start=80' >> /etc/sysctl.conf
[root@localhost ~]# sysctl -p
net.ipv4.ping_group_range = 0 200000
user.max_user_namespaces = 15000
net.ipv4.ip_unprivileged_port_start = 80
[www@localhost ~]$ podman run -d -p 80:80 httpd
4c327d55182ef362547f7baa87449822d98a95013442aa9e107c577250e8e460
[xiaoxin@localhost ~]$ ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 *:1024 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*