Podman<一>开机自启容器

Podman是一个无需守护进程的容器引擎,它提供了与Docker类似的命令行接口。Podman的优势在于不需要root权限,使用用户命名空间模拟root运行,且兼容Docker大部分功能。此外,它支持通过systemd单元文件进行服务管理。本文详细介绍了Podman的安装、基本操作,包括运行web容器并设置开机自启,展示了如何在无root环境下管理容器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


在这里插入图片描述


1.podman介绍

podman之前是CRI-O项目的一部分,后被分离成独立的项目libpod,libpod是一个创建容器pod的工具和库,podman是个无守护程序容器引擎,以root用户或无根模式运行,简而言之podman提供了一个docker-CLI的命令行,管理着容器

2.与docker相比的优势

docker劣势一:

docker大家都知道,其守护程序在多个核心上占用差不多高达100%cpu资源,采用C/S模型

podman优势一:

podman不需要守护进程,不需要root权限组,而且利用着用户命名空间(namespace)模拟容器中的root运行,采用fork/exec模型。

fork/exec模型相比C/S模型优势

  • 系统管理员知道某个容器由谁启动
  • 利用cgroup对podman做限制,对应着创建的容器也会受到限制
  • systemd单元文件的生成,可以管理着任务的启动与关闭
  • socket激活,将socker从systemd发送给podman容器使用
3.兼容性

docker的功能大部分podman都是兼容的,也可以使用别名(alias)来写成docker的命令

4.后台服务单元文件的优先级

/usr/lib/systemd/user:优先级最低,会被优先级高的同名 unit 覆盖

~/.local/share/systemd/user

/etc/systemd/user:全局共享的用户级 unit[s]

~/.config/systemd/user:优先级最高

5.podman基本操作

安装

#默认centos源
[root@slave02 ~]# yum -y  module install container-tools   #容器工具基于模块
[root@slave02 ~]# yum  -y install podman-docker            #安装docker兼容包(可选)

版本

[root@slave02 ~]# podman -v
podman version 3.3.0-dev

仓库

官方仓库:registry.access.redhat.com

第三方仓库:docker.io

私有仓库:registry.lab.example.com

命令帮助

[root@slave02 ~]# podman help|head -15
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
....

镜像加速器

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

注意:不能带有httpds//:url格式

[root@slave02 ~]# cp /etc/containers/registries.conf  /backup/registries.conf.back  #备份一下          
[root@slave02 ~]# vim  /etc/containers/registries.conf
unqualified-search-registries = ["docker.io"]           #非限定搜索登记处
[[registry]]
prefix = "docker.io"
location = "x"             #x是阿里加速镜像地址

拉取镜像

[root@slave02 ~]# podman pull nginx

6.运行一个web容器

后台启动一个web容器,并访问容器内容

#准备html页面内容
[root@192 ~]# cat /opt/webhtml/index.html 
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambition

#运行一个守护web容器进程,将/opt/webhtml目录内容映射到容器的/usr/share/nginx/html存放网页的位置
[root@192 ~]# podman run -d --name web -p 8888:80 -v /opt/webhtml:/usr/share/nginx/html nginx
3528e6d5148bcf980f0df5708a82419d3485a33d1d16d722db3e880cc103cd2c

[root@podman ~]# curl 192.168.136.129:8888
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambition

#容器的ip
[root@podman ~]# podman inspect web|grep IPAddress
"IPAddress": "10.88.0.6",
"IPAddress": "10.88.0.6",
#宿主机的ip
[root@podman ~]# ip r
192.168.136.0/24 dev ens33 proto kernel scope link src 192.168.136.129 metric 100 
#由于进行了端口绑定,所以直接 curl 192.168.136.129:8888即可访问

进入后台web容器,查看服务状态

[root@podman ~]# podman exec -it  web bash
root@3528e6d5148b:/# service nginx status
[ ok ] nginx is running.                             #运行中

怎样修改容器业务内容呢?很简单

#修改宿主机/opt/webhtml/index.html即可
[root@podman ~]# cat /opt/webhtml/index.html 
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambitionRHCAS
RHCE RHCA

#进行访问
[root@podman ~]# curl 192.168.136.129:8888
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambitionRHCAS 
RHCE RHCA

#进入容器查看内容是否修改
[root@podman ~]# podman exec -it web bash
root@3528e6d5148b:/# cat /usr/share/nginx/html/index.html 
Go your own way, see your own scenery, surpass others without complacency, and be surpassed without losing ambitionRHCAS 
RHCE RHCA

暂停与删除容器

#暂停
[root@podman ~]# podman stop web
web
[root@podman ~]# podman ps -a
CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS                     PORTS                 NAMES
3528e6d5148b  docker.io/library/nginx:latest  nginx -g daemon o...  25 minutes ago  Exited (0) 16 seconds ago  0.0.0.0:8888->80/tcp  web
#删除
[root@podman ~]# podman rm web
3528e6d5148bcf980f0df5708a82419d3485a33d1d16d722db3e880cc103cd2c
#或强制删除运行中的容器
[root@podman ~]# podman rm  -f web
3528e6d5148bcf980f0df5708a82419d3485a33d1d16d722db3e880cc103cd2c

7.web容器设置开机自启

后台运行一个web容器

[root@podman ~]# podman run --name web -d -p 8080:80 -v /opt/webhtml:/usr/shar/nginx/html nginx
910db3ab6bd1ef18e5fd0afe1844912f0b89334b7b8ab758353a948a1b55282a

基于web容器,在优先级一般的/etc/systemd/system内创建.service单元文件

[root@192 ~]# cd /etc/systemd/system/
[root@podman user]# podman generate systemd --
--container-prefix  (Systemd unit name prefix for containers)
--files             {生成.service文件,而不是打印到标准输出}
--format            (Print the created units in specified format (json)) #以指定的格式打印单元文件
--name              (Use container/pod names instead of IDs)  #创建新容器,而不是使用现有的容器
--new               (Create a new container instead of starting an existing one)#(跳过标头生成)
--no-header         (Skip header generation)
--pod-prefix        (Systemd unit name prefix for pods)
--restart-policy    (Systemd restart-policy)
--separator         (Systemd unit name separator between name/id and prefix)
--time              (Stop timeout override)
[root@192 system]# podman generate systemd --name web --files --new
/etc/systemd/system/container-web.service

查看生成的单元文件

[root@192 system]# cat container-web.service 
# container-web.service
# autogenerated by Podman 3.3.0-dev                                 #podman 3.3.0-dev自动生成
# Tue Aug 17 13:03:13 CST 2021                                      #8月17日星期二13:03:13 CST 2021
                                                            
[Unit]       #单元
Description=Podman container-web.service              #描述
Documentation=man:podman-generate-systemd(1)          #帮助以及生成的系统
Wants=network-online.target                           #网络
After=network-online.target
RequiresMountsFor=%t/containers                         #前面不重要直接跳过

[Service]
Environment=PODMAN_SYSTEMD_UNIT=%n                   
Restart=on-failure                  #故障时重新启动
TimeoutStopSec=70                   #超时时间    
ExecStart=/usr/bin/podman run --sdnotify=conmon --cgroups=no-conmon --rm --replace --name web -d -p 8080:80 -v /opt/webhtml:/usr/shar/nginx/html nginx   #执行开始为/usr/bin/podman  运行刚才创建的容器
Type=notify
NotifyAccess=all

[Install]
WantedBy=multi-user.target default.target

删除刚才的容器

[root@podman ~]# podman rm web
910db3ab6bd1ef18e5fd0afe1844912f0b89334b7b8ab758353a948a1b55282a
[root@podman ~]# podman ps -a
CONTAINER ID  IMAGE       COMMAND     CREATED     STATUS      PORTS       NAMES

设置开机自启

[root@192 ~]# systemctl daemon-reload 
[root@192 ~]# systemctl enable --now container-web.service 
Created symlink /etc/systemd/system/multi-user.target.wants/container-web.service → /etc/systemd/system/container-web.service.
Created symlink /etc/systemd/system/default.target.wants/container-web.service → /etc/systemd/system/container-web.service.

[root@192 user]# podman ps -a
CONTAINER ID  IMAGE                           COMMAND               CREATED         STATUS             PORTS                   NAMES
b0c7709cb00e  docker.io/library/nginx:latest  nginx -g daemon o...  15 seconds ago  Up 16 seconds ago  0.0.0.0:8080->80/tcp    web

无根root模式设置容器和上面这种方式大同小异

使用systemctl命令带上 --user 即可

#需要运行loginctl enable-linger命令,使用户服务在服务器启动时自动启动即可
[containers@serverb ~]$ loginctl enable-linger 
### 设置Docker容器开机动 为了使Docker容器能够在系统动时自动运行,需先确认Docker服务已经被设定为随操作系统动[^1]。这步骤至关重要,因为只有当Docker守护程序处于活动状态时,其管理下的任何容器才可能被激活。 对于Linux发行版而言,通常可以通过`systemctl enable docker`命令来实现这目标;而对于Windows或macOS平台,则应参照各自官方文档中的指导完成相应设置。之后,在确保Docker服务能够顺利开的基础上,还需要满足几个条件: - **前置依赖项准备**:如果有其他外部服务(比如数据库服务器或其他网络组件)是当前要设为自容器所必需的话,那么应当优先处理好这些基础架构层面的工作,使得它们能在目标容器尝试加载前就绪。 - **资源评估与规划**:考虑到实际环境中可能存在多台虚拟机实例或是多个应用同时竞争计算能力的情况,合理预估所需硬件规格以及预留适当余量是非常必要的。这样做不仅有助于提高整体性能表现,也能有效防止因过度消耗而导致的服务中断风险。 最后,针对具体的某个镜像文件创建持久化任务定义,以便每次机器重后都能按照既定策略执行对应的操作逻辑。具体来说就是利用如下所示的方式之来进行部署: #### 方法:借助Compose工具简化流程 如果项目结构较为复杂且涉及到了跨多个微服务之间的协作关系,推荐采用[Docker Compose](https://docs.docker.com/compose/)框架辅助构建环境。只需编辑份YAML格式描述文档即可次性声明所有关联实体及其属性参数,并指定其中哪些部分应该参与自动化过程。 ```yaml version: '3' services: webapp: image: my-web-app-image restart: always # 关键字用于指示该service始终尝试恢复至健康态 ``` 保存上述配置片段到名为`docker-compose.yml`的本地文件当中去,随后便可通过简单的CLI指令触发整个集群的次性初始化动作: ```shell $ docker-compose up -d ``` #### 方法二:直接修改容器动选项 当然也可以不引入额外层次而是在调用`docker run`的时候显式加入`--restart=always`标记位,从而达到相同的效果。这种方式更加直观易懂,适合初学者快速上手实践学习之用。 ```shell $ docker run --name some-mysql \ --restart=always \ # 此处即为控制行为的核心开关 -e MYSQL_ROOT_PASSWORD=my-secret-pw \ -d mysql:tag ``` 无论采取哪种途径实施改造措施,都建议定期审查现有方案的有效性和安全性状况,及时作出改进调整以适应不断变化的应用场景需求[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神慕蔡蔡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值