Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏

第 105 章 Podman

目录

105.1. 安装 Podman
105.1.1. RockyLinux 安装 Podman
105.1.2. Almalinux 9.0
105.1.3. MacOS 安装 Podman
105.1.4. 初始化 Podman
105.1.5. 兼容 Docker
105.1.6. 安装 podman-compose
105.1.7. 配置 Podman
105.2. podman 管理
105.2.1. 虚拟机管理
105.2.2. 镜像管理
105.2.3. 运行命令
105.3. 按例
105.3.1. podman run 用法
105.3.2. mysql
105.3.3. 制作镜像

105.1. 安装 Podman

105.1.1. RockyLinux 安装 Podman

某些 Redhat 家族的 Linux 是自带 Podman,例如 Almalinux 9.0,RockyLinux没有自带 podman 需要自己安装,是方法执行下面的命令

			
[root@netkiller ~]# dnf install -y podman
		
		

105.1.2. Almalinux 9.0

Almalinux 9.0 自带 podman

		 
systemctl enable podman		
		
		

105.1.3. MacOS 安装 Podman

MacOS 安装方法

			 
brew install podman
            
		

105.1.4. 初始化 Podman

初始化,启动 Podman

			 
podman machine init
podman machine start
            
		

操作演示

			 
neo@MacBook-Pro-M2 ~ % podman machine init
Downloading VM image: fedora-coreos-37.20221127.2.0-qemu.aarch64.qcow2.xz: done  
Extracting compressed file Image resized.
Machine init complete
To start your machine run:

    podman machine start

neo@MacBook-Pro-M2 ~ % podman machine start          
Starting machine "podman-machine-default"
Waiting for VM ...
Mounting volume... /Users/neo:/Users/neo

This machine is currently configured in rootless mode. If your containers
require root permissions (e.g. ports < 1024), or if you run into compatibility
issues with non-podman clients, you can switch using the following command: 

    podman machine set --rootful

API forwarding listening on: /Users/neo/.local/share/containers/podman/machine/podman-machine-default/podman.sock

The system helper service is not installed; the default Docker API socket
address can't be used by podman. If you would like to install it run the
following commands:

    sudo /opt/homebrew/Cellar/podman/4.3.1/bin/podman-mac-helper install
    podman machine stop; podman machine start

You can still connect Docker API clients by setting DOCKER_HOST using the
following command in your terminal session:

    export DOCKER_HOST='unix:///Users/neo/.local/share/containers/podman/machine/podman-machine-default/podman.sock'

Machine "podman-machine-default" started successfully
            
		

105.1.5. 兼容 Docker

		
echo "alias docker='podman'" >> ~/.bashrc
source ~/.bashrc
		
		

105.1.6. 安装 podman-compose

安装 python 3.13

			
[root@netkiller ~]# dnf install python3.13 python3.13-pip
[root@netkiller ~]# rm -f /usr/bin/python3
[root@netkiller ~]# ln -s /usr/bin/python3.13 /usr/bin/python3
			
		
			
[root@netkiller ~]# dnf install -y podman-compose
[root@netkiller ~]# pip install podman-compose
			
		

验证是否正常工作

		
[root@netkiller ~]# podman compose 
Run compose workloads via an external provider such as docker-compose or podman-compose

Description:
  This command is a thin wrapper around an external compose provider such as docker-compose or podman-compose.  This means that podman compose is executing another tool that implements the compose functionality but sets up the environment in a way to let the compose provider communicate transparently with the local Podman socket.  The specified options as well the command and argument are passed directly to the compose provider.

The default compose providers are docker-compose and podman-compose.  If installed, docker-compose takes precedence since it is the original implementation of the Compose specification and is widely used on the supported platforms (i.e., Linux, Mac OS, Windows).

If you want to change the default behavior or have a custom installation path for your provider of choice, please change the compose_providers field in containers.conf(5) to compose_providers = ["/path/to/provider"]. You may also set the PODMAN_COMPOSE_PROVIDER environment variable.

Usage:
  podman compose [options]

Examples:
  podman compose -f nginx.yaml up --detach
  podman --log-level=debug compose -f many-images.yaml pull

>>>> Executing external compose provider "/usr/local/bin/podman-compose". Please see podman-compose(1) for how to disable this message. <<<<

usage: podman-compose [-h] [-v] [--in-pod in_pod] [--pod-args pod_args] [--env-file env_file] [-f file] [--profile profile] [-p PROJECT_NAME] [--podman-path PODMAN_PATH]
                      [--podman-args args] [--podman-pull-args args] [--podman-push-args args] [--podman-build-args args] [--podman-inspect-args args] [--podman-run-args args]
                      [--podman-start-args args] [--podman-stop-args args] [--podman-rm-args args] [--podman-volume-args args] [--no-ansi] [--no-cleanup] [--dry-run]
                      [--parallel PARALLEL] [--verbose]
                      {help,version,wait,systemd,pull,push,build,up,down,ps,run,exec,start,stop,restart,logs,config,port,pause,unpause,kill,stats,images} ...

options:
  -h, --help            show this help message and exit
  -v, --version         show version
  --in-pod in_pod       Specify pod usage:
                          'true'   - create/use a pod named pod_<project name>
                          'false'  - do not use a pod
                          '<name>' - create/use a custom pod with the given name
  --pod-args pod_args   custom arguments to be passed to `podman pod`
  --env-file env_file   Specify an alternate environment file
  -f, --file file       Specify an compose file (default: docker-compose.yml) or '-' to read from stdin.
  --profile profile     Specify a profile to enable
  -p, --project-name PROJECT_NAME
                        Specify an alternate project name (default: directory name)
  --podman-path PODMAN_PATH
                        Specify an alternate path to podman (default: use location in $PATH variable)
  --podman-args args    custom global arguments to be passed to `podman`
  --podman-pull-args args
                        custom arguments to be passed to `podman pull`
  --podman-push-args args
                        custom arguments to be passed to `podman push`
  --podman-build-args args
                        custom arguments to be passed to `podman build`
  --podman-inspect-args args
                        custom arguments to be passed to `podman inspect`
  --podman-run-args args
                        custom arguments to be passed to `podman run`
  --podman-start-args args
                        custom arguments to be passed to `podman start`
  --podman-stop-args args
                        custom arguments to be passed to `podman stop`
  --podman-rm-args args
                        custom arguments to be passed to `podman rm`
  --podman-volume-args args
                        custom arguments to be passed to `podman volume`
  --no-ansi             Do not print ANSI control characters
  --no-cleanup          Do not stop and remove existing pod & containers
  --dry-run             No action; perform a simulation of commands
  --parallel PARALLEL
  --verbose             Print debugging output

command:
  {help,version,wait,systemd,pull,push,build,up,down,ps,run,exec,start,stop,restart,logs,config,port,pause,unpause,kill,stats,images}
    help                show help
    version             show version
    wait                wait running containers to stop
    systemd             create systemd unit file and register its compose stacks
    pull                pull stack images
    push                push stack images
    build               build stack images
    up                  Create and start the entire stack or some of its services
    down                tear down entire stack
    ps                  show status of containers
    run                 create a container similar to a service to run a one-off command
    exec                execute a command in a running container
    start               start specific services
    stop                stop specific services
    restart             restart specific services
    logs                show logs from services
    config              displays the compose file
    port                Prints the public port for a port binding.
    pause               Pause all running containers
    unpause             Unpause all running containers
    kill                Kill one or more running containers with a specific signal
    stats               Display percentage of CPU, memory, network I/O, block I/O and PIDs for services.
    images              List images used by the created containers		
		
		

105.1.6.1. 让 Podman 支持 Docker Compose(旧版本)

启用 socket

		 
systemctl enable podman.socket
systemctl start podman.socket
systemctl status podman.socket
		
			

验证 sock 是否正常工作

		 
[root@localhost ~]# curl -H "Content-Type: application/json" --unix-socket /run/podman/podman.sock http://localhost/_ping
OK
		
			

此时可以使用 docker compose

		 
[root@localhost ~]# ln -s /run/podman/podman.sock /var/run/docker.sock		
		
			

105.1.7. 配置 Podman

105.1.7.1. Registry

/etc/containers/registries.conf

		 
mkdir -p /var/lib/registry
podman run --privileged -d --name registry -p 5000:5000 -v /var/lib/registry:/var/lib/registry --restart=always registry:2		
		
			

修改 /etc/containers/registries.conf 配置文件

		 
registries = []
改为
registries = ['localhost:5000']