容器平台

  • 容器平台已关闭评论
  • 26 次浏览
  • A+
所属分类:linux技术
摘要

Podman(01)Install PodmanPodman:安装   安装容器管理工具的Podman。[1]  安装 Podman。

Podman

(01)Install Podman

Podman:安装


  安装容器管理工具的Podman。

[1]  安装 Podman。

root@dlp:~# apt -y install podman

 

[2]  下载官方镜像并创建一个容器,并在容器内输出 [Welcome to the Podman World] 字样。

# download the official image root@dlp:~# podman pull ubuntu # run echo inside a container root@dlp:~# podman run ubuntu /bin/echo "Welcome to the Podman World" Welcome to the Podman World Resolved "ubuntu" as an alias (/etc/containers/registries.conf.d/shortnames.conf) Trying to pull docker.io/library/ubuntu:latest... Getting image source signatures Copying blob 49b384cc7b4a done   | Copying config bf3dc08bfe done   | Writing manifest to image destination bf3dc08bfed031182827888bb15977e316ad797ee2ccb63b4c7a57fdfe7eb31d

 

[3]  使用 [i] 和 [t] 选项连接到容器的交互式会话,如下所示。

  如果 [exit] 退出容器会话,则容器的过程完成。

root@dlp:~# podman run -it ubuntu /bin/bash root@591e1ea31c34:/#     # connected root@591e1ea31c34:/# uname -a Linux 591e1ea31c34 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux root@591e1ea31c34:/# exit exit root@dlp:~#     # come back

 

[4]  如果要将容器作为守护进程运行,请添加 [d] 选项。

root@dlp:~# podman run -itd ubuntu /bin/bash # show podman processes root@dlp:~# podman ps a45d9a04eb16c35c28bf2c223a41613a99c4eaddba7e619decbe27e851eb906b   CONTAINER ID  IMAGE                            COMMAND     CREATED         STATUS         PORTS       NAMES a45d9a04eb16  docker.io/library/ubuntu:latest  /bin/bash   16 seconds ago  Up 16 seconds              angry_driscoll  # attach to container session root@dlp:~# podman exec -it a45d9a04eb16 /bin/bash root@a45d9a04eb16:/#     # connected root@a45d9a04eb16:/# exit # stop container process # * if force stop, specify [podman kill ***] root@dlp:~# podman stop a45d9a04eb16 root@dlp:~# podman ps CONTAINER ID  IMAGE   COMMAND  CREATED  STATUS  PORTS   NAMES

 

(02)Add Container Images

Podman:添加容器镜像


  添加修改设置的新容器映像。

[1]  例如,使用安装 [apache2] 更新官方镜像,并将其添加为新的容器镜像。

# show container images root@dlp:~# podman images REPOSITORY                TAG         IMAGE ID      CREATED     SIZE docker.io/library/ubuntu  latest      bf3dc08bfed0  5 days ago  78.7 MB  # run a container and install [apache2] root@dlp:~# podman run ubuntu /bin/bash -c "apt-get update; apt-get -y install apache2" root@dlp:~# podman ps -a | tail -1 # add the image that [apache2] was installed root@dlp:~# podman commit 8ba738311a1b srv.world/ubuntu-apache2 # show container images root@dlp:~# podman images # confirm [apache2] to run a container root@dlp:~# podman run srv.world/ubuntu-apache2 /usr/bin/whereis apache2 apache2: /usr/sbin/apache2 /usr/lib/apache2 /etc/apache2 /usr/share/apache2 8ba738311a1b  docker.io/library/ubuntu:latest  /bin/bash -c apt-...  26 seconds ago  Exited (0) 8 seconds ago              intelligent_haibt  Getting image source signatures Copying blob 80098e3d304c skipped: already exists Copying blob 8b942a48c2d6 done   | Copying config 38d5083368 done   | Writing manifest to image destination 38d5083368637cec96e051ebe22f2b5776538eb9ad7dc2143f4d617896ec05f4  REPOSITORY                TAG         IMAGE ID      CREATED         SIZE srv.world/ubuntu-apache2  latest      38d508336863  26 seconds ago  226 MB docker.io/library/ubuntu  latest      bf3dc08bfed0  5 days ago      78.7 MB

 

(03)容器服务接入

Podman:访问容器上的服务


  如果要访问在容器上作为守护程序运行的 HTTP 或 SSH 等服务,请按如下方式进行配置。

[1]  例如,使用已安装 [apache2] 的容器。

root@dlp:~# podman images # run a container and also start [apache2] # map with [-p xxx:xxx] to [(Host Port):(Container Port)] root@dlp:~# podman run -dt -p 8081:80 --security-opt apparmor=unconfined srv.world/ubuntu-apache2 /usr/sbin/apachectl -D FOREGROUND root@dlp:~# podman ps REPOSITORY                TAG         IMAGE ID      CREATED             SIZE srv.world/ubuntu-apache2  latest      38d508336863  About a minute ago  226 MB docker.io/library/ubuntu  latest      bf3dc08bfed0  5 days ago          78.7 MB  77c61575ad7b23c0091e0744cf691d8fb28484bd52a3be00280456e64309b191   CONTAINER ID  IMAGE                            COMMAND               CREATED         STATUS         PORTS                 NAMES 77c61575ad7b  srv.world/ubuntu-apache2:latest  /usr/sbin/apachec...  14 seconds ago  Up 14 seconds  0.0.0.0:8081->80/tcp  affectionate_mirzakhani  # create a test page root@dlp:~# podman exec 77c61575ad7b /bin/bash -c 'echo "Apache2 on Podman Container" > /var/www/html/index.html' # verify accesses root@dlp:~# curl localhost:8081 Apache2 on Podman Container # also possible to access via container network root@dlp:~# podman inspect -l | grep "IPAddress root@dlp:~# curl 10.88.0.7 Apache2 on Podman Container             "IPAddress": "10.88.0.7",                     "IPAddress": "10.88.0.7",

 

(04)使用Dockerfile

Podman:使用Dockerfile


  使用Dockerfile并自动创建容器映像。

  它对容器映像的配置管理也很有用。

[1]  例如,创建一个已安装并启动 Nginx 的 Dockerfile。

root@dlp:~# vi Dockerfile # create new FROM ubuntu MAINTAINER ServerWorld <[email protected]>  RUN apt-get update RUN apt-get -y install nginx RUN echo "Dockerfile Test on Nginx" > /var/www/html/index.html  EXPOSE 80 CMD ["/usr/sbin/nginx", "-g", "daemon off;"]  # build image ⇒ podman build -t [image name]:[tag] . root@dlp:~# podman build -t srv.world/ubuntu-nginx:latest . root@dlp:~# podman images # run container root@dlp:~# podman run -d -p 80:80 --security-opt apparmor=unconfined srv.world/ubuntu-nginx root@dlp:~# podman ps STEP 1/7: FROM ubuntu STEP 2/7: MAINTAINER ServerWorld <[email protected]> --> f2fc42956ea5 STEP 3/7: RUN apt-get update  ..... .....  STEP 6/7: EXPOSE 80 --> 79117f75b19a STEP 7/7: CMD ["/usr/sbin/nginx", "-g", "daemon off;"] COMMIT srv.world/ubuntu-nginx:latest --> 1b1a4d3f4f26 Successfully tagged srv.world/ubuntu-nginx:latest 1b1a4d3f4f26fdac6a7556c81a8cee65bfdbaaeff204ccc700f16b91d83e8dec  REPOSITORY                TAG         IMAGE ID      CREATED             SIZE srv.world/ubuntu-nginx    latest      1b1a4d3f4f26  About a minute ago  125 MB srv.world/ubuntu-apache2  latest      38d508336863  37 minutes ago      226 MB docker.io/library/ubuntu  latest      bf3dc08bfed0  5 days ago          78.7 MB  1628817a1fa315f96be582e324daf4fad31bc51f1ef015be6dd3f2d2750fb5fd   CONTAINER ID  IMAGE                          COMMAND               CREATED         STATUS         PORTS               NAMES 1628817a1fa3  srv.world/ubuntu-nginx:latest  /usr/sbin/nginx -...  18 seconds ago  Up 18 seconds  0.0.0.0:80->80/tcp  intelligent_chatterjee  # verify accesses root@dlp:~# curl localhost Dockerfile Test on Nginx # also possible to access via container network root@dlp:~# podman inspect -l | grep "IPAddress root@dlp:~# curl 10.88.0.2 Dockerfile Test on Nginx             "IPAddress": "10.88.0.2",                     "IPAddress": "10.88.0.2",

 

Dockerfile 的格式为 [INSTRUCTION arguments] 。

有关指令,请参阅以下说明。

指令 描述
FROM 它为后续指令设置基础映像。
MAINTAINER 它设置生成图像的“作者”字段。
RUN 它将在创建 Docker 映像时执行任何命令。
CMD 它将在执行 Docker 容器时执行任何命令。
ENTRYPOINT 它将在执行 Docker 容器时执行任何命令。
LABEL 它将元数据添加到图像中。
EXPOSE 它通知 Docker 容器将在运行时侦听指定的网络端口。
ENV 它设置环境变量。
ADD 它复制新文件、目录或远程文件 URL。
COPY 它复制新文件或目录。
[ADD]的区别在于无法指定远程URL,也不会自动提取存档文件。
VOLUME 它创建一个具有指定名称的挂载点,并将其标记为持有 从本机主机或其他容器外部挂载的卷。
USER 它设置用户名或 UID。
WORKDIR 它设置工作目录。

 

(05)使用外部存储

(06)使用外部存储(NFS)

(07)使用注册表

(08)Podman网络基础

(09)使用Docker CLI

(10)使用Docker Compose

(11)创建Pod

(12)普通用户使用

(13)生成Systemd单元文件

(14)容器资源使用情况