containerd使用
1.查看帮助
输入 ctr
命令即可获得所有相关的操作命令使用方式
$ ctr
NAME:
ctr -
__
_____/ /______
/ ___/ __/ ___/
/ /__/ /_/ /
\___/\__/_/
containerd CLI
USAGE:
ctr [global options] command [command options] [arguments...]
VERSION:
v1.5.9
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
2.镜像操作
2.1 拉取镜像 ctr image pull
containerd拉取镜像可以使用 ctr image pull
来完成,但是需要加上 docker.io
Host 地址
示例1
# docker方法
docker pull gitea/gitea:1.16.0
# containerd方法
ctr image pull docker.io/gitea/gitea:1.16.0
示例2
# 使用docker拉取镜像最后会提示镜像完整地址,containerd拉取镜像的时候必须加上这个路径
$ docker pull nginx:alpine
alpine: Pulling from library/nginx
59bf1c3509f3: Pull complete
...
40e5d2fe5bcd: Pull complete
Digest: sha256:eb05700fe7baa6890b74278e39b66b2ed1326831f9ec3ed4bdc6361a4ac2f333
Status: Downloaded newer image for nginx:alpine
docker.io/library/nginx:alpine
# 使用containerd拉取
ctr image pull docker.io/library/nginx:alpine
2.2 列出本地镜像 ctr image ls
$ ctr image ls
REF TYPE DIGEST SIZE PLATFORMS LABELS
docker.io/gitea/gitea:1.16.0 application/vnd.docker.distribution.manifest.list.v2+json sha256:67ccf27b427ec65fd7378d0999a3d94e9649f1953d2bb115864faa71ce7b9ec2 98.4 MiB linux/amd64,linux/arm64/v8 -
docker.io/library/nginx:alpine application/vnd.docker.distribution.manifest.list.v2+json sha256:da9c94bec1da829ebd52431a84502ec471c8e548ffb2cedbf36260fd9bd1d4d3 9.7 MiB linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x -
使用 -q(--quiet)
选项可以只打印镜像名称
$ ctr image ls -q
docker.io/gitea/gitea:1.16.0
docker.io/library/nginx:alpine
2.3 检测本地镜像 ctr image check
主要查看其中的 STATUS
,complete
表示镜像是完整可用的状态。
$ ctr image check
REF TYPE DIGEST STATUS SIZE UNPACKED
docker.io/gitea/gitea:1.16.0 application/vnd.docker.distribution.manifest.list.v2+json sha256:67ccf27b427ec65fd7378d0999a3d94e9649f1953d2bb115864faa71ce7b9ec2 complete (9/9) 98.4 MiB/98.4 MiB true
docker.io/library/nginx:alpine application/vnd.docker.distribution.manifest.list.v2+json sha256:da9c94bec1da829ebd52431a84502ec471c8e548ffb2cedbf36260fd9bd1d4d3 complete (7/7) 9.7 MiB/9.7 MiB true
2.4 给镜像重新打标签 ctr image tag
# 打标签
$ ctr image tag docker.io/library/nginx:alpine abc.com/def/nginx:alpine
abc.com/def/nginx:alpine
# 查看
$ ctr image ls -q
abc.com/def/nginx:alpine
docker.io/gitea/gitea:1.16.0
docker.io/library/nginx:alpine
2.5 删除镜像 ctr image rm
提示
加上 --sync
选项可以同步删除镜像和所有相关的资源
# 查看镜像
$ ctr image ls -q
abc.com/def/nginx:alpine
docker.io/gitea/gitea:1.16.0
docker.io/library/nginx:alpine
# 删除镜像
$ ctr image rm abc.com/def/nginx:alpine
abc.com/def/nginx:alpine
# 再次查看镜像
$ ctr image ls -q
docker.io/gitea/gitea:1.16.0
docker.io/library/nginx:alpine
2.6 将镜像挂载到主机目录 ctr image mount
# 挂载
ctr image mount docker.io/library/nginx:alpine /mnt
$ ls /mnt
bin dev docker-entrypoint.d docker-entrypoint.sh etc home lib media mnt opt proc root run sbin srv sys tmp usr var
$ tree -L 1 /mnt
/mnt
├── bin
├── dev
├── docker-entrypoint.d
├── docker-entrypoint.sh
├── etc
├── home
├── lib
├── media
├── mnt
├── opt
├── proc
├── root
├── run
├── sbin
├── srv
├── sys
├── tmp
├── usr
└── var
18 directories, 1 file
2.7 将镜像从主机目录上卸载 ctr image unmount
ctr image unmount /mnt
2.8 导出镜像 ctr image export
ctr image export --all-platforms nginx.tar.gz docker.io/library/nginx:alpine
执行命令报错
$ ctr image export --all-platforms nginx.tar.gz docker.io/library/nginx:alpine
ctr: content digest sha256:4bb6d6fd8bff453bece3b2dd033897ba8b4023bc686a57c50fae1b0668ae18f1: not found
解决方法是在pull镜像的时候加上 --all-platforms
选项
ctr i pull --all-platforms docker.io/library/nginx:alpine
2.9 导入镜像 ctr image import
# 查看镜像
$ ctr image ls -q
docker.io/gitea/gitea:1.16.0
# 导入镜像
$ ctr image import nginx.tar.gz
unpacking docker.io/library/nginx:alpine (sha256:da9c94bec1da829ebd52431a84502ec471c8e548ffb2cedbf36260fd9bd1d4d3)...done
# 再次查看镜像
$ ctr image ls -q
docker.io/gitea/gitea:1.16.0
docker.io/library/nginx:alpine
3.容器操作
3.1 查看容器操作帮助 ctr container
$ ctr container
NAME:
ctr containers - manage containers
USAGE:
ctr containers command [command options] [arguments...]
COMMANDS:
create create container
delete, del, rm delete one or more existing containers
info get info about a container
list, ls list containers
label set and clear labels for a container
checkpoint checkpoint a container
restore restore a container from checkpoint
OPTIONS:
--help, -h show help
3.2 创建容器 ctr container create
ctr container create docker.io/library/nginx:alpine nginx