Containerd 常用命令

镜像操作

查看镜像

ctr -n k8s.io images list

拉取镜像

ctr -n k8s.io images pull <image_url>

# ctr -n k8s.io images pull --user <用户名>:<密码> registry.example.com/image:tag

给镜像重新打标签

ctr -n k8s.io images tag <old_tag> <new_tag>

导入镜像

ctr -n k8s.io images import <image.tar>

导出镜像

# 1. 导出为普通 .tar 文件
ctr -n k8s.io images export nginx.tar docker.io/library/nginx:latest

# 2. 用 gzip 压缩
gzip nginx.tar  # 生成 nginx.tar.gz

# 一步到位
ctr -n k8s.io images export - <image.tar> | gzip > image.tar.gz

容器操作

查看所有容器(包括运行中和已停止的)

ctr -n k8s.io containers list

仅查看运行中的容器

ctr -n k8s.io tasks list

查看容器的详细信息

ctr -n k8s.io containers info <容器ID或名称>

查看容器的日志

ctr -n k8s.io tasks logs <容器ID或名称>

实时查看日志(类似 docker logs -f)

ctr -n k8s.io tasks -f logs <容器ID或名称>

查看容器的进程

ctr -n k8s.io tasks ps <容器ID或名称>

1. 创建并启动新容器

ctr run -d <镜像名称> <容器名称>

# -d:后台运行(类似 Docker 的 -d)。
ctr run -d docker.io/library/nginx:latest nginx

启动容器(从现有容器启动)

ctr tasks start <容器ID或名称>
ctr tasks start nginx

2. 停止容器

ctr tasks kill <容器ID或名称>

强制停止(发送 SIGKILL):

ctr tasks kill -s SIGKILL nginx

优雅停止(发送 SIGTERM,默认):

ctr tasks kill nginx

3. 重启容器

ctr 没有直接重启命令,需先停止再启动:

ctr tasks kill nginx && ctr tasks start nginx

4. 删除容器

删除已停止的容器

ctr containers delete <容器ID或名称>
ctr containers delete nginx

强制删除运行中的容器

先停止任务,再删除容器:

ctr tasks kill nginx && ctr containers delete nginx

5. 其他常用操作

查看容器列表

ctr containers list      # 所有容器
ctr tasks list           # 运行中的容器

进入容器(需借助 runc

runc exec -it <容器ID> sh

Last updated