本教程适用于以下操作系统版本

  • Ubuntu 22.04 (Jammy Jellyfish)
  • Ubuntu 20.04 (Focal Fossa)

本页内容

  • 先决条件
  • 在 Ubuntu 22.04 上安装 Podman
  • 配置 Podman Registry
  • 使用 Podman 下载镜像
  • 使用 Podman 创建和管理容器
  • Podman 入门
  • 总结

Podman(也被称为“Pod Manager”)是一款用于创建和管理容器的开源工具。它是 libpod 库的一部分,不依赖于 Docker daemon,并且兼容 Docker。它是 Kubernetes 的一个简单且轻量级的容器运行时环境。随着 Docker 许可证最近的变更,Podman 可以成为在大多数 Linux 系统中使用的良好替代方案。

在本教程中,我将向您展示如何在 Ubuntu 22.04 服务器上安装和使用 Podman。

先决条件

  • 一台运行 Ubuntu 22.04 的服务器。
  • 服务器上已配置 root 密码。

在 Ubuntu 22.04 上安装 Podman

默认情况下,Podman 软件包已包含在 Ubuntu 默认仓库中。您只需运行以下命令即可安装它:

apt install podman -y

Podman 安装完成后,您可以使用以下命令验证它:

podman -v

您应该在以下输出中看到 Podman 版本:

podman version 3.4.4

您还可以使用以下命令查看有关 Podman 的详细信息:

podman info

您应该获得以下输出:

host:
  arch: amd64
  buildahVersion: 1.23.1
  cgroupControllers:
  - cpuset
  - cpu
  - io
  - memory
  - hugetlb
  - pids
  - rdma
  - misc
  cgroupManager: systemd
  cgroupVersion: v2
  conmon:
    package: 'conmon: /usr/bin/conmon'
    path: /usr/bin/conmon
    version: 'conmon version 2.0.25, commit: unknown'
  cpus: 2
  distribution:
    codename: jammy
    distribution: ubuntu
    version: "22.04"
  eventLogger: journald
  hostname: ubuntu2204
  idMappings:

配置 Podman Registry

默认情况下,Podman registry 未配置为从网络下载和安装容器镜像。因此您需要先对其进行配置。

nano /etc/containers/registries.conf

添加以下行:

[registries.search]
registries=["registry.access.redhat.com", "registry.fedoraproject.org", "docker.io"]

完成后保存并关闭文件。

使用 Podman 下载镜像

Podman 允许您轻松地从网络搜索和下载镜像。例如,要搜索 Debian 镜像,请运行以下命令:

podman search debian

您应该在以下输出中获得所有 Debian 镜像的列表:

INDEX       NAME                                           DESCRIPTION                                      STARS       OFFICIAL    AUTOMATED
docker.io   docker.io/library/ubuntu                       Ubuntu is a Debian-based Linux operating sys...  14959       [OK]        
docker.io   docker.io/library/debian                       Debian is a Linux distribution that's compos...  4435        [OK]        
docker.io   docker.io/library/neurodebian                  NeuroDebian provides neuroscience research s...  93          [OK]        
docker.io   docker.io/bitnami/debian-base-buildpack        Debian base compilation image                    2                       [OK]
docker.io   docker.io/mirantis/debian-build-ubuntu-xenial                                                   0                       
docker.io   docker.io/mirantis/debian-build-ubuntu-trusty                                                   0                       
docker.io   docker.io/osrf/debian_arm64                    Debian arm64 Base Images                         1                       
docker.io   docker.io/rancher/debianconsole                                                                 1                       
docker.io   docker.io/dokken/debian-10                     Debian 10 image for use with kitchen-dokken      0                       
docker.io   docker.io/dokken/debian-9                      Debian 9 image for kitchen-dokken                0                       
docker.io   docker.io/ustclug/debian                       Official Debian Image with USTC Mirror           1                       
docker.io   docker.io/dokken/debian-8                      EOL: Debian 8 image for kitchen-dokken           0                       
docker.io   docker.io/dokken/debian-11                     Debian 11 image for use with kitchen-dokken      0                       
docker.io   docker.io/corpusops/debian-bare                https://github.com/corpusops/docker-images/      0                       
docker.io   docker.io/datadog/debian-i386                                                                   0                       
docker.io   docker.io/corpusops/debian                     debian corpusops baseimage                       0                       
docker.io   docker.io/osrf/debian_armhf                    Debian Armhf Base Images                         1                       
docker.io   docker.io/treehouses/debian                                                                     2                       
docker.io   docker.io/dokken/debian-7                      EOL DISTRO: For use with kitchen-dokken, Bas...  0                       
docker.io   docker.io/treehouses/debian-tags                                                                0                       
docker.io   docker.io/dokken/debian-12                                                                      0                       
docker.io   docker.io/dockage/debian-runit                 Docker image uses runit as a process supervi...  2                       [OK]
docker.io   docker.io/galaxy/debian32-wheel                                                                 0                       
docker.io   docker.io/galaxy/debian-wheel                                                                   0                       
docker.io   docker.io/dockage/debian                       Debian image that forms the base for some Do...  2                       [OK]

接下来,使用以下命令从互联网下载最新的 Debian 镜像:

podman pull debian

这将搜索并下载最新的 Debian 镜像,如下所示:

Getting image source signatures
Copying blob 23858da423a6 done  
Copying config 43d28810c1 done  
Writing manifest to image destination
Storing signatures
43d28810c1b4c28a1be3bac8e0e40fcc472b2bfcfcda952544ed99cb874d2b1a

现在您可以使用以下命令验证已下载的镜像:

podman images

您应该看到以下输出:

REPOSITORY                TAG         IMAGE ID      CREATED     SIZE
docker.io/library/debian  latest      43d28810c1b4  5 days ago  129 MB

使用 Podman 创建和管理容器

借助 Podman,您可以通过命令行轻松创建和管理容器。

要从 Debian 镜像创建容器,请运行以下命令:

podman run -dit --name debian-container debian

容器创建完成后,您可以使用以下命令查看正在运行的容器:

podman ps

您应该看到以下输出:

CONTAINER ID  IMAGE                            COMMAND     CREATED        STATUS            PORTS       NAMES
533698eaf6f1  docker.io/library/debian:latest  bash        6 seconds ago  Up 6 seconds ago              debian-container

要连接到 Debian 容器,请运行以下命令:

podman attach debian-container

您将进入 Debian 容器,如下所示:

root@533698eaf6f1:/# 

您可以使用以下命令验证容器版本:

cat /etc/os-release

您将获得以下输出:

PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

要退出容器,请运行以下命令:

root@533698eaf6f1:/# exit

要停止容器,请运行以下命令:

podman stop debian-container

要启动容器,请运行以下命令:

podman start debian-container

要删除容器,请运行以下命令:

podman rm debian-container

要删除 Debian 镜像,请运行以下命令:

podman rmi debian

您将获得以下输出:

Untagged: docker.io/library/debian:latest
Deleted: 43d28810c1b4c28a1be3bac8e0e40fcc472b2bfcfcda952544ed99cb874d2b1a

您可以使用以下命令列出所有 Podman 命令选项:

podman --help

您应该在以下输出中看到所有有用的选项:

Manage pods, containers and images

Usage:
  podman [options] [command]

Available Commands:
  attach      Attach to a running container
  auto-update Auto update containers according to their auto-update policy
  build       Build an image using instructions from Containerfiles
  commit      Create new image based on the changed container
  container   Manage containers
  cp          Copy files/folders between a container and the local filesystem
  create      Create but do not start a container
  diff        Display the changes to the object's file system
  events      Show podman events
  exec        Run a process in a running container
  export      Export container's filesystem contents as a tar archive
  generate    Generate structured data based on containers, pods or volumes
  healthcheck Manage health checks on containers
  help        Help about any command
  history     Show history of a specified image
  image       Manage images
  images      List images in local storage
  import      Import a tarball to create a filesystem image
  info        Display podman system information
  init        Initialize one or more containers
  inspect     Display the configuration of object denoted by ID
  kill        Kill one or more running containers with a specific signal
  load        Load image(s) from a tar archive
  login       Login to a container registry
  logout      Logout of a container registry
  logs        Fetch the logs of one or more containers
  machine     Manage a virtual machine
  manifest    Manipulate manifest lists and image indexes
  mount       Mount a working container's root filesystem
  network     Manage networks
  pause       Pause all the processes in one or more containers
  play        Play containers, pods or volumes from a structured file
  pod         Manage pods
  port        List port mappings or a specific mapping for the container
  ps          List containers
  pull        Pull an image from a registry
  push        Push an image to a specified destination
  rename      Rename an existing container
  restart     Restart one or more containers
  rm          Remove one or more containers
  rmi         Removes one or more images from local storage
  run         Run a command in a new container
  save        Save image(s) to an archive
  search      Search registry for image
  secret      Manage secrets
  start       Start one or more containers
  stats       Display a live stream of container resource usage statistics
  stop        Stop one or more containers
  system      Manage podman
  tag         Add an additional name to a local image
  top         Display the running processes of a container
  unmount     Unmounts working container's root filesystem
  unpause     Unpause the processes in one or more containers
  unshare     Run a command in a modified user namespace
  untag       Remove a name from a local image
  version     Display the Podman version information
  volume      Manage volumes
  wait        Block on one or more containers

Podman 入门

此处提供了一份关于如何使用 Podman 创建镜像、卷和容器的详细指南:Getting Started with Podman: Manage Images, Containers and Volumes

总结

恭喜!您已成功在 Ubuntu 22.04 服务器上安装了 Podman。您现在可以使用 Podman 作为 Docker 的替代工具来管理容器。如果您有任何问题,请随时向我提问。有关 Podman 的更多信息,请访问 Podman 文档页面。