Podman容器技术基础入门

Podman容器技术基础入门

介绍podman

为什么使用podman

Podman 是 Docker 的替代品,用于容器化应用程序的本地开发。Podman 命令将 1 对 1 映射 Docker 命令,包括它们的参数。你可以使用 podman 为 docker 添加别名,并且从不会发现管理本地容器的是两种完全不同的工具。Podman 的核心功能之一是它专注于**安全性。**使用 Podman 不需要守护进程。相反,它使用传统的 fork-exec 模型,并且大量地使用 用户名称空间 和 网络名称空间 。因此,Podman 比 Docker 更加孤立,使用起来也更安全。更多细节请点击这里

podman与docker的区别

docker 需要在我们的系统上运行一个守护进程(docker daemon),而podman 不需要
启动容器的方式不同:

  • docker cli 命令通过API跟 Docker Engine(引擎)交互告诉它我想创建一个container,然后docker Engine才会调用OCI container runtime(runc)来启动一个container。这代表container的process(进程)不会是Docker CLI的child process(子进程),而是Docker Engine的child process。Podman是直接给OCI containner runtime(runc)进行交互来创建container的,所以container process直接是podman的child process。

  • 因为docke有docker daemon,所以docker启动的容器支持–restart策略,但是podman不支持,如果在k8s中就不存在这个问题,我们可以设置pod的重启策略,在系统中我们可以采用编写systemd服务来完成自启动docker需要使用root用户来创建容器,但是podman不需要

安装podman

[root@localhost ~]# yum -y install podman
[root@localhost ~]# alias docker=podman

// 定义别名
[root@localhost ~]# docker  images
REPOSITORY   TAG   IMAGE ID   CREATED   SIZE
[root@localhost ~]# podman  images
REPOSITORY   TAG   IMAGE ID   CREATED   SIZE

podman使用帮助

要获得一些帮助并了解Podman的工作原理,您可以使用以下帮助:

$ podman --help
$ podman <subcommand> --help

有关更多详细信息,您可以查看手册页:

$ man podman
$ man podman-<subcommand>

另请参阅Podman 故障排除指南,以查找有关如何解决常见配置错误的已知问题和提示。

普通用户使用的配置

在允许没有root特权的用户运行Podman之前,管理员必须安装或构建Podman并完成以下配置

cgroup V2Linux内核功能允许用户限制普通用户容器可以使用的资源,如果使用cgroup V2启用了运行Podman的Linux发行版,则可能需要更改默认的OCI运行时。某些较旧的版本runc不适用于cgroup V2,必须切换到备用OCI运行时crun。

安装crun

[root@localhost ~]# yum -y install crun

使用–runtime选项在命令行中打开对cgroup V2的替代OCI运行时支持

podman  --runtime crun

也可以修改containers.conf文件runtime = "runc"到runtime = “crun”

[root@localhost ~]# vim /usr/share/containers/containers.conf 
......
runtime = "crun"      # 取消注释并将值改为crun
......


[root@localhost ~]# podman run -d -p 80 docker.io/library/httpd
736c2146b807988538c700a207a45b1be50f4b3e8466b9cc57a68b4d7b5ea568
[root@podman ~]# podman inspect -l | grep crun
        "OCIRuntime": "crun",
            "crun",

安装slirp4netns

slirp4nets包为普通用户提供一种网络模式

[root@localhost ~]# yum -y install slirp4netns

安装fuse-overlayfs

在普通用户环境中使用Podman时,建议使用fuse-overlayfs而不是VFS文件系统,至少需要版本0.7.6。

[root@localhost ~]# yum -y install fuse-overlayfs

配置storage.conf文件

[root@localhost ~]#  vim /etc/containers/storage.conf
......
mount_program = "/usr/bin/fuse-overlayfs"  #取消注释
......

/etc/subuid和/etc/subgid配置

Podman要求运行它的用户在/etc/subuid和/etc/subgid文件中列出一系列UID,shadow-utils或newuid包提供这些文件

[root@podman ~]# cat /etc/subuid
demo:100000:65536
bus:165536:65536

[root@podman ~]# cat /etc/subgid
demo:100000:65536
bus:165536:65536

// 在/etc/subuid和/etc/subgid查看,每个用户的值必须唯一且没有任何重叠

该文件的格式为USERNAME:UID:RANGE

  • 在/ etc / passwd或getpwent中列出的用户名。
  • 为用户分配的初始uid。
  • 为用户分配的UID范围的大小

用户的配置文件

三个主要的配置文件是container.conf,storage.conf和registries.conf。用户可以根据需要修改这些文件。

container.conf
Podman读取时,按照循序来了,当前面一位找不到时,就去找下一个

1./usr/share/containers/containers.conf
2./etc/containers/containers.conf
3.$HOME/.config/containers/containers.conf

storage.conf
podman保存在本地的容器镜像和其运行容器的文件目录。

1./etc/containers/storage.conf
2.$HOME/.config/containers/storage.conf

在普通用户中/etc/containers/storage.conf的一些字段将被忽略

graphroot=``""`` ``container storage graph ``dir` `(default: ``"/var/lib/containers/storage"``)`` ``Default directory to store all writable content created by container storage programs.` `runroot=``""`` ``container storage run ``dir` `(default: ``"/run/containers/storage"``)`` ``Default directory to store all temporary writable content created by container storage programs.

在普通用户中默认

graphroot=``"$HOME/.local/share/containers/storage"``runroot=``"$XDG_RUNTIME_DIR/contai

registries.conf
配置按此顺序读入,这些文件不是默认创建的,可以从/usr/share/containers或复制文件/etc/containers并进行修改

1./etc/containers/registries.conf
2./etc/containers/registries.d/*
3.HOME/.config/containers/registries.conf

podman login 登录,默认授权文件在${XDG_RUNTIME_DIR}/containers/auth.json

[root@podman ~]# cat /run/user/0/containers/auth.json 
{
	"auths": {
		"docker.io": {
			"auth": "********************="
		}
	}
}

podman常用基础命令

podman search:查找

// --filter=is-official:指定查找官方版本的httpd
[root@localhost ~]# podman search httpd --filter=is-official
INDEX       NAME                      DESCRIPTION                      STARS   OFFICIAL   AUTOMATED
docker.io   docker.io/library/httpd   The Apache HTTP Server Project   3794    [OK]       

podman pull:下载(拉取)镜像

注意:Podman在不同的注册管理机构中搜索。因此,建议使用完整的映像名称(docker.io/library/httpd而不是httpd)来确保使用正确的映像

// 拉取httpd镜像
[root@localhost ~]# podman pull docker.io/library/httpd
Trying to pull docker.io/library/httpd...
Getting image source signatures
Copying blob e5ae68f74026 done  
Copying blob aa379c0cedc2 done  
Copying blob d3576f2b6317 done  
Copying blob bc36ee1127ec done  
Copying blob f1aa5f54b226 done  
Copying config ea28e1b82f done  
Writing manifest to image destination
Storing signatures
ea28e1b82f314092abd3f90a69e57d6ccf506382821ee0b8d9b48c3e47440c1f

// 当你不知道镜像准确位置,无法确定的时候,直接podman pull + 镜像名,然后上下滑动选择你要指定拉取镜像的位置
[root@localhost ~]# podman pull nginx
? Please select an image: 
    registry.fedoraproject.org/nginx:latest
    registry.access.redhat.com/nginx:latest
    registry.centos.org/nginx:latest
  ▸ docker.io/library/nginx:latest

podman images:显示所有镜像

[root@localhost ~]# podman images
REPOSITORY                TAG      IMAGE ID       CREATED       SIZE
docker.io/library/nginx   latest   f652ca386ed1   12 days ago   146 MB
docker.io/library/httpd   latest   ea28e1b82f31   12 days ago   148 MB

podman run:运行容器

运行一个非常基本的 httpd 服务器

[root@localhost ~]# podman run -d -p 80:80 docker.io/library/httpd
a15ffcd949cb9f18d0322cf6ae4f740eee2e6ffa0eed0677570747693171d19d

注意:由于容器在分离模式下运行,由命令中的 表示,Podman将在执行命令后打印容器ID。它还添加了一个伪 tty,用于在交互式 shell 中运行任意命令。-dpodman run-t

注意:我们使用端口转发来访问HTTP服务器。要成功运行,至少需要 slirp4netns v0.3.0。

podman ps:列出正在运行的容器

[root@localhost ~]# podman ps
CONTAINER ID  IMAGE                           COMMAND           CREATED             STATUS                 PORTS               NAMES
a15ffcd949cb  docker.io/library/httpd:latest  httpd-foreground  About a minute ago  Up About a minute ago  0.0.0.0:80->80/tcp  thirsty_maxwell

在这里插入图片描述
注意:如果添加 -a 命令,Podman 将显示所有容器(已创建、已退出、正在运行等)

podman inspect:查看容器详细信息

查看最新信息(最新的信息以最新的时间来定义)

您可以"检查"正在运行的容器,以查找有关其自身的元数据和详细信息。 将提供许多有用的信息,如环境变量,网络设置或分配的资源。podman inspect

由于容器在无根模式下运行,因此不会为容器分配 IP 地址

[root@localhost ~]# podman ps -a
CONTAINER ID  IMAGE                           COMMAND           CREATED        STATUS            PORTS               NAMES
a15ffcd949cb  docker.io/library/httpd:latest  httpd-foreground  3 minutes ago  Up 3 minutes ago  0.0.0.0:80->80/tcp  thirsty_maxwell
[root@localhost ~]# podman inspect a15ffcd949cb
[
    {
        "Id": "a15ffcd949cb9f18d0322cf6ae4f740eee2e6ffa0eed0677570747693171d19d",
        "Created": "2021-12-14T18:40:02.330917567+08:00",
        "Path": "httpd-foreground",
        "Args": [
            "httpd-foreground"
        ],
        "State": {
            "OciVersion": "1.0.1-dev",
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "OOMKilled": false,
            ......

// -l 查看最新信息
[root@localhost ~]# podman inspect -l | grep IPAddress
            "SecondaryIPAddresses": null,
            "IPAddress": "10.88.0.2"

curl + IP:访问测试页面

如您所见,容器未分配 IP 地址。容器可通过本地计算机上的已发布端口访问。

[root@localhost ~]# curl 192.168.200.138
<html><body><h1>It works!</h1></body></html>

在另一台计算机上,需要使用运行容器的主机的 IP 地址

curl http://<IP_Address>:80

注意:除了使用curl之外,您还可以将浏览器指向http://localhost:80。

podman logs:查看容器日志

[root@localhost ~]# podman ps
CONTAINER ID  IMAGE                           COMMAND           CREATED        STATUS            PORTS               NAMES
a15ffcd949cb  docker.io/library/httpd:latest  httpd-foreground  7 minutes ago  Up 7 minutes ago  0.0.0.0:80->80/tcp  thirsty_maxwell
[root@localhost ~]# podman logs a15ffcd949cb
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.88.0.2. 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.2. Set the 'ServerName' directive globally to suppress this message
[Tue Dec 14 10:40:03.083511 2021] [mpm_event:notice] [pid 1:tid 140514200993088] AH00489: Apache/2.4.51 (Unix) configured -- resuming normal operations
[Tue Dec 14 10:40:03.083684 2021] [core:notice] [pid 1:tid 140514200993088] AH00094: Command line: 'httpd -D FOREGROUND'
192.168.200.1 - - [14/Dec/2021:10:41:46 +0000] "GET / HTTP/1.1" 200 45
192.168.200.1 - - [14/Dec/2021:10:41:46 +0000] "GET /favicon.ico HTTP/1.1" 404 196
192.168.200.1 - - [14/Dec/2021:10:42:38 +0000] "-" 408 -
192.168.200.138 - - [14/Dec/2021:10:46:06 +0000] "GET / HTTP/1.1" 200 45

//  -l:查看最新日志
[root@localhost ~]# podman logs -l
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 10.88.0.2. 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.2. Set the 'ServerName' directive globally to suppress this message
[Tue Dec 14 10:40:03.083511 2021] [mpm_event:notice] [pid 1:tid 140514200993088] AH00489: Apache/2.4.51 (Unix) configured -- resuming normal operations
[Tue Dec 14 10:40:03.083684 2021] [core:notice] [pid 1:tid 140514200993088] AH00094: Command line: 'httpd -D FOREGROUND'
192.168.200.1 - - [14/Dec/2021:10:41:46 +0000] "GET / HTTP/1.1" 200 45
192.168.200.1 - - [14/Dec/2021:10:41:46 +0000] "GET /favicon.ico HTTP/1.1" 404 196
192.168.200.1 - - [14/Dec/2021:10:42:38 +0000] "-" 408 -
192.168.200.138 - - [14/Dec/2021:10:46:06 +0000] "GET / HTTP/1.1" 200 45

podman top:查看容器的 pids

[root@localhost ~]# podman ps
CONTAINER ID  IMAGE                           COMMAND           CREATED        STATUS            PORTS               NAMES
a15ffcd949cb  docker.io/library/httpd:latest  httpd-foreground  9 minutes ago  Up 9 minutes ago  0.0.0.0:80->80/tcp  thirsty_maxwell
[root@localhost ~]# podman top a15ffcd949cb
USER       PID   PPID   %CPU    ELAPSED           TTY   TIME   COMMAND
root       1     0      0.000   9m30.118872467s   ?     0s     httpd -DFOREGROUND 
www-data   7     1      0.000   9m30.119002833s   ?     0s     httpd -DFOREGROUND 
www-data   8     1      0.000   9m30.119095631s   ?     0s     httpd -DFOREGROUND 
www-data   9     1      0.000   9m30.119190449s   ?     0s     httpd -DFOREGROUND 
www-data   91    1      0.000   7m14.119268827s   ?     0s     httpd -DFOREGROUND 


// -l: 查看最新的pids
[root@localhost ~]# podman top -l
USER       PID   PPID   %CPU    ELAPSED           TTY   TIME   COMMAND
root       1     0      0.000   10m9.965557662s   ?     0s     httpd -DFOREGROUND 
www-data   7     1      0.000   10m9.965785544s   ?     0s     httpd -DFOREGROUND 
www-data   8     1      0.000   10m9.965928944s   ?     0s     httpd -DFOREGROUND 
www-data   9     1      0.000   10m9.966310959s   ?     0s     httpd -DFOREGROUND 
www-data   91    1      0.000   7m53.966531091s   ?     0s     httpd -DFOREGROUND

podman stop:停止容器

[root@localhost ~]# podman ps
CONTAINER ID  IMAGE                           COMMAND           CREATED         STATUS             PORTS               NAMES
0cfcacd30c8e  docker.io/library/httpd:latest  httpd-foreground  21 seconds ago  Up 20 seconds ago                      web
a15ffcd949cb  docker.io/library/httpd:latest  httpd-foreground  12 minutes ago  Up 11 minutes ago  0.0.0.0:80->80/tcp  thirsty_maxwell

-l:停止最新的容器 
[root@localhost ~]# podman stop -l
0cfcacd30c8e9736993b5ca57826649de65b424d386f7bdf954d38ce5b5ae24d
[root@localhost ~]# podman ps
CONTAINER ID  IMAGE                           COMMAND           CREATED         STATUS             PORTS               NAMES
a15ffcd949cb  docker.io/library/httpd:latest  httpd-foreground  12 minutes ago  Up 12 minutes ago  0.0.0.0:80->80/tcp  thirsty_maxwell

podman start:启动容器

-l:启动最新的容器
[root@localhost ~]# podman start -l
0cfcacd30c8e9736993b5ca57826649de65b424d386f7bdf954d38ce5b5ae24d
[root@localhost ~]# podman ps
CONTAINER ID  IMAGE                           COMMAND           CREATED         STATUS             PORTS               NAMES
0cfcacd30c8e  docker.io/library/httpd:latest  httpd-foreground  2 minutes ago   Up 2 seconds ago                       web
a15ffcd949cb  docker.io/library/httpd:latest  httpd-foreground  13 minutes ago  Up 13 minutes ago  0.0.0.0:80->80/tcp  thirsty_maxwell

podman rm:删除容器

-f:强制删除
-l:指定最新的
[root@localhost ~]# podman ps -a
CONTAINER ID  IMAGE                           COMMAND           CREATED         STATUS             PORTS               NAMES
0cfcacd30c8e  docker.io/library/httpd:latest  httpd-foreground  4 minutes ago   Up 2 minutes ago                       web
a15ffcd949cb  docker.io/library/httpd:latest  httpd-foreground  15 minutes ago  Up 15 minutes ago  0.0.0.0:80->80/tcp  thirsty_maxwell
[root@localhost ~]# podman rm -f -l
0cfcacd30c8e9736993b5ca57826649de65b424d386f7bdf954d38ce5b5ae24d

podman rmi:删除镜像

-f:强制删除
[root@localhost ~]# podman images
REPOSITORY                TAG      IMAGE ID       CREATED       SIZE
docker.io/library/nginx   latest   f652ca386ed1   12 days ago   146 MB
docker.io/library/httpd   latest   ea28e1b82f31   12 days ago   148 MB
[root@localhost ~]# podman rmi -f f652ca386ed1
Untagged: docker.io/library/nginx:latest
Deleted: f652ca386ed135a4cbe356333e08ef0816f81b2ac8d0619af01e2b256837ed3e
[root@localhost ~]# podman images
REPOSITORY                TAG      IMAGE ID       CREATED       SIZE
docker.io/library/httpd   latest   ea28e1b82f31   12 days ago   148 MB

podman tag:修改标签

[root@localhost ~]# podman images
REPOSITORY                TAG      IMAGE ID       CREATED       SIZE
docker.io/library/httpd   latest   ea28e1b82f31   12 days ago   148 MB
[root@localhost ~]# podman tag docker.io/library/httpd:latest docker.io/syblyw0806/httpd:v1.0
[root@localhost ~]# podman images
REPOSITORY                   TAG      IMAGE ID       CREATED       SIZE
docker.io/syblyw0806/httpd   v1.0     ea28e1b82f31   12 days ago   148 MB
docker.io/library/httpd      latest   ea28e1b82f31   12 days ago   148 MB
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值