podman学习随笔

podman 基本使用方法

一、装包

[root@localhost ~]# yum module install podman

二、镜像基本操作

2.1 配置文件相关

[root@localhost ~]# vim /etc/containers/registries.conf

# 将地址添加到如下内容
[registries.search]
registries = ['需要添加的地址']

# 如果需要使用不安全地址的仓库,将地址添加到配置文件中的以下内容即可:
[registries.insecure]
registries = ['需要添加的地址']

2.2 镜像管理操作

# 查询仓库提供了哪些镜像,例如查询Nginx
[root@localhost ~]# podman search nginx  

# 下载镜像
[root@localhost ~]# podman pull docker.io/library/nginx
# 一般下载的容器存放目录在/var/lib/containers下

# 列出已有的镜像
[root@localhost ~]# podman images
REPOSITORY               TAG     IMAGE ID      CREATED      SIZE
docker.io/library/nginx  latest  08b152afcfae  2 weeks ago  137 MB

# 查看镜像配置文件
[root@localhost ~]# podman image inspect docker.io/library/nginx 

# 删除镜像
[root@localhost ~]# podman rmi docker.io/library/nginx:latest

# 备份镜像(导出)
[root@localhost ~]# podman save docker.io/library/nginx > /root/img-nginx.tar

# 导入
[root@localhost ~]# podman load -i /root/img-nginx.tar

# 如果仓库需要用户名密码验证
[root@localhost ~]# podman login 仓库地址

# 修改镜像的名称或标记
[root@localhost ~]# podman tag docker.io/library/nginx:latest  docker.io/library/nginx-new:latest 
[root@localhost ~]# podman images
REPOSITORY                   TAG     IMAGE ID      CREATED      SIZE
docker.io/library/nginx      latest  08b152afcfae  2 weeks ago  137 MB
docker.io/library/nginx-new  latest  08b152afcfae  2 weeks ago  137 MB

三、容器基本操作

# 检查当前运行的容器
[root@localhost ~]# podman ps
# 查看所有容器
[root@localhost ~]# podman ps -a

# 运行一个容器(-d 表示在后台运行一个容器 --name 用来指定运行容器的名称)
[root@localhost ~]# podman run -d --name myweb nginx

# 删除容器(5a为容器ID,运行中的容器需要添加-f选项,强制删除)
[root@localhost ~]# podman rm 5a
5a09f002139e4f00e6b10d32f00d86c5f59b70feca404e37c2faf87bda274541

# 真机端口(8000)与容器端口(80)映射
[root@localhost ~]# podman run -d -p 8000:80 nginx
# 验证端口映射是否成功
[root@localhost ~]# curl http://localhost:8000

# 进入到容器中执行命令
[root@localhost ~]# podman exec 8bac cat /etc/os-release 	

# 进入容器终端交互环境
[root@localhost ~]# podman exec -it 8bac bash
root@8bac18883f5a:/# 

# 将本机文件拷贝到容器中
[root@localhost ~]# podman cp /root/a.html 8bac1:/usr/share/nginx/html/index.html

# 将本机目录作为容器的网页目录
[root@localhost ~]# mkdir /opt/webtest
[root@localhost ~]# echo "podman webtest" > /opt/webtest/index.html
[root@localhost ~]# podman run -d -p 8001:80 -v /opt/webtest:/usr/share/nginx/html --name webtest nginx
[root@localhost ~]# curl http://localhost:8001
podman webtest

# 停止运行的容器
[root@localhost ~]# podman stop 8bac

# 启用停止运行的容器
[root@localhost ~]# podman start 8bac

# 查看容器的定义
[root@localhost ~]# podman container inspect 8bac

四、容器服务化

要求:通过systemd设置容器开机自启

4.1 知识前提

配置文件存放位置:

1、系统原始配置存放于 /usr/lib/systemd/system

2、一般管理员另外建的系统服务存放于 /etc/systemd/system

建议将容器启动的服务配置文件放置在etc目录下

4.2 建立服务的配置

[root@localhost ~]# cd /etc/systemd/system

[root@localhost system]# podman generate systemd --name webtest --files
/etc/systemd/system/container-webtest.service

[root@localhost system]# systemctl daemon-reload

4.3 通过服务方式启动容器

[root@localhost system]# systemctl start container-webtest.service

4.4 设置开机自启

[root@localhost system]# systemctl start container-webtest.service

五、rootless

目标:非root用户如何通过系统服务来管理容器

当前普通用户:

1、只能开1024以下的端口

2、配置目录在家目录下 ~/.config/systemd/user

3、普通用户需要用到容器需要单独下载

# 新增用户
[root@localhost ~]# useradd -d /home/podman podman 
[root@localhost ~]# echo 111111 | passwd --stdin podman

# ssh到普通用户
[root@localhost ~]# ssh podman@localhost
# 查看镜像,发现当前用户下没有镜像资源
[podman@localhost ~]$ podman images
REPOSITORY  TAG     IMAGE ID  CREATED  SIZE
[podman@localhost ~]$ 

# 下载资源
[podman@localhost ~]$ podman pull docker.io/library/nginx

[podman@localhost ~]$ podman images
REPOSITORY               TAG     IMAGE ID      CREATED      SIZE
docker.io/library/nginx  latest  08b152afcfae  3 weeks ago  137 MB
[podman@localhost ~]$ 

# 新建测试页
[podman@localhost ~]$ mkdir myserver-file
[podman@localhost ~]$ vim myserver-file/index.html
[podman@localhost ~]$ echo "podman8003" > myserver-file/index.html

# 启动podman的Nginx
[podman@localhost ~]$ podman run -d -p 8003:80 -v /home/podman/myserver-file:/usr/share/nginx/html --name podman-nginx nginx
9171784e6dfaff31e7820db0e9e66cfb8743345bf2c5a79fc63213c3263ec020

# 创建启动目录
[podman@localhost ~]$ mkdir -p ~/.config/systemd/user

# 创建配置文件
[podman@localhost user]$ podman generate systemd --name podman-nginx --files 
/home/pt/.config/systemd/user/container-podman-nginx.service

# 更新成我们的系统启动
[podman@localhost user]$ systemctl --user daemon-reload 


# 测试
[podman@localhost user]$ podman stop -l
[podman@localhost user]$ systemctl --user start container-podman-nginx.service
[podman@localhost user]$ curl http://podman-test:8003
podman8003

# 设置开机自启-方法1
[podman@localhost user]$ systemctl --user enable container-podman-nginx.service 
# 用户不登录的情况下保留启动资源
[podman@localhost user]$ loginctl enable-linger 
# 检查设置 Linger=yes
[podman@localhost user]$ loginctl show-user podman
...
...
Linger=yes

# 设置开机自启-方法2
[pt@podman-test ~]$ crontab -e
@reboot /usr/bin/systemctl --user restart container-podman-nginx.service

作者:likaifei

出处:https://www.cnblogs.com/likaifei/p/16707409.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   adai_kfl  阅读(70)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
more_horiz
keyboard_arrow_up light_mode palette
选择主题
点击右上角即可分享
微信分享提示