镜像基本操作
Containerd 默认提供 CLI 命令行工具 ctr,ctr 命名提供基本的镜像和容器操作功能,可以通过如下查看命令帮助:
[root@localhost ~]# ctr -help
镜像基本操作主要是 ctr image 命令,查看命令帮助
[root@localhost ~]# ctr images -h
NAME:
ctr images - Manage images
USAGE:
ctr images command [command options] [arguments...]
COMMANDS:
check Check existing images to ensure all content is available locally
export Export images
import Import images
list, ls List images known to containerd
mount Mount an image to a target path
unmount Unmount the image from the target
pull Pull an image from a remote
push Push an image to a remote
prune Remove unused images
delete, del, remove, rm Remove one or more images by reference
tag Tag an image
label Set and clear labels for an image
convert Convert an image
usage Display usage of snapshots for a given image ref
OPTIONS:
--help, -h show help
拉取镜像
containerd支持oci标准的镜像,所以可以直接使用docker官方或dockerfile构建的镜像 需要注意的是,与docker不同,拉取镜像时要加上 ?docker.io/liarary
# 镜像名不能简写
[root@localhost ~]# ctr images pull docker.io/library/nginx:alpine
docker.io/library/nginx:alpine: resolved |++++++++++++++++++++++++++++++++++++++|
index-sha256:3923f8de8d2214b9490e68fd6ae63ea604deddd166df2755b788bef04848b9bc: done |++++++++++++++++++++++++++++++++++++++|
manifest-sha256:558b1480dc5c8f4373601a641c56b4fd24a77105d1246bd80b991f8b5c5dc0fc: done |++++++++++++++++++++++++++++++++++++++|
layer-sha256:d7fb62c2e1cc7510e9c63402d02061002604b6ab79deab339ee8abf9f7452fde: done |++++++++++++++++++++++++++++++++++++++|
config-sha256:01e5c69afaf635f66aab0b59404a0ac72db1e2e519c3f41a1ff53d37c35bba41: done |++++++++++++++++++++++++++++++++++++++|
layer-sha256:c926b61bad3b94ae7351bafd0c184c159ebf0643b085f7ef1d47ecdc7316833c: done |++++++++++++++++++++++++++++++++++++++|
layer-sha256:eb2797aa8e799e16f2a041cb7d709dc913519995a8a7dd22509d33c662612c5e: done |++++++++++++++++++++++++++++++++++++++|
layer-sha256:47df6ca4b6bc8e8c42f5fcb7ce4d37737d68cb5fb5056a54605deb2b0d33415b: done |++++++++++++++++++++++++++++++++++++++|
layer-sha256:5ea1ba8ab969c385f95c844167644f56aca56cc947548764033c92654d60a304: done |++++++++++++++++++++++++++++++++++++++|
layer-sha256:6a4b140a5e7cbbec14bdbc3d9e7eced3b5f87652515c1cb65af5abeb53fc9fa8: done |++++++++++++++++++++++++++++++++++++++|
layer-sha256:c99555e79d522323ea54a2e9e5c56c0bc5ed2fd7ffa16fa9cf06e5c231c15db8: done |++++++++++++++++++++++++++++++++++++++|
layer-sha256:f9302969eafdbfd15516462ec6f9a8c8b537abb385d938e2fb154c23998c3851: done |++++++++++++++++++++++++++++++++++++++|
elapsed: 8.1 s total: 16.1 M (2.0 MiB/s)
unpacking linux/amd64 sha256:3923f8de8d2214b9490e68fd6ae63ea604deddd166df2755b788bef04848b9bc...
done: 781.530658ms
指定平台
- –all-platforms:所有平台(amd64 、arm、386 、ppc64le 等),不加的话下载当前平台架构
- –platform:指定linux/amd64平台
[root@localhost ~]# ctr images pull --all-platforms docker.io/library/nginx:alpine
[root@localhost ~]# ctr images pull --platform linux/amd64 docker.io/library/nginx:alpine
查看镜像
- 查看镜像可以使用 i 简写或者image
- -q 只打印镜像名称
[root@localhost ~]# ctr images list
REF TYPE DIGEST SIZE PLATFORMS LABELS
docker.io/library/nginx:alpine application/vnd.docker.distribution.manifest.list.v2+json sha256:3923f8de8d2214b9490e68fd6ae63ea604deddd166df2755b788bef04848b9bc 17.1 MiB linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x -
[root@localhost ~]# ctr i ls
[root@localhost ~]# ctr i ls -q
检测镜像
[root@localhost ~]# ctr images check
REF TYPE DIGEST STATUS SIZE UNPACKED
docker.io/library/nginx:alpine application/vnd.docker.distribution.manifest.list.v2+json sha256:3923f8de8d2214b9490e68fd6ae63ea604deddd166df2755b788bef04848b9bc complete (9/9) 17.1 MiB/17.1 MiB true
主要查看其中的 STATUS,complete 表示镜像是完整可用的状态。
镜像挂载
把已下载的容器镜像挂载至当前文件系统,是为了方便查看镜像中包含的内容
[root@localhost ]# ctr images mount docker.io/library/nginx:alpine /tmp
sha256:01bb4fce3eb1b56b05adf99504dafd31907a5aadac736e36b27595c8b92f07f1
/tmp
查看挂载内容
[root@localhost tmp]# ll /tmp/
总用量 76
drwxr-xr-x 2 root root 4096 11月 30 17:32 bin
drwxr-xr-x 2 root root 4096 11月 30 17:32 dev
drwxr-xr-x 1 root root 4096 12月 1 15:02 docker-entrypoint.d
-rwxrwxr-x 1 root root 1620 12月 1 15:02 docker-entrypoint.sh
drwxr-xr-x 1 root root 4096 12月 1 15:02 etc
drwxr-xr-x 2 root root 4096 11月 30 17:32 home
drwxr-xr-x 1 root root 4096 12月 1 15:02 lib
drwxr-xr-x 5 root root 4096 11月 30 17:32 media
drwxr-xr-x 2 root root 4096 11月 30 17:32 mnt
drwxr-xr-x 2 root root 4096 11月 30 17:32 opt
dr-xr-xr-x 2 root root 4096 11月 30 17:32 proc
drwx------ 2 root root 4096 11月 30 17:32 root
drwxr-xr-x 2 root root 4096 11月 30 17:32 run
drwxr-xr-x 2 root root 4096 11月 30 17:32 sbin
drwxr-xr-x 2 root root 4096 11月 30 17:32 srv
drwxr-xr-x 2 root root 4096 11月 30 17:32 sys
drwxrwxrwt 1 root root 4096 12月 1 15:02 tmp
drwxr-xr-x 1 root root 4096 11月 30 17:32 usr
drwxr-xr-x 1 root root 4096 11月 30 17:32 var
卸载
[root@localhost ]# ctr images unmount /tmp
镜像导入导出
导出
- 同时导出可以使用–platform导出其它平台的(例如arm)
- –all-platforms为导出所有平台
[root@localhost ]# ctr image export --all-platforms mynginx.tar docker.io/library/nginx:alpine
导入
删除本地镜像
[root@localhost ]# ctr images rm docker.io/library/nginx:alpine
导入
[root@localhost ]# ctr images import mynginx.tar
unpacking docker.io/library/nginx:alpine (sha256:3923f8de8d2214b9490e68fd6ae63ea604deddd166df2755b788bef04848b9bc)...done
导入所有平台
[root@localhost ]# ctr images import --all-platforms mynginx.tar
unpacking docker.io/library/nginx:alpine (sha256:3923f8de8d2214b9490e68fd6ae63ea604deddd166df2755b788bef04848b9bc)...done
导入可能会出现类似于 ctr: content digest sha256:xxxxxx not found 的错误,要解决这个办法需要 pull 所有平台镜像:
[root@localhost ]# ctr i pull --all-platforms docker.io/library/nginx:alpine
[root@localhost ]# ctr i export --all-platforms mynginx.tar docker.io/library/nginx:alpine
[root@localhost ]# ctr i rm docker.io/library/nginx:alpine
[root@localhost ]# ctr i import mynginx.tar
删除镜像
[root@localhost ]# ctr images rm docker.io/library/nginx:alpine
镜像打标签
[root@localhost ~]# ctr images tag docker.io/library/nginx:alpine docker.io/bongmu/nginx:alpine
推送镜像
[root@localhost ~]# ctr images push docker.io/bongmu/nginx:alpine
index-alpine@sha256:3923f8de8d2214b9490e68fd6ae63ea604deddd166df2755b788bef04848b9bc: done |++++++++++++++++++++++++++++++++++++++|
manifest-sha256:bf575af9dafa90e26c6827b3b9cb2f87900a2a67a899d0fc01023e992cadbbde: done |++++++++++++++++++++++++++++++++++++++|
manifest-sha256:558b1480dc5c8f4373601a641c56b4fd24a77105d1246bd80b991f8b5c5dc0fc: done |++++++++++++++++++++++++++++++++++++++|
manifest-sha256:4937f49e504c3b0a082eec0c9c336315ba10dbd2fa1800575f84a0a69ee9a4d4: done |++++++++++++++++++++++++++++++++++++++|
manifest-sha256:9f6dc3ad07da76ccf600ee39d0524266bbef01414a31466ef67d0b172448c531: done |++++++++++++++++++++++++++++++++++++++|
manifest-sha256:18d2bb20c22e511b92a3ec81f553edfcaeeb74fd1c96a92c56a6c4252c75eec7: done |++++++++++++++++++++++++++++++++++++++|
manifest-sha256:c2b754e66172ca0ca15b9ee2290a2e1b921ceb0a643019a49aa9c68a1dedf4dc: done |++++++++++++++++++++++++++++++++++++++|
manifest-sha256:aed771cbe3afa57b9ba1fbdd83a45b35792ae08046db29dd306fbc411bfc9396: done |++++++++++++++++++++++++++++++++++++++|
config-sha256:92b9e48381c41c7ec99a82b7056f6a194606b241788ca752f0496aa07b538d9d: done |++++++++++++++++++++++++++++++++++++++|
config-sha256:7cae28957771c6e97257fd5e25334c9c3d80fe07781688c7a14f2bb63790f54a: done |++++++++++++++++++++++++++++++++++++++|
config-sha256:0b76d6a71b9e5058ce30cad1e0e03fad54a2d6a31fa0d539312fcce133937b6a: done |++++++++++++++++++++++++++++++++++++++|
config-sha256:01e5c69afaf635f66aab0b59404a0ac72db1e2e519c3f41a1ff53d37c35bba41: done |++++++++++++++++++++++++++++++++++++++|
config-sha256:561adbe2d8b3a9ad5ef35d0661133a4fa0fb665e3d1a75a0c16333ae9494ff90: done |++++++++++++++++++++++++++++++++++++++|
config-sha256:f09fc93534f6a80e1cb9ad70fe8c697b1596faa9f1b50895f203bc02feb9ebb8: done |++++++++++++++++++++++++++++++++++++++|
config-sha256:aa2502e079292fd7c12af367afba401c53dfe37d4eec74693a8dda264be75d70: done |++++++++++++++++++++++++++++++++++++++|
elapsed: 4.5 s
推送镜像到带认证的镜像仓库
[root@localhost ~]# ctr images push --user bongmu:xx docker.io/bongmu/nginx:alpine
命名空间操作
containerd相比于docker,多了namespace概念,每个image和containe都会在各自的namespace下可见。
[root@localhost ~]# ctr namespace -h
NAME:
ctr namespaces - Manage namespaces
USAGE:
ctr namespaces command [command options] [arguments...]
COMMANDS:
create, c Create a new namespace
list, ls List namespaces
remove, rm Remove one or more namespaces
label Set and clear labels for a namespace
OPTIONS:
--help, -h show help
查看命名空间
[root@localhost ~]# ctr namespaces ls
NAME LABELS
default
创建命名空间
[root@localhost ~]# ctr namespaces create test
删除命名空间
[root@localhost ~]# ctr namespaces rm test
默认containerd会使用default命名空间,Kubernetes 下使用的 containerd 默认命名空间是 ?k8s.io,Docker 使用的 containerd 下面的命名空间默认是 moby。下载镜像的时候命名空间只如果不指定,默认是看不到,这点和k8s namespace作用相同。有了命名空间后就可以在操作资源的时候指定 namespace。
[root@localhost ~]# ctr -n test images pull --all-platforms docker.io/library/nginx:alpine
查看命名空间镜像
[root@localhost ~]# ctr images ls
REF TYPE DIGEST SIZE PLATFORMS LABELS
[root@localhost ~]#
[root@localhost ~]# ctr -n test images ls
REF TYPE DIGEST SIZE PLATFORMS LABELS
docker.io/library/nginx:alpine application/vnd.docker.distribution.manifest.list.v2+json sha256:3923f8de8d2214b9490e68fd6ae63ea604deddd166df2755b788bef04848b9bc 17.1 MiB linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/s390x -
下一篇文章将介绍Containerd容器管理,请尽情期待!别忘了留个关注哟!