首页 > 其他分享 >podman学习随笔

podman学习随笔

时间:2022-09-19 13:33:52浏览次数:56  
标签:容器 nginx 学习 podman user 随笔 root localhost

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

标签:容器,nginx,学习,podman,user,随笔,root,localhost
From: https://www.cnblogs.com/likaifei/p/16707409.html

相关文章

  • 为什么Kubernetes和容器与机器学习密不可分?
    原文出自infosecurity作者:RebeccaJames京东云开发者社区编译当前,数字化转型的热潮在IT领域发展的如火如荼,越来越多的企业投身其中,机器学习和人工智能等现代技术的融合......
  • 随笔
    感觉到大学与高中有一点不同:大学可能是个对集体生活能力与意愿有所要求的“小社会”(所以无法或者不愿适应集体生活而退学的情况也可以理解)。高中的“集体生活”也许也有,但......
  • redis学习笔记
    Redis一、rhel7安装redis6.0.6[[email protected]]#cat/etc/redhat-releaseRedHatEnterpriseLinuxServerrelease7.6(Maipo)1、下载安装包地址:https:......
  • 什么是合奏技术?让我们用柠檬语言学习吧。✔
    什么是合奏技术?让我们用柠檬语言学习吧。✔什么是合奏技巧?每当我们有大量数据时,或者我们可以说,每当我们在数据集中有大量行和列时,我们使用技术集。例如:-假设我们有......
  • 机器学习模型
    机器学习模型在MySkill继续学习,最后教授的材料是关于机器学习的。在这篇文章中,我将分享机器学习的一个概念或一个粗略的想法。我想分享一下如何一步一步地在python中构......
  • Prometheus学习路线概览
    第一章:Prometheus基础概述通过一个简单案例(使用Prometheus采集主机的监控数据)来了解Prometheus是什么,能做什么,以及它的架构组成。通过阅读本章希望读者能对Promentheus有......
  • Day01学习java的第一太难
    Makdown学习标题字体helloworld!helloworld!helloworld!分割线 引用我是你爹图片超链接我是你亲列表abcabc表格名字性别生......
  • Java学习第二天-快捷键
    快捷键Ctrl+CCtrl+VCtrl+ACtrl+XCtrl+ZCtrl+SAlt+F4Shift+Deleteexplorer打开CMD的方式开始+系统+命令提示符Win键+R,输入cmd打开命令提示符(推荐)任意文......
  • Electron学习(四)之应用程序打包
    如何将应用程序打包(Win)1、关于package.js文件详解完整实例如下:"build":{"productName":"xxxx",//项目名这也是生成的exe文件的前缀名"appId":"com.leon.x......
  • Electron学习(三)之简单交互操作
    点击按钮可以打开另一个界面按钮及界面都需要样式引入样式安装bootstrap命令如下:npminstallbootstrap--save点击按钮可以打开另一个界面在根目录下创建一个名为r......