容器镜像:运行容器的静态蓝图
我们要运行容器,必须要使用容器镜像。
容器镜像是什么呢?容器镜像就是包含了编码步骤的静态文件,充当创建0容器的蓝图。
接下来我们就要从容器注册表中来拉取或下载容器镜像到本地系统,来运行容器了。
容器注册表又是什么呢?容器注册表就是用于存储和检索容器镜像的存储库。
安装容器工具
首先,我们需要安装 container-tools 元软件包
sudo dnf install container-tools
这个软件包包含了 podman
、buildah
、skopeo
等核心工具,适用于容器镜像的拉取、构建、运行和管理。
配置容器注册表
1. 登录注册表
使用 podman login
登录到红帽容器注册表:
echo "PASSWORD" | podman login --username USERNAME --password-stdin registry.access.redhat.com
2. 配置 registries.conf
你可以在 ~/.config/containers/registries.conf 中自定义注册表设置:
unqualified-search-registries = ['registry.redhat.io', 'registry.access.redhat.com'][[registry]]
location = "registry.lab.example.com"
insecure = true
blocked = false
搜索、拉取和构建容器镜像
1. 搜索镜像
podman search 镜像名字
2. 检查镜像详情
skopeo inspect
3. 拉取镜像
podman pull 路径/镜像名字
4. 构建自定义镜像
创建一个 Containerfile
:
FROM registry.access.redhat.com/ubi8/ubi:latest
RUN dnf install -y python36
CMD ["/bin/bash", "-c", "sleep infinity"]
构建镜像:
podman build -t 新镜像名字:版本 工作目录路径
运行和管理容器
1. 创建并运行容器
podman run -d --name 容器名字 路径/引用的容器镜像名字
2. 查看容器状态
podman ps
podman ps -a
3.停止与删除容器
podman stop python3d
podman rm python3d
将容器配置为 systemd 用户服务
1. 创建专用用户(推荐)
sudo useradd appdev-adm
sudo passwd appdev-adm
2. 生成 systemd 服务文件
podman generate systemd --name 容器名称 --new --files
3. 移动服务文件并重新加载
mkdir -p ~/.config/systemd/user/
mv container-webserver1.service ~/.config/systemd/user/
systemctl --user daemon-reload
4. 启动并启用服务
systemctl --user start container-容器名字.service
systemctl --user enable container-容器名字.service
5. 启用用户级服务开机启动
loginctl enable-linger