Containerd容器镜像管理命令

  • docker使用docker images命令管理镜像
  • 单机containerd使用ctr images命令管理镜像,containerd本身的CLI
  • k8s中containerd使用crictl images命令管理镜像,Kubernetes社区的专用CLI工具

一、命令使用

[root@ceotos_7][15:36:10][OK] ~ 
#ctr --help
NAME:
   ctr - 
        __
  _____/ /______
 / ___/ __/ ___/
/ /__/ /_/ /
\___/\__/_/

containerd CLI


USAGE:
   ctr [global options] command [command options] [arguments...]

VERSION:
   v1.6.19

DESCRIPTION:
   
ctr is an unsupported debug and administrative client for interacting
with the containerd daemon. Because it is unsupported, the commands,
options, and operations are not guaranteed to be backward compatible or
stable from release to release of the containerd project.

COMMANDS:
   plugins, plugin            provides information about containerd plugins
   version                    print the client and server versions
   containers, c, container   manage containers
   content                    manage content
   events, event              display containerd events
   images, image, i           manage images
   leases                     manage leases
   namespaces, namespace, ns  manage namespaces
   pprof                      provide golang pprof outputs for containerd
   run                        run a container
   snapshots, snapshot        manage snapshots
   tasks, t, task             manage tasks
   install                    install a new package
   oci                        OCI tools
   shim                       interact with a shim directly
   help, h                    Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --debug                      enable debug output in logs
   --address value, -a value    address for containerd's GRPC server (default: "/run/containerd/containerd.sock") [$CONTAINERD_ADDRESS]
   --timeout value              total timeout for ctr commands (default: 0s)
   --connect-timeout value      timeout for connecting to containerd (default: 0s)
   --namespace value, -n value  namespace to use with commands (default: "default") [$CONTAINERD_NAMESPACE]
   --help, -h                   show help
   --version, -v                print the version

二、Containerd-镜像管理

2.1、查看镜像(五种方式均可查看)

[root@ceotos_7][15:36:57][OK] ~ 
#ctr i ls
REF TYPE DIGEST SIZE PLATFORMS LABELS 
[root@ceotos_7][16:09:19][OK] ~ 
#ctr image ls
REF TYPE DIGEST SIZE PLATFORMS LABELS 
[root@ceotos_7][16:09:28][OK] ~ 
#ctr image list
REF TYPE DIGEST SIZE PLATFORMS LABELS 
[root@ceotos_7][16:09:40][OK] ~ 
#ctr i list
REF TYPE DIGEST SIZE PLATFORMS LABELS 
[root@ceotos_7][16:09:46][OK] ~ 
#ctr images ls
REF TYPE DIGEST SIZE PLATFORMS LABELS

镜像也是有命名空间的(指定命名空间)

[root@ceotos_7][16:09:56][OK] ~ 
#ctr -n k8s.io image ls
REF                                                                     TYPE                                                      DIGEST                                                                  SIZE     PLATFORMS                                                                                               LABELS                          
docker.io/library/nginx:stable                                          application/vnd.docker.distribution.manifest.list.v2+json sha256:362b3204bf9c7252f41df91924b72f311a93c108e5bcb806854715c0efffd5f7 54.2 MiB linux/386,linux/amd64,linux/arm/v5,linux/arm/v7,linux/arm64/v8,linux/mips64le,linux/ppc64le,linux/s390x io.cri-containerd.image=managed 
sha256:8c9eabeac475449c72ad457ccbc014788a02dbbc64f24158b0a40fdc5def2dc9 application/vnd.docker.distribution.manifest.list.v2+json sha256:362b3204bf9c7252f41df91924b72f311a93c108e5bcb806854715c0efffd5f7 54.2 MiB linux/386,linux/amd64,linux/arm/v5,linux/arm/v7,linux/arm64/v8,linux/mips64le,linux/ppc64le,linux/s390x io.cri-containerd.image=managed 

2.2、下载镜像

containerd支持oci标准的镜像,所以可以直接使用docker官方或dockerfile构建的镜像

[root@ceotos_7][16:10:06][OK] ~ 
#ctr images pull --help
NAME:
   ctr images pull - pull an image from a remote

USAGE:
   ctr images pull [command options] [flags] <ref>

DESCRIPTION:
   Fetch and prepare an image for use in containerd.

After pulling an image, it should be ready to use the same reference in a run
command. As part of this process, we do the following:
1. Fetch all resources into containerd.
2. Prepare the snapshot filesystem with the pulled resources.
3. Register metadata for the image.
OPTIONS:
   --skip-verify, -k                 skip SSL certificate validation    # 跳过 SSL 证书验证
   --plain-http                      allow connections using plain HTTP  # 允许使用纯 HTTP 连接
   --user value, -u value            user[:password] Registry user and password  # 注册用户和密码
   --refresh value                   refresh token for authorization server  # 授权服务器的刷新令牌
   --hosts-dir value                 Custom hosts configuration directory  # 自定义主机配置目录
   --tlscacert value                 path to TLS root CA  # 值到 TLS 根 CA 的路径
   --tlscert value                   path to TLS client certificate  # 值 TLS 客户端证书的路径
   --tlskey value                    path to TLS client key  # 客户端密钥的 tlskey 值路径
   --http-dump                       dump all HTTP request/responses when interacting with container registry  #在与容器注册表交互时转储所有 HTTP 请求/响应
   --http-trace                      enable HTTP tracing for registry interactions  # 为注册表交互启用 HTTP 跟踪
   --snapshotter value               snapshotter name. Empty value stands for the default value. [$CONTAINERD_SNAPSHOTTER]  # 值快照程序名称。空值代表默认值。
   --label value                     labels to attach to the image  # 值标签附加到图像
   --platform value                  Pull content from a specific platform  # 从特定平台拉取内容
   --all-platforms                   pull content and metadata from all platforms  # 从所有平台中提取内容和元数据
   --all-metadata                    Pull metadata for all platforms  # 为所有平台拉取元数据
   --print-chainid                   Print the resulting image's chain ID   # 打印结果图像的链 ID
   --max-concurrent-downloads value  Set the max concurrent downloads for each pull (default: 0)  # 设置每次拉取的最大并发下载量 

2.2.1 指定(单个)平台下载

[root@node1 ~]# ctr images pull --platform  linux/amd64 docker.io/library/nginx:alpine
docker.io/library/nginx:alpine:                                                   resolved       |++++++++++++++++++++++++++++++++++++++| 
index-sha256:455c39afebd4d98ef26dd70284aa86e6810b0485af5f4f222b19b89758cabf1e:    done           |++++++++++++++++++++++++++++++++++++++| 
manifest-sha256:0f2ab24c6aba5d96fcf6e7a736333f26dca1acf5fa8def4c276f6efc7d56251f: done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:4342b1ab302e894161372b32fe2976899a978bf8ff2241fb1655dc25e6645a34:    done           |++++++++++++++++++++++++++++++++++++++| 
config-sha256:19dd4d73108a1feefc29d299f3727467ac02486c83474fc3979e4a7637291fe6:   done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:ca7dd9ec2225f2385955c43b2379305acd51543c28cf1d4e94522b3d94cce3ce:    done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:76a48b0f58980a64d28bc3575ae4733eb337f7b82403559122b13d5e2ced3921:    done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:2f12a0e7c01d607251a4040fa41518fd2542f3ebab83a6f7817867d0de111c96:    done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:1a7b9b9bbef6853211515e42f58be7763749950c244a0c485bb4afd1946e06d7:    done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:b704883c57afcf77f6bc48709943bcf808c9e9945d7e04926be41226fa415d33:    done           |++++++++++++++++++++++++++++++++++++++| 
elapsed: 8.6 s                                                                    total:  7.7 Mi (915.8 KiB/s)                                     
unpacking linux/amd64 sha256:455c39afebd4d98ef26dd70284aa86e6810b0485af5f4f222b19b89758cabf1e...
done: 488.54181ms 
[root@node1 ~]# uname -a
Linux node1 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

2.2.2 指定(全部)平台下载

[root@node1 ~]# ctr images pull --all-platforms docker.io/library/nginx:latest
..........
layer-sha256:5b221a36b4338b09410bbe89507e41d0b7f29bca528624270cdae477a994a020:    done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:fcd48e11f0ee5b433a823d2ce982c083cc16daf0de2c64acd8f58f0fee3b4abf:    done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:2c61dffb3feda2a72f267842bc181dda76c16a6902616dbf8379f2e2175aa046:    done           |++++++++++++++++++++++++++++++++++++++| 
elapsed: 38.7s                                                                    total:  395.6  (10.2 MiB/s)                                      
unpacking linux/amd64 sha256:d08d964023fe853b491e1f5eb182499653722c58cc4c294f2675f39d7c6a209d...
unpacking linux/arm/v5 sha256:d08d964023fe853b491e1f5eb182499653722c58cc4c294f2675f39d7c6a209d...
unpacking linux/arm/v7 sha256:d08d964023fe853b491e1f5eb182499653722c58cc4c294f2675f39d7c6a209d...
unpacking linux/arm64/v8 sha256:d08d964023fe853b491e1f5eb182499653722c58cc4c294f2675f39d7c6a209d...
unpacking linux/386 sha256:d08d964023fe853b491e1f5eb182499653722c58cc4c294f2675f39d7c6a209d...
unpacking linux/mips64le sha256:d08d964023fe853b491e1f5eb182499653722c58cc4c294f2675f39d7c6a209d...
unpacking linux/ppc64le sha256:d08d964023fe853b491e1f5eb182499653722c58cc4c294f2675f39d7c6a209d...
unpacking linux/s390x sha256:d08d964023fe853b491e1f5eb182499653722c58cc4c294f2675f39d7c6a209d...
done: 30.090253019s 

2.3 查看所有镜像

[root@ceotos_7][16:31:14][OK] ~ 
#ctr -n k8s.io image ls
REF                                                                     TYPE                                                      DIGEST                                                                  SIZE     PLATFORMS                                                                                               LABELS                          
docker.io/library/nginx:alpine                                          application/vnd.docker.distribution.manifest.list.v2+json sha256:207332a7d1d17b884b5a0e94bcf7c0f67f1a518b9bf8da6c2ea72c83eec889b8 15.9 MiB linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x                io.cri-containerd.image=managed 
docker.io/library/nginx:latest                                          application/vnd.docker.distribution.manifest.list.v2+json sha256:aa0afebbb3cfa473099a62c4b32e9b3fb73ed23f2a75a65ce1d4b4f55a5c2ef2 54.3 MiB linux/386,linux/amd64,linux/arm/v5,linux/arm/v7,linux/arm64/v8,linux/mips64le,linux/ppc64le,linux/s390x io.cri-containerd.image=managed 
docker.io/library/nginx:stable                                          application/vnd.docker.distribution.manifest.list.v2+json sha256:362b3204bf9c7252f41df91924b72f311a93c108e5bcb806854715c0efffd5f7 54.2 MiB linux/386,linux/amd64,linux/arm/v5,linux/arm/v7,linux/arm64/v8,linux/mips64le,linux/ppc64le,linux/s390x io.cri-containerd.image=managed 
sha256:2bc7edbc3cf2fce630a95d0586c48cd248e5df37df5b1244728a5c8c91becfe0 application/vnd.docker.distribution.manifest.list.v2+json sha256:207332a7d1d17b884b5a0e94bcf7c0f67f1a518b9bf8da6c2ea72c83eec889b8 15.9 MiB linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x                io.cri-containerd.image=managed 
sha256:8c9eabeac475449c72ad457ccbc014788a02dbbc64f24158b0a40fdc5def2dc9 application/vnd.docker.distribution.manifest.list.v2+json sha256:362b3204bf9c7252f41df91924b72f311a93c108e5bcb806854715c0efffd5f7 54.2 MiB linux/386,linux/amd64,linux/arm/v5,linux/arm/v7,linux/arm64/v8,linux/mips64le,linux/ppc64le,linux/s390x io.cri-containerd.image=managed 
sha256:904b8cb13b932e23230836850610fa45dce9eb0650d5618c2b1487c2a4f577b8 application/vnd.docker.distribution.manifest.list.v2+json sha256:aa0afebbb3cfa473099a62c4b32e9b3fb73ed23f2a75a65ce1d4b4f55a5c2ef2 54.3 MiB linux/386,linux/amd64,linux/arm/v5,linux/arm/v7,linux/arm64/v8,linux/mips64le,linux/ppc64le,linux/s390x io.cri-containerd.image=managed

2.4、挂载镜像

[root@node1 ~]# ctr images mount docker.io/library/nginx:latest /mnt
sha256:8b811a30cb94c227fb2ae61a2a1ec1e93381dbef06f9ea6b5c06df4f27651fed
/mnt
[root@node1 ~]# ls /mnt
bin  boot  dev  docker-entrypoint.d  docker-entrypoint.sh  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

2.5、卸载镜像

[root@node1 ~]# umount /mnt/
[root@node1 ~]# ls /mnt/

2.6、镜像导出

说明:

  • --all-platforms,导出所有平台镜像,本版本为1.6版本,1.4版本不需要添加此选项。

2.6.1、导出所有平台镜像

[root@node1 ~]# ctr i export --all-platforms nginx.img docker.io/library/nginx:latest
[root@node1 ~]# ls
etc  nginx.img  opt  usr

2.6.2、导出单个平台镜像

[root@node1 ~]# ctr i export --platform linux/amd64 nginx.img docker.io/library/nginx:latest
[root@node1 ~]# ll
总用量 183296
drwxr-xr-x  4 root root        51 10月 25 01:43 etc
-rw-r--r--  1 root root  56853504 11月 16 10:56 nginx.img
drwxr-xr-x  4 root root        35 10月 25 01:42 opt
drwxr-xr-x  3 root root        19 10月 25 01:41 usr

2.7、删除镜像

说明:

  • rm 、remove 、delete、del  四个命令都可以用来删除镜像
  • 多个镜像也可以一起删除,只需要加到命令后面
[root@node1 ~]# ctr images rm docker.io/library/nginx:alpine
docker.io/library/nginx:alpine
[root@node1 ~]# ctr i ls
REF                            TYPE                                                      DIGEST                                                                  SIZE     PLATFORMS                                                                                               LABELS 
docker.io/library/nginx:latest application/vnd.docker.distribution.manifest.list.v2+json sha256:d08d964023fe853b491e1f5eb182499653722c58cc4c294f2675f39d7c6a209d 54.2 MiB linux/386,linux/amd64,linux/arm/v5,linux/arm/v7,linux/arm64/v8,linux/mips64le,linux/ppc64le,linux/s390x -      
docker.io/library/nginx:stable application/vnd.docker.distribution.manifest.list.v2+json sha256:6f93c7c8b3ecc6ff99a743564c9701278d3f678bbe09d12dd3019bbb3d534f92 54.2 MiB linux/386,linux/amd64,linux/arm/v5,linux/arm/v7,linux/arm64/v8,linux/mips64le,linux/ppc64le,linux/s390x - 

删除所有镜像:

[root@node1 ~]# ctr i rm $(ctr i ls|grep -v REF)
docker.io/library/mysql:latest

2.8、导入镜像

[root@node1 ~]# ctr i ls
REF                            TYPE                                                      DIGEST                                                                  SIZE     PLATFORMS                                                                                               LABELS 
docker.io/library/nginx:stable application/vnd.docker.distribution.manifest.list.v2+json sha256:6f93c7c8b3ecc6ff99a743564c9701278d3f678bbe09d12dd3019bbb3d534f92 54.2 MiB linux/386,linux/amd64,linux/arm/v5,linux/arm/v7,linux/arm64/v8,linux/mips64le,linux/ppc64le,linux/s390x - 
[root@node1 ~]# ctr images import nginx.img
unpacking docker.io/library/nginx:latest (sha256:d08d964023fe853b491e1f5eb182499653722c58cc4c294f2675f39d7c6a209d)...done
[root@node1 ~]# ctr i ls
REF                            TYPE                                                      DIGEST                                                                  SIZE     PLATFORMS                                                                                               LABELS 
docker.io/library/nginx:latest application/vnd.docker.distribution.manifest.list.v2+json sha256:d08d964023fe853b491e1f5eb182499653722c58cc4c294f2675f39d7c6a209d 54.2 MiB linux/386,linux/amd64,linux/arm/v5,linux/arm/v7,linux/arm64/v8,linux/mips64le,linux/ppc64le,linux/s390x -      
docker.io/library/nginx:stable application/vnd.docker.distribution.manifest.list.v2+json sha256:6f93c7c8b3ecc6ff99a743564c9701278d3f678bbe09d12dd3019bbb3d534f92 54.2 MiB linux/386,linux/amd64,linux/arm/v5,linux/arm/v7,linux/arm64/v8,linux/mips64le,linux/ppc64le,linux/s390x - 

2.9、修改tag

语法:

  • ctr images tag 命令选项  源镜像tag  目标tag  目标tag  ....可以跟多个目标tag
[root@node1 ~]# ctr i tag docker.io/library/mysql:latest mysql:latest
mysql:latest
[root@node1 ~]# ctr i ls
REF                            TYPE                                                      DIGEST                                                                  SIZE      PLATFORMS                                                                                               LABELS 
docker.io/library/mysql:latest application/vnd.docker.distribution.manifest.list.v2+json sha256:25aace9734db96ae09c24c6a2eeb6db4720c41d493de352eb76007eddf437fbe 150.0 MiB linux/amd64,linux/arm64/v8                                                                              -      
docker.io/library/nginx:latest application/vnd.docker.distribution.manifest.list.v2+json sha256:d08d964023fe853b491e1f5eb182499653722c58cc4c294f2675f39d7c6a209d 54.2 MiB  linux/386,linux/amd64,linux/arm/v5,linux/arm/v7,linux/arm64/v8,linux/mips64le,linux/ppc64le,linux/s390x -      
docker.io/library/nginx:stable application/vnd.docker.distribution.manifest.list.v2+json sha256:6f93c7c8b3ecc6ff99a743564c9701278d3f678bbe09d12dd3019bbb3d534f92 54.2 MiB  linux/386,linux/amd64,linux/arm/v5,linux/arm/v7,linux/arm64/v8,linux/mips64le,linux/ppc64le,linux/s390x -      
mysql:latest                   application/vnd.docker.distribution.manifest.list.v2+json sha256:25aace9734db96ae09c24c6a2eeb6db4720c41d493de352eb76007eddf437fbe 150.0 MiB linux/amd64,linux/arm64/v8  
[root@node1 ~]# ctr i tag docker.io/library/mysql:latest mysql:12345 mysql:123
mysql:12345
mysql:123

三、Containerd容器管理

3.1、查看容器

[root@node1 ~]# ctr c ls
CONTAINER    IMAGE    RUNTIME    
[root@node1 ~]# ctr container ls
CONTAINER    IMAGE    RUNTIME    
[root@node1 ~]# ctr containers ls
CONTAINER    IMAGE    RUNTIME  

3.2、查看容器进程(任务)

[root@node1 ~]# ctr t ls
TASK    PID    STATUS    
[root@node1 ~]# ctr tasks ls
TASK    PID    STATUS    
[root@node1 ~]# ctr task ls
TASK    PID    STATUS   

3.3、创建静态容器

[root@node1 ~]# ctr containers create docker.io/library/nginx:latest nginx
ctr: image "docker.io/library/nginx:latest": not found
[root@node1 ~]# ctr images pull docker.io/library/nginx:latest
docker.io/library/nginx:latest:                                                   resolved       |++++++++++++++++++++++++++++++++++++++| 
index-sha256:e209ac2f37c70c1e0e9873a5f7231e91dcd83fdf1178d8ed36c2ec09974210ba:    done           |++++++++++++++++++++++++++++++++++++++| 
manifest-sha256:6ad8394ad31b269b563566998fd80a8f259e8decf16e807f8310ecc10c687385: done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:9802a2cfdb8d8504273e75f503a7c9fb4594782653b8252ec3073ae7b850a235:    done           |++++++++++++++++++++++++++++++++++++++| 
config-sha256:88736fe827391462a4db99252117f136b2b25d1d31719006326a437bb40cb12d:   done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:a603fa5e3b4127f210503aaa6189abf6286ee5a73deeaab460f8f33ebc6b64e2:    done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:c39e1cda007e48da53e4b20c928bcefa9e10958c7461c1ca645b5eed9a2ba029:    done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:90cfefba34d7c6a81fe1dfbb4a579998c65ff49092052967f63ddc48f6be85d9:    done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:a38226fb7abac764207dffedaee902fdf63c9d4ec076236fb632fe991c4d4b4f:    done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:62583498bae6886d90f3b1cbad2ebbeb68b66948161413087ff27b05cb75b994:    done           |++++++++++++++++++++++++++++++++++++++| 
elapsed: 10.7s                                                                    total:  54.2 M (5.1 MiB/s)                                       
unpacking linux/amd64 sha256:e209ac2f37c70c1e0e9873a5f7231e91dcd83fdf1178d8ed36c2ec09974210ba...
done: 2.371879085s 
[root@node1 ~]# ctr containers create docker.io/library/nginx:latest nginx
[root@node1 ~]# ctr containers ls
CONTAINER    IMAGE                             RUNTIME                  
nginx        docker.io/library/nginx:latest    io.containerd.runc.v2    
[root@node1 ~]# ctr tasks ls
TASK    PID    STATUS

说明:

  • 使用 ctr container create 命令创建容器后,容器并没有处于运行状态,其只是一个静态的容器。这个 container 对象只是包含了运行一个容器所需的资源及配置的数据结构,例如: namespaces、rootfs 和容器的配置都已经初始化成功了,只是用户进程(本案例为nginx)还没有启动。需要使用`ctr tasks`命令才能获取一个动态容器。
  • ctr 创建容器的时候本地必须要有镜像,否则报错

3.4、静态容器启动为动态容器 

[root@node1 ]# ctr tasks start nginx
ctr: failed to start shim: failed to resolve runtime path: runtime "io.containerd.runc.v2" binary not installed "containerd-shim-runc-v2": file does not exist: unknown
CTR:启动垫片失败:无法解析运行时路径:未安装运行时“io.containerd.runc.v2”二进制文件“containerd-shim-runc-v2”:文件不存在:未知
[root@node1 bin]# cp /root/usr/local/bin/containerd-shim-runc-v2 /usr/bin/
[root@node1 bin]# ls /usr/bin/ | grep containerd-shim-runc-v2
containerd-shim-runc-v2
# 启动task,即表时在容器中运行了进程,即为动态容器。
[root@node1 bin]# ctr tasks start -d nginx

3.4.1、查看容器宿主机进程

# 查看容器所在宿主机进程,是以宿主机进程的方式存在的。
[root@node1 bin]# ctr task ls 
TASK     PID     STATUS    
nginx    3356    RUNNING
# 查看容器的进程(都是物理机的进程)
[root@node1 bin]# ctr task ps nginx
PID     INFO
3356    -
3387    -
3388    -
# 物理机查看到相应的进程
[root@node1 bin]# ps aux | grep 3356
root       3356  0.0  0.3   8916  3488 ?        Ss   17:26   0:00 nginx: master process nginx -g daemon off;
root       3416  0.0  0.0 112824   988 pts/0    S+   17:29   0:00 grep --color=auto 3356

3.5、进入到容器中

[root@node1 bin]# ctr tasks exec  --exec-id 2 -t nginx2 /bin/sh

说明:

  • 为exec进程设定一个id,可以随意输入,只要保证唯一即可,也可使用$RANDOM变量。

3.6、运行一个动态容器

说明:

  •  -d   代表dameon,后台运行
  •  --net-host   代表容器的IP就是宿主机的IP(相当于docker里的host类型网络)
[root@node1 vod]# ctr run -d --net-host docker.io/library/nginx:alpine nginx
[root@node1 vod]# ctr t ls
TASK     PID     STATUS    
nginx    3582    RUNNING

3.6.1、进入容器

[root@node1 vod]# ctr task exec --exec-id 1 -t nginx /bin/sh
/ # ifconfig
ens32     Link encap:Ethernet  HWaddr 00:0C:29:DF:7E:67  
          inet addr:192.168.1.90  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::8449:8163:c2e:26fb/64 Scope:Link
          inet6 addr: fe80::e340:238:62a0:6413/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:39979 errors:0 dropped:0 overruns:0 frame:0
          TX packets:4733 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:4738148 (4.5 MiB)  TX bytes:496878 (485.2 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:56 errors:0 dropped:0 overruns:0 frame:0
          TX packets:56 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:4409 (4.3 KiB)  TX bytes:4409 (4.3 KiB)

/ # curl http://192.168.1.90
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
# 修改显示文件
/ # echo "nginx" > /usr/share/nginx/html/index.html 
/ # curl http://192.168.1.90
nginx
/ # exit
# 宿主机也可以访问
[root@node1 vod]# curl http://192.168.1.90
nginx 

3.7、暂停容器

[root@node1 ~]# ctr t ls
TASK      PID     STATUS    
nginx4    3187    RUNNING
[root@node1 ~]# ctr tasks --help
NAME:
   ctr tasks - manage tasks

USAGE:
   ctr tasks command [command options] [arguments...]

COMMANDS:
   attach                   attach to the IO of a running container
   checkpoint               checkpoint a container
   delete, del, remove, rm  delete one or more tasks
   exec                     execute additional processes in an existing container
   list, ls                 list tasks
   kill                     signal a container (default: SIGTERM)
   pause                    pause an existing container
   ps                       list processes for container
   resume                   resume a paused container
   start                    start a container that has been created
   metrics, metric          get a single data point of metrics for a task with the built-in Linux runtime

OPTIONS:
   --help, -h  show help
   
[root@node1 ~]# ctr tasks pause nginx4
[root@node1 ~]# ctr t ls
TASK      PID     STATUS    
nginx4    3187    PAUSED

3.8、恢复容器

[root@node1 ~]# ctr task resume nginx4
[root@node1 ~]# ctr t ls
TASK      PID     STATUS    
nginx4    3187    RUNNING

3.9、停止容器

# 使用kill命令停止容器中运行的进程,既为停止容器
[root@node1 ~]# ctr t ls
TASK      PID     STATUS    
nginx4    3187    RUNNING
[root@node1 ~]# ctr task kill nginx4
[root@node1 ~]# ctr t ls
TASK      PID     STATUS    
nginx4    3187    STOPPED

3.9.1、删除一个进程

# 必须先停止tasks或先删除task,再删除容器
[root@node1 ~]# ctr task delete nginx4
[root@node1 ~]# ctr c ls
CONTAINER    IMAGE                             RUNTIME                  
nginx4       docker.io/library/nginx:alpine    io.containerd.runc.v2  

注:

  • 查看静态容器,确认其还存在于系统中
  • 再次启动,容器即可恢复,如下:

 

[root@node1 ~]# ctr task start -d nginx4
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
[root@node1 ~]# ctr t ls
TASK      PID     STATUS    
nginx4    3416    RUNNING 

3.10、删除容器

注:删除容器之前,必须先停止进程,否则报错,删除容器后,进程也会一块删除!

[root@node1 ~]# ctr t ls
TASK      PID     STATUS    
nginx4    3416    RUNNING
[root@node1 ~]# ctr c ls
CONTAINER    IMAGE                             RUNTIME                  
nginx4       docker.io/library/nginx:alpine    io.containerd.runc.v2    
[root@node1 ~]# ctr container rm nginx4
ERRO[0000] failed to delete container "nginx4"           error="cannot delete a non stopped container: {running 0 0001-01-01 00:00:00 +0000 UTC}"
ctr: cannot delete a non stopped container: {running 0 0001-01-01 00:00:00 +0000 UTC}  # 无法删除未停止的容器
[root@node1 ~]# ctr task kill nginx4
[root@node1 ~]# ctr t ls
TASK      PID     STATUS    
nginx4    3416    STOPPED
[root@node1 ~]# ctr container delete nginx4
[root@node1 ~]# ctr c ls
CONTAINER    IMAGE    RUNTIME   
GitHub 加速计划 / co / containerd
16.89 K
3.34 K
下载
containerd 是一个容器运行时和镜像生成工具,用于管理容器化应用程序的生命周期管理。 * 容器化应用程序管理、容器运行时和编排工具 * 有什么特点:容器管理工具、支持多种容器化应用程序管理和部署工具、易于使用和集成
最近提交(Master分支:2 个月前 )
a4579956 Fix runtimeoptions location in v2 migration script 8 天前
66f72902 Revert "Disable vagrant strict dependency checking" 9 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐