Containerd 的镜像和容器管理

一.  镜像管理:

        1.  查看镜像列表:

[root@node2 ~]# ctr images/image/i ls

        2.  拉取镜像:

[root@node2 ~]# ctr images pull --platform linux/amd64 docker.io/library/busybox:latest

--platform:指定要拉取得镜像的平台

        3.  镜像挂载,把已下载的容器镜像挂载至宿主机的目录中:

命令格式:ctr images mount 镜像名 挂载的目录

[root@node2 ~]# ctr images mount docker.io/library/busybox:latest /mnt

        4.  卸载:

[root@node2 ~]# umount /mnt

        5.  镜像导出:

## 下载镜像
[root@node2 ~]# ctr images pull --all-platforms docker.io/library/busybox:latest

--all-platforms:表示下载所有平台的busybox:latest镜像

## 导出镜像
[root@node2 ~]# ctr images export --all-platforms nginx-alpine.img(导出的文件名) 
docker.io/library/nginx:alpine(要导出的镜像)

        6.  镜像导入:

[root@node2 ~]# ctr images import nginx-alpine.img(要导入的镜像文件)

注意:直接导入镜像可能会出现类似"ctr: content digest sha256:xxxxxxx not found"的错误,因此在拉取和导出镜像时,都要指定" --all-platforms "参数再导入镜像。

        7.  删除镜像:

[root@node2 ~]# ctr images rm docker.io/library/busybox:latest

        8.  给镜像打标记tag:

[root@node2 ~]# ctr images tag docker.io/library/busybox:latest busybox:latest

        再次查看镜像列表:

 二.  容器管理:

        1.  静态容器命令查询:

[root@node2 ~]# ctr containerd --help

[root@node2 ~]# ctr c -h

        2.  查看容器列表:

[root@node2 ~]# ctr container/containers/c ls

        3.  查看任务列表:当容器运行起来的时候才会产生任务。

[root@node2 ~]# ctr task/task/t ls

        4.  创建静态容器:

[root@node2 ~]# ctr c create busybox:latest busybox

[root@node2 ~]# ctr c ls
CONTAINER    IMAGE             RUNTIME                  
busybox      busybox:latest    io.containerd.runc.v2

        5.  启动静态容器,启动task,即表时在容器中运行了进程,即为动态容器。:

[root@node2 ~]# ctr t start -d busybox(容器名)

[root@node2 ~]# ctr t ls
TASK       PID     STATUS    
busybox    1510    RUNNING

-d:表示daemon或者后台的意思,否则会卡住终端

        6.  进入到动态容器:

[root@node2 ~]# ctr -n default task exec --exec-id $RANDOM -t busybox sh

-n:表示指定命名空间
-t:表示提供一个终端

        7.  查看容器详细信息:

[root@node2 ~]# ctr c info busybox

        8.  直接运行一个动态容器:

[root@node2 ~]# ctr run -d --net-host busybox:latest busybox2

-d:表示dameon,后台运行
--net-host:表示容器的IP就是宿主机的IP(相当于docker里的host类型网络)

## 查看容器列表:
[root@node2 ~]# ctr c ls
CONTAINER    IMAGE             RUNTIME                  
busybox      busybox:latest    io.containerd.runc.v2    
busybox2     busybox:latest    io.containerd.runc.v2 

## 查看任务列表:
[root@node2 ~]# ctr t ls
TASK        PID     STATUS    
busybox     1510    RUNNING
busybox2    1637    RUNNING


## 进入到容器
[root@node2 ~]# ctr -n default task exec --exec-id $RANDOM -t busybox2 sh

        9.  暂停容器:

[root@node2 ~]# ctr t pause busybox2

[root@node2 ~]# ctr t ls
TASK        PID     STATUS    
busybox     1510    RUNNING
busybox2    1637    PAUSED

        10.  恢复容器:

[root@node2 ~]# ctr t resume busybox2

[root@node2 ~]# ctr t ls
TASK        PID     STATUS    
busybox2    1637    RUNNING
busybox     1510    RUNNING

        11.  停止容器:

## 使用kill命令停止容器中运行的进程,既为停止容器
[root@node2 ~]# ctr tasks kill busybox2

## 查看容器停止后状态,STATUS为STOPPED
TASK      PID      STATUS
busybox 10660     STOPPED
busybox2 9823     RUNNING

        12.  删除容器:

[root@node2 ~]# ctr t delete busybox2  ## 删除已经停止的容器

[root@node2 ~]# ctr t delete -f busybox2  ## 强制删除容器,包括正在运行的容器

## 查看任务列表:
[root@node2 ~]# ctr t ls
TASK       PID     STATUS    
busybox    1510    RUNNING

## 查看容器列表:
[root@node2 ~]# ctr c ls
CONTAINER    IMAGE             RUNTIME                  
busybox      busybox:latest    io.containerd.runc.v2    
busybox2     busybox:latest    io.containerd.runc.v2

        13.  查看命名空间列表:

[root@node2 ~]# ctr ns ls
NAME    LABELS 
default   

        14.  创建命名空间:

[root@node2 ~]# ctr ns c it(命名空间名称)

[root@node2 ~]# ctr ns ls
NAME    LABELS 
default        
it 

        15.  删除命名空间:

[root@node2 ~]# ctr ns rm it

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值