Docker 大勢已去,Podman 即將崛起!
Podman 簡介
什麼是 Podman?
Podman 官網地址:https://podman.io/
Podman 和 Docker 的主要區別是什麼?
圖中所體現的事情是,podman 不需要守護進程,而 docker 需要守護進程。在這個圖的示意中,docker 的 containerd-shim 與 podman 的 common 被歸在 Container 一層。
Podman 的使用與 docker 有什麼區別?
podman 的定位也是與 docker 兼容,因此在使用上面儘量靠近 docker。在使用方面,可以分成兩個方面來說,一是系統構建者的角度,二是使用者的角度。
在使用者方面,podman 與 docker 的命令基本兼容,都包括容器運行時(run/start/kill/ps/inspect),本地鏡像(images/rmi/build)、鏡像倉庫(login/pull/push)等幾個方面。因此 podman 的命令行工具與 docker 類似,比如構建鏡像、啓停容器等。甚至可以通過alias docker=podman
可以進行替換。因此,即便使用了 podman,仍然可以使用 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 #檢索鏡像
docke 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
//倉庫配置
[root@localhost ~]# vim /etc/containers/registries.conf
[registries.search]
registries = ['registry.access.redhat.com', 'registry.redhat.io', 'docker.io'] //這個是查找,從這三個地方查找,如果只留一個,則只在一個源裏查找
unqualified-search-registries = ["registry.fedoraproject.org", "registry.access.redhat.com", "registry.centos.org", "docker.io"] //這裏也要改爲一個
[registries.insecure]
registries = [10.0.0.1] //這裏寫那些http的倉庫,比如harbor
//配置加速器
[registries.search]
registries = ['https://l9h8fu9j.mirror.aliyuncs.com','docker.io']
使用 Podman
使用 Podman 非常的簡單,Podman 的指令跟 Docker 大多數都是相同的。另外,Docker 系列面試題和答案全部整理好了,微信搜索 Java 技術棧,在後臺發送:面試,可以在線閱讀。
運行一個容器
[root@localhost ~]# podman run -d --name httpd docker.io/library/httpd
Trying to pull docker.io/library/httpd...
Getting image source signatures
Copying blob e5ae68f74026 done
Copying blob d3576f2b6317 done
Copying blob bc36ee1127ec done
Copying blob f1aa5f54b226 done
Copying blob aa379c0cedc2 done
Copying config ea28e1b82f done
Writing manifest to image destination
Storing signatures
0492e405b9ecb05e6e6be1fec0ac1a8b6ba3ff949df259b45146037b5f355035
//查看鏡像
[root@localhost ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/httpd latest ea28e1b82f31 11 days ago 148 MB
列出運行的容器
[root@localhost ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0492e405b9ec docker.io/library/httpd:latest httpd-foreground About a minute ago Up About a minute ago httpd
注意:如果在 ps 命令中添加 - a,Podman 將顯示所有容器。
檢查正在運行的容器
[root@localhost ~]# podman inspect -l | grep IPAddress\":
"SecondaryIPAddresses": null,
"IPAddress": "10.88.0.5",
[root@localhost ~]# curl 10.88.0.5
<html><body><h1>It works!</h1></body></html>
注意:-l 是最新容器的便利參數。您還可以使用容器的 ID 代替 -l。
查看一個運行中容器的日誌
選項
--latest #最近的
[root@localhost ~]# podman logs --latest
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.88.0.5. Set the 'ServerName' directive globally to suppress this message
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.88.0.5. Set the 'ServerName' directive globally to suppress this message
[Mon Dec 13 15:17:53.690844 2021] [mpm_event:notice] [pid 1:tid 140665160166720] AH00489: Apache/2.4.51 (Unix) configured -- resuming normal operations
[Mon Dec 13 15:17:53.690946 2021] [core:notice] [pid 1:tid 140665160166720] AH00094: Command line: 'httpd -D FOREGROUND'
10.88.0.1 - - [13/Dec/2021:15:19:48 +0000] "GET / HTTP/1.1" 200 45
10.88.0.1 - - [13/Dec/2021:15:20:47 +0000] "GET / HTTP/1.1" 200 45
查看一個運行容器中的進程資源使用情況
podman top <container_id>
[root@localhost ~]# podman top httpd
USER PID PPID %CPU ELAPSED TTY TIME COMMAND
root 1 0 0.000 15m38.599711321s ? 0s httpd -DFOREGROUND
www-data 7 1 0.000 15m38.599783256s ? 0s httpd -DFOREGROUND
www-data 8 1 0.000 15m38.599845342s ? 0s httpd -DFOREGROUND
www-data 9 1 0.000 15m38.599880444s ? 0s httpd -DFOREGROUND
停止一個運行中的容器
[root@localhost ~]# podman stop --latest
2f3edf712621d3a41e03fa8c7f6a5cdba56fbbad43a7a59ede26cc88f31006c4
[root@localhost ~]# podman ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
刪除一個容器
[root@localhost ~]# podman rm --latest
2f3edf712621d3a41e03fa8c7f6a5cdba56fbbad43a7a59ede26cc88f31006c4
[root@localhost ~]# podman ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
以上這些特性基本上都和 Docker 一樣,Podman 除了兼容這些特性外,還支持了一些新的特性。
上傳鏡像
例如,如果我們想在 docker.io 上分享我們新建的 Nginx 容器鏡像,這很容易。首先登錄碼頭:
[root@localhost nginx]# tree
.
├── Dockerfile
└── files
└── nginx-1.20.1.tar.gz
[root@localhost nginx]# cat Dockerfile
FROM docker.io/library/centos
ENV PATH /usr/local/nginx/sbin:$PATH
ADD files/nginx-1.20.1.tar.gz /usr/src
RUN useradd -r -M -s /sbin/nologin nginx && \
yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make && \
mkdir -p /var/log/nginx && \
cd /usr/src/nginx-1.20.1 && \
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log && \
make && make install
CMD ["nginx","-g","daemon off"]
[root@localhost nginx]# podman build -t nginx .
// 修改鏡像名
[root@localhost ~]# podman tag docker.io/library/nginx:latest docker.io/1314444/test:latest
// 登錄並上傳鏡像
[root@localhost ~]# podman login docker.io // 需要告訴其要登錄到docker倉庫
[root@localhost ~]# podman login docker.io
Username: 1314444 #賬戶
Password: ******** #密碼
Login Succeeded!
[root@localhost nginx]# podman push docker.io/1314444/test:latest //上傳鏡像
Getting image source signatures
Copying blob 38c40d6c2c85 done
Copying blob fee76a531659 done
Copying blob c2adabaecedb done
Copying config 7f3589c0b8 done
Writing manifest to image destination
Copying config 7f3589c0b8 done
Writing manifest to image destination
Storing signatures
//請注意,我們將四層推送到我們的註冊表,現在可供其他人共享。快速瀏覽一下:
[root@localhost ~]# podman inspect 1314444/test:nginx
//輸出:
[
{
"Id": "7f3589c0b8849a9e1ff52ceb0fcea2390e2731db9d1a7358c2f5fad216a48263",
"Digest": "sha256:7822b5ba4c2eaabdd0ff3812277cfafa8a25527d1e234be028ed381a43ad5498",
"RepoTags": [
"docker.io/1314444/test:nginx",
......
總而言之,Podman 使查找、運行、構建和共享容器變得容易。
配置別名
如果習慣了使用 Docker 命令,可以直接給 Podman 配置一個別名來實現無縫轉移。你只需要在 .bashrc 下加入以下行內容即可:
[root@localhost ~]# echo "alias docker=podman" >> .bashrc
source .bashrc
[root@localhost ~]# alias
alias cp='cp -i'
alias docker='podman'
.......
用戶操作
在允許沒有 root 特權的用戶運行 Podman 之前,管理員必須安裝或構建 Podman 並完成以下配置。
[root@localhost ~]# yum -y install crun //centos8系統自帶
[root@localhost ~]# vi /usr/share/containers/containers.conf
446 # Default OCI runtime
447 #
448 runtime = "crun" //取消註釋並將runc改爲crun
[root@localhost ~]# podman run -d --name web -p 80:80 docker.io/library/nginx
c8664d2e43c872e1e5219f82d41f63048ed3a5ed4fb6259c225a14d6c243677f
[root@localhost ~]# podman inspect web | grep crun
"OCIRuntime": "crun",
"crun",
安裝 slirp4netns 和 fuse-overlayfs
在普通用戶環境中使用 Podman 時,建議使用 fuse-overlayfs 而不是 VFS 文件系統,至少需要版本 0.7.6。現在新版本默認就是了。
最新面試題整理好了,大家可以在 Java 面試庫小程序在線刷題。
[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 zz
[root@localhost ~]# cat /etc/subuid
zz:100000:65536
[root@localhost ~]# cat /etc/subgid
zz:100000:65536
// 啓動非特權ping
[root@localhost ~]# sysctl -w "net.ipv4.ping_group_range=0 200000" //大於100000這個就表示tom可以操作podman
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以下,則需要做以下操作:
在普通用戶中這些字段默認
graphroot="$HOME/.local/share/containers/storage"
runroot="$XDG_RUNTIME_DIR/containers"
- registries.conf
配置按此順序讀入, 這些文件不是默認創建的, 可以從 / usr/share/containers 或複製文件 / etc/containers 並進行修改。
最新面試題整理好了,大家可以在 Java 面試庫小程序在線刷題。
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 用戶的鏡像的
//root用戶
[root@localhost ~]# podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/library/httpd latest ea28e1b82f31 11 days ago 146 MB
//普通用戶
[root@localhost ~]# su - zz
[zz@localhost ~]$ podman images
REPOSITORY TAG IMAGE ID CREATED SIZE
卷
使用卷
[root@localhost ~]# su - zz
[zz@localhost ~]$ pwd
/home/zz
[zz@localhost ~]$ mkdir /home/zz/data
[zz@localhost ~]$ podman run -it -v "$(pwd)"/data:/data docker.io/library/busybox /bin/sh
Trying to pull docker.io/library/busybox:latest...
Getting image source signatures
Copying blob 3cb635b06aa2 done
Copying config ffe9d497c3 done
Writing manifest to image destination
Storing signatures
/ # ls
bin data dev etc home proc root run sys tmp usr var
/ # cd data/
/data # ls
/data # touch 123
/data # ls -l
total 0
-rw-r--r-- 1 root root 0 Dec 13 00:17 123
在主機上查看
[zz@localhost ~]$ ll data/
總用量 0
-rw-r--r-- 1 zz zz 0 12月 13 00:17 123
//寫入文件
[zz@localhost ~]$ echo "hell world" >> 123
[zz@localhost ~]$ cat 123
hell world
容器裏查看
/data # cat 123
hell world
//我們可以發現在容器裏面的文件的屬主和屬組都屬於root,那麼如何才能讓其屬於tom用戶呢?下面告訴你答案
/data # ls -l
total 4
-rw-rw-r-- 1 root root 12 Dec 13 00:20 123
//只要在運行容器的時候加上一個--userns=keep-id即可。
[zz@localhost ~]$ podman run -it --name test -v "$(pwd)"/data:/data --userns=keep-id docker.io/library/busybox /bin/sh
~ $ cd data/
/data $ ls -l
total 4
-rw-r--r-- 1 zz zz 11 Dec 13 00:21 123
使用普通用戶映射容器端口時會報 “permission denied” 的錯誤
[zz@localhost ~]$ podman run -d -p 80:80 httpd
Error: rootlessport cannot expose privileged port 80, you can add 'net.ipv4.ip_unprivileged_port_start=80' to /etc/sysctl.conf (currently 1024), or choose a larger port number (>= 1024): listen tcp 0.0.0.0:80: bind: permission denied
[zz@localhost ~]$ podman run -d -p 1024:80 httpd
58613a6bdc70d4d4f9f624583f795a62a610596d166f0873bdff8fb26aa15092
[zz@localhost ~]$ ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:1024 *:*
LISTEN 0 128 [::]:22 [::]:*
配置echo ‘net.ipv4.ip_unprivileged_port_start=80’ >> /etc/sysctl.conf
後可以映射大於等於 80 的端口
[root@localhost ~]# echo 'net.ipv4.ip_unprivileged_port_start=80' >> /etc/sysctl.conf
[root@localhost ~]# sysctl -p
net.ipv4.ip_unprivileged_port_start = 80
[zz@localhost ~]$ podman run -d -p 80:80 httpd
1215455a0c300d78e7bf6afaefc9873f818c6b0f26affeee4e2bc17954e72d8e
[zz@localhost ~]$ ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:1024 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
好了,關於 Podman,我們就聊到這裏,如果覺得文章對你有幫助,歡迎點個贊和在看哦~~
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/5BCPIsNL265EvAUye7DUog