【containerd】Containerd高阶命令行工具nerdctl

news2024/11/23 6:21:09

前言

对于习惯了使用docker cli的用户来说,containerd的命令行工具ctr使用起来不是很顺手,此时别慌,还有另外一个命令行工具项目nerdctl可供我们选择。 nerdctl是一个与docker cli风格兼容的containerd的cli工具。 nerdctl已经作为子项目加入了containerd项目,它的github地址是https://github.com/containerd/nerdctl,而且从最近的nerdctl 0.8开始,nerdctl直接兼容了docker compose的语法(不包含swarm), 这很大提高了直接将containerd作为本地开发、测试和单机容器部署使用的体验。本来k8s后续将不再支持dockershim,docker在k8s社区的地位急剧下降,现在单机直接使用containerd易用性也不断被完善,也许docker的辉煌已经远去了。

实际上nerdctl compose实现的是Compose Specification规范, 这个规范是从自Docker Compose file version 3 specification规范发展而来的。

安装nerdctl

本章节在上一章节的基础下进行,containerd都已安装并启动完成。nerdctl下载链接:https://github.com/containerd/nerdctl/releases/tag/v1.7.6
nerdctl 官方发布包含两个安装版本:

  • Minimal:仅包含 nerdctl 二进制文件及 rootless 模式下的辅助安装脚本;
  • Full:全量包,其中包含了 Containerd、CNI、runc、BuildKit 等完整组件。

如下:
在这里插入图片描述

下载nerdctl

注意:安装 nerdctl-full 版本集成了 containerd 。由于我之前虽然安装了containerd,但是没有安装其他插件如CNI、BuildKit 等。我还是选择了安装nerdctl-full版本

[root@kube-master ~]# wget https://github.com/containerd/nerdctl/releases/download/v1.7.6/nerdctl-full-1.7.6-linux-amd64.tar.gz

解压安装

[root@kube-master ~]# tar -xvf v1.7.6/nerdctl-full-1.7.6-linux-amd64.tar.gz -C /usr/local

验证查看

[root@kube-master ~]# ctr version
Client:
  Version:  v1.7.18
  Revision: ae71819c4f5e67bb4d5ae76a6b735f29cc25774e
  Go version: go1.21.11

Server:
  Version:  1.6.33
  Revision: d2d58213f83a351ca8f528a95fbd145f5654e957
  UUID: 2bf0456c-c052-4d37-83b7-3d58cf632b91
WARNING: version mismatch
WARNING: revision mismatch
[root@kube-master ~]# nerdctl version
WARN[0000] unable to determine buildctl version: exec: "buildctl": executable file not found in $PATH 
Client:
 Version:       v1.7.6
 OS/Arch:       linux/amd64
 Git commit:    845e989f69d25b420ae325fedc8e70186243fd93
 buildctl:
  Version:

Server:
 containerd:
  Version:      1.6.33
  GitCommit:    d2d58213f83a351ca8f528a95fbd145f5654e957
 runc:
  Version:      1.1.12
  GitCommit:    v1.1.12-0-g51d5e946

nerdctl使用

nerdctl 是 containerd 的命令行界面的工具。nerdctl 兼容 docker ,如果会使用 docker-cli 就等于掌握了 nerdctl 80% 的使用方法。nerdctl 不但兼容docker-cli 甚至还兼容了 docker-compose的功能点。

更名docker

甚至可以直接将nerdctl更名为 docker,注意如果服务器本身安装了docker需要注意到底使用的哪个目录的。

[root@kube-master ~]# cat << 'EOF' > /usr/local/bin/docker
> #!/bin/bash
> /usr/local/bin/nerdctl $@
> EOF
[root@kube-master ~]# chmod +x /usr/local/bin/docker

nerdctl bash自动补全

[root@kube-master ~]# yum install bash-completion -y
[root@kube-master ~]# nerdctl completion bash > /etc/bash_completion.d/nerdctl
[root@kube-master ~]# source /etc/bash_completion.d/nerdctl

上面补全的是 nerdctl 的命令,而当 nerdctl 重命名 docker 后,没有 docker 的自动补全。

添加 docker 别名的自动补全:
生成自动补全文件

[root@kube-master ~]#nerdctl completion bash > /etc/bash_completion.d/docker

生效

[root@kube-master ~]# source /etc/bash_completion.d/nerdctl
[root@kube-master ~]# source /etc/bash_completion.d/docker

测试
输入 docker image + 两下 tab

[root@kube-master ~]# nerdctl image
image   (Manage images)  images  (List images) 

安装常用插件

安装docker常用扩展插件
[root@kube-master ~]# docker run -it registry.cn-beijing.aliyuncs.com/k7scn/tools bash
Unable to find image 'registry.cn-beijing.aliyuncs.com/k7scn/tools:latest' locally
latest: Pulling from k7scn/tools
88ecf269dec3: Pull complete 
7e3e2c929b89: Pull complete 
dedb8fce9b84: Pull complete 
Digest: sha256:71442d19f1f35271a66de5c9d1f869c61946b25b991544b99b1de3cc2a9129c3
Status: Downloaded newer image for registry.cn-beijing.aliyuncs.com/k7scn/tools:latest
bash-5.2# 
bash-5.2# ls
bin      dev      etc      home     lib      media    mnt      opt      pkg.tgz  proc     root     run      sbin     srv      sys      tmp      usr      var
bash-5.2# mkdir /sysdir
bash-5.2# tar xf pkg.tgz -C /sysdir/
bash-5.2# cd /sysdir/
bash-5.2# ls
cclear          ctop            docker-compose  ergoget         iclear          kdtoken         upgrade-tools
crictl          din             dps             helminit        kbtoken         scope

可以看到有很多常用的命令,下面我们通过一条命令直接拷贝到本地。

[root@kube-master cni]# nerdctl run --rm -v /usr/local/bin:/sysdir registry.cn-beijing.aliyuncs.com/k7scn/tools tar zxf /pkg.tgz -C /sysdir
FATA[0000] failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error running hook #0: error running hook: exit status 1, stdout: , stderr: time="2024-06-20T14:32:18+08:00" level=fatal msg="failed to call cni.Setup: plugin type=\"bridge\" failed (add): incompatible CNI versions; config is \"1.0.0\", plugin supports [\"0.1.0\" \"0.2.0\" \"0.3.0\" \"0.3.1\"]"
Failed to write to log, write /var/lib/nerdctl/1935db59/containers/default/4cb2dd96d087592d4b69a09e3c992e52e3bbb90c226df698887eedd015565095/oci-hook.createRuntime.log: file already closed: unknown 

可以看到在拷贝过程中出现了问题,在网上找了一下资料解决。如下:

# 下载包
[root@kube-master ~]# wget https://github.com/containernetworking/plugins/releases/download/v1.1.1/cni-plugins-linux-amd64-v1.1.1.tgz
# 备份一下驱动
[root@kube-master ~]# mv /opt/cni/bin /opt/cni/bin.bak
# 解压到指定目录
[root@kube-master ~]# tar -zxvf cni-plugins-linux-amd64-v1.1.1.tgz -C /opt/cni/bin        
#然后再次执行拷贝
[root@kube-master ~]# nerdctl run --rm -v /usr/local/bin:/sysdir registry.cn-beijing.aliyuncs.com/k7scn/tools tar zxf /pkg.tgz -C /sysdir

执行完成后,就已经拷贝到 /usr/local/bin 目录下。

镜像管理

查看镜像

[root@kube-master cni]# nerdctl images
REPOSITORY                                      TAG       IMAGE ID        CREATED        PLATFORM       SIZE        BLOB SIZE
registry.cn-beijing.aliyuncs.com/k7scn/tools    latest    71442d19f1f3    3 hours ago    linux/amd64    55.4 MiB    45.7 MiB
[root@kube-master cni]# 
或者
[root@kube-master cni]# nerdctl image ls
REPOSITORY                                      TAG       IMAGE ID        CREATED        PLATFORM       SIZE        BLOB SIZE
registry.cn-beijing.aliyuncs.com/k7scn/tools    latest    71442d19f1f3    3 hours ago    linux/amd64    55.4 MiB    45.7 MiB

下载镜像

[root@kube-master cni]# nerdctl pull nginx:alpine
WARN[0000] skipping verifying HTTPS certs for "docker.io" 
docker.io/library/nginx:alpine:                                                   resolved       |++++++++++++++++++++++++++++++++++++++| 
index-sha256:eb05700fe7baa6890b74278e39b66b2ed1326831f9ec3ed4bdc6361a4ac2f333:    done           |++++++++++++++++++++++++++++++++++++++| 
manifest-sha256:544ba2bfe312bf2b13278495347bb9381ec342e630bcc8929af124f1291784bb: done           |++++++++++++++++++++++++++++++++++++++| 
config-sha256:cc44224bfe208a46fbc45471e8f9416f66b75d6307573e29634e7f42e27a9268:   done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:40e5d2fe5bcd566dbde3e961f33ced0f1503fc6ee320a427b185a07afe2f96ae:    done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:59bf1c3509f33515622619af21ed55bbe26d24913cedbca106468a5fb37a50c3:    done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:f3322597df46099a66ed5773c10a9d1cb587faca7be14ceba985e3d1fbfdbc36:    done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:d09cf91cabdcf5f64672598b8e4da9b0b7d8546e83ec49633bdd92abb994ba61:    done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:3a97535ac2efcf94ab3e5f93a6ec4d934469de66909f17ba1229f86ee660970a:    done           |++++++++++++++++++++++++++++++++++++++| 
layer-sha256:919ade35f869e23d663ea51fdf2e99aa183239a73b4b4780e052c8b248ed5b7e:    done           |++++++++++++++++++++++++++++++++++++++| 
elapsed: 20.9s                                                                    total:  9.7 Mi (475.7 KiB/s)                                     
[root@kube-master cni]# nerdctl images
REPOSITORY                                      TAG       IMAGE ID        CREATED           PLATFORM       SIZE        BLOB SIZE
nginx                                           alpine    eb05700fe7ba    13 seconds ago    linux/amd64    26.4 MiB    9.7 MiB
registry.cn-beijing.aliyuncs.com/k7scn/tools    latest    71442d19f1f3    3 hours ago       linux/amd64    55.4 MiB    45.7 MiB

查看镜像详细信息

[root@kube-master ~]# nerdctl inspect nginx:alpine

构建镜像

[root@kube-master ~]# cd /tmp/
[root@kube-master tmp]# cat > /tmp/Dockerfile <<EOF
FROM ustc-edu-cn.mirror.aliyuncs.com/library/nginx:latest
RUN echo "hello world!" > /usr/share/nginx/html/index.html
EOF

[root@kube-master tmp]# nerdctl build -t mynginx:v1 /tmp
ERRO[0000] `buildctl` needs to be installed and `buildkitd` needs to be running, see https://github.com/moby/buildkit  error="failed to ping to host unix:///run/buildkit-default/buildkitd.sock: exit status 1\nfailed to ping to host unix:///run/buildkit/buildkitd.sock: exit status 1"
FATA[0000] no buildkit host is available, tried 2 candidates: failed to ping to host unix:///run/buildkit-default/buildkitd.sock: exit status 1
failed to ping to host unix:///run/buildkit/buildkitd.sock: exit status 1

当构建镜像时,出现如上报错信息,是因为 buildkit.service 服务没有启动,下面先添加启动文件,然后启动服务:

#添加buildkit.service文件
[root@kube-master tmp]# vim /usr/lib/systemd/system/buildkit.service
[Unit]
Description=BuildKit
Requires=buildkit.socket
After=buildkit.socket
Documentation=https://github.com/moby/buildkit

[Service]
Type=notify
ExecStart=/usr/local/bin/buildkitd --addr fd://

[Install]
WantedBy=multi-user.target

#添加buildkit.socket文件
[root@kube-master tmp]# vim /usr/lib/systemd/system/buildkit.socket
[Unit]
Description=BuildKit
Documentation=https://github.com/moby/buildkit

[Socket]
ListenStream=%t/buildkit/buildkitd.sock
SocketMode=0660

[Install]
WantedBy=sockets.target

#启动服务
[root@kube-master tmp]# systemctl enable buildkit.service ; systemctl start buildkit.service
Created symlink from /etc/systemd/system/multi-user.target.wants/buildkit.service to /usr/local/lib/systemd/system/buildkit.service.

BuildKit 是由 docker 公司开发的下一代 docker build 工具,具有更高效、更安全、 易于扩展等特点。BuildKit 是由 buildkitd 守护程序 和 buildctl 客户端组成。

  • buildkitd 作为服务端,连接容器运行时,目前支持 runc 和 containerd 作为镜像构建环境,默认是 runc
  • buildctl 作为客户端,负责解析 Dockerfile 文件,并向 buildkitd 发出构建请求。由于命令复杂,使用 nerdctl 替代
    再次进行构建
[root@kube-master tmp]# nerdctl  build -t mynginx:v1 /tmp/ 
[+] Building 17.8s (6/6) FINISHED                                                                                                                      
 => [internal] load build definition from Dockerfile                                                                                              0.0s
 => => transferring dockerfile: 154B                                                                                                              0.0s
 => [internal] load metadata for ustc-edu-cn.mirror.aliyuncs.com/library/nginx:latest                                                            15.6s
 => [internal] load .dockerignore                                                                                                                 0.0s
 => => transferring context: 2B                                                                                                                   0.0s
 => [1/2] FROM ustc-edu-cn.mirror.aliyuncs.com/library/nginx:latest@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31       0.0s
 => => resolve ustc-edu-cn.mirror.aliyuncs.com/library/nginx:latest@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31       0.0s
 => CACHED [2/2] RUN echo "hello world!" > /usr/share/nginx/html/index.html                                                                       0.0s
 => exporting to docker image format                                                                                                              2.1s
 => => exporting layers                                                                                                                           0.0s
 => => exporting manifest sha256:786482a9a8cfc283cf2aa577c6428968660afc2a8fd334d37d9a4b06b80f0888                                                 0.0s
 => => exporting config sha256:c3bc6d0ebbe5d11f803b6b783f69c494e22a17bcedafb74bbf9876052f57ff46                                                   0.0s
 => => sending tarball                                                                                                                            2.1s
unpacking docker.io/library/mynginx:v1 (sha256:786482a9a8cfc283cf2aa577c6428968660afc2a8fd334d37d9a4b06b80f0888)...
Loaded image: docker.io/library/mynginx:v1

查看镜像

[root@kube-master tmp]# nerdctl  images
REPOSITORY                                       TAG       IMAGE ID        CREATED              PLATFORM       SIZE         BLOB SIZE
mynginx                                          v1        786482a9a8cf    About an hour ago    linux/amd64    149.1 MiB    54.1 MiB
ustc-edu-cn.mirror.aliyuncs.com/library/nginx    latest    0d17b565c37b    About an hour ago    linux/amd64    149.1 MiB    

注意:
nerdctl 构建的机制和 docker 是完全不同的。

  • docker 首先会检查本地是否有 Dockerfile 中 FROM 的镜像。如果有,直接使用。没有则通过网络下载镜像;
  • nerdctl 会根据 Dockerfile FROM参数指定镜像的域名去网上找这个镜像,找到后确认和本地同名镜像校验无误之后,才会使用本地的镜像构建新镜像。

举例:
通过tag 打标一个不存在域名的镜像

[root@kube-master tmp]# nerdctl tag ustc-edu-cn.mirror.aliyuncs.com/library/nginx:latest margu.com/library/nginx:latest
#查看镜像
[root@kube-master tmp]# nerdctl  images
REPOSITORY                                       TAG       IMAGE ID        CREATED              PLATFORM       SIZE         BLOB SIZE
mynginx                                          v1        786482a9a8cf    About an hour ago    linux/amd64    149.1 MiB    54.1 MiB
margu.com/library/nginx                          latest    0d17b565c37b    3 seconds ago        linux/amd64    149.1 MiB    54.1 MiB
ustc-edu-cn.mirror.aliyuncs.com/library/nginx    latest    0d17b565c37b    About an hour ago    linux/amd64    149.1 MiB    54.1 MiB

通过margu.com/library/nginx:latest构建新镜像

[root@kube-master tmp]# cat > /tmp/Dockerfile <<EOF
FROM umargu.com/library/nginx:latest
RUN echo "hello world!" > /usr/share/nginx/html/index.html
EOF


[root@kube-master tmp]# nerdctl  build -t mynginx:v2 /tmp/ 
[+] Building 12.1s (2/2) FINISHED                                                                                                                      
 => [internal] load build definition from Dockerfile                                                                                              0.0s
 => => transferring dockerfile: 132B                                                                                                              0.0s
 => ERROR [internal] load metadata for margu.com/library/nginx:latest                                                                            12.1s
------
 > [internal] load metadata for margu.com/library/nginx:latest:
------
Dockerfile:1
--------------------
   1 | >>> FROM margu.com/library/nginx:latest
   2 |     RUN echo "hello world!" > /usr/share/nginx/html/index.html
   3 |     
--------------------
error: failed to solve: margu.com/library/nginx:latest: failed to do request: Head "https://margu.com/v2/library/nginx/manifests/latest": dial tcp 5.161.180.74:443: connect: connection refused
FATA[0012] no image was built

构建时,直接就抛出了错误信息,这里要 非常注意!

镜像标签TAG

[root@kube-master tmp]# nerdctl tag ustc-edu-cn.mirror.aliyuncs.com/library/nginx:latest margu.com/library/nginx:latest
[root@kube-master tmp]# nerdctl  images
REPOSITORY                                       TAG       IMAGE ID        CREATED              PLATFORM       SIZE         BLOB SIZE
mynginx                                          v1        786482a9a8cf    About an hour ago    linux/amd64    149.1 MiB    54.1 MiB
margu.com/library/nginx                          latest    0d17b565c37b    6 seconds ago        linux/amd64    149.1 MiB    54.1 MiB
ustc-edu-cn.mirror.aliyuncs.com/library/nginx    latest    0d17b565c37b    About an hour ago    linux/amd64    149.1 MiB    54.1 MiB

删除镜像

[root@kube-master tmp]# nerdctl  rmi margu.com/library/nginx:latest
Untagged: margu.com/library/nginx:latest@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Deleted: sha256:2edcec3590a4ec7f40cf0743c15d78fb39d8326bc029073b41ef9727da6c851f
Deleted: sha256:e379e8aedd4d72bb4c529a4ca07a4e4d230b5a1d3f7a61bc80179e8f02421ad8
Deleted: sha256:b8d6e692a25e11b0d32c5c3dd544b71b1085ddc1fddad08e68cbd7fda7f70221
Deleted: sha256:f1db227348d0a5e0b99b15a096d930d1a69db7474a1847acbc31f05e4ef8df8c
Deleted: sha256:32ce5f6a5106cc637d09a98289782edf47c32cb082dc475dd47cbf19a4f866da
Deleted: sha256:d874fd2bc83bb3322b566df739681fbd2248c58d3369cb25908d68e7ed6040a6
[root@kube-master tmp]# nerdctl  images
REPOSITORY                                       TAG       IMAGE ID        CREATED              PLATFORM       SIZE         BLOB SIZE
mynginx                                          v1        786482a9a8cf    About an hour ago    linux/amd64    149.1 MiB    54.1 MiB
ustc-edu-cn.mirror.aliyuncs.com/library/nginx    latest    0d17b565c37b    About an hour ago    linux/amd64    149.1 MiB    54.1 MiB

导出镜像

#导出,不压缩
[root@kube-master tmp]# nerdctl save mynginx:v1 -o mynginx-1.tar
#导出且压缩
[root@kube-master tmp]# nerdctl save mynginx:v1|gzip  >mynginx-2.tar

#比较两者大小,镜像太小大小差别不明显。越大的镜像越压缩后越明显
[root@kube-master tmp]# ll -h mynginx-*
-rw-r--r-- 1 root root 55M Jun 24 11:21 mynginx-1.tar
-rw-r--r-- 1 root root 54M Jun 24 11:22 mynginx-2.tar

导入镜像

[root@kube-master tmp]# nerdctl load <  mynginx-1.tar 
unpacking docker.io/library/mynginx:v1 (sha256:786482a9a8cfc283cf2aa577c6428968660afc2a8fd334d37d9a4b06b80f0888)...
Loaded image: mynginx:v1
#或
[root@kube-master tmp]# nerdctl load -i mynginx-1.tar 

通过上面的展示,基本和docker无差别,其他镜像管理的功能不再赘述。

网络

在安装 nerdctl-full-1.7.6-linux-amd64.tar.gz 时,网络插件也安装了。主要文件在/etc/cni 目录。

[root@kube-master ~]# ll /etc/cni/*
total 8
-rw-r--r--. 1 root root 292 Apr 23  2020 10-flannel.conflist
-rw-r--r--  1 root root 860 Jun 20 14:32 nerdctl-bridge.conflist

查看网络

[root@kube-master ~]# nerdctl network ls
NETWORK ID      NAME      FILE
                cbr0      /etc/cni/net.d/10-flannel.conflist
17f29b073143    bridge    /etc/cni/net.d/nerdctl-bridge.conflist
                host      
                none  

创建桥接网络

[root@kube-master ~]# nerdctl network create -d bridge --subnet 10.244.0.0/16 mynet
11c844f95e2862126712e209cd3acbc68c137931c639633da9dfc17b3a464bde
[root@kube-master ~]# nerdctl network ls
NETWORK ID      NAME      FILE
                cbr0      /etc/cni/net.d/10-flannel.conflist
17f29b073143    bridge    /etc/cni/net.d/nerdctl-bridge.conflist
11c844f95e28    mynet     /etc/cni/net.d/nerdctl-mynet.conflist
                host      
                none                      

#查看创建的网络的配置文件                
[root@kube-master ~]# cat /etc/cni/net.d/nerdctl-mynet.conflist
{
  "cniVersion": "1.0.0",
  "name": "mynet",
  "nerdctlID": "11c844f95e2862126712e209cd3acbc68c137931c639633da9dfc17b3a464bde",
  "nerdctlLabels": {},
  "plugins": [
    {
      "type": "bridge",
      "bridge": "br-11c844f95e28",
      "isGateway": true,
      "ipMasq": true,
      "hairpinMode": true,
      "ipam": {
        "ranges": [
          [
            {
              "gateway": "10.244.0.1",
              "subnet": "10.244.0.0/16"
            }
          ]
        ],
        "routes": [
          {
            "dst": "0.0.0.0/0"
          }
        ],
        "type": "host-local"
      }
    },
    {
      "type": "portmap",
      "capabilities": {
        "portMappings": true
      }
    },
    {
      "type": "firewall",
      "ingressPolicy": "same-bridge"
    },
    {
      "type": "tuning"
    }

nerdctl 所使用的网络及模式和 docker 完全一致,可以参考博客docker相关章节。

容器管理

nerdctl 和 dockerc-cli 类似,nerdctl 出现的原因之一就是为了 兼容 docker-cli,所以用法一致,这里只列举几个,其他使用请直接参考 docker-cli

启动容器

[root@kube-master ~]# nerdctl run --name ngx -d -p 80:80  ustc-edu-cn.mirror.aliyuncs.com/library/nginx
b42999d7549bff53767152a364d14438360b7a2751b8b89be9872c199ed121c5

#启动容器并指定特定网络(使用宿主机网络直接启动容器)
[root@kube-master ~]# nerdctl run --name ngx1 --net host -d   ustc-edu-cn.mirror.aliyuncs.com/library/nginx:latest
20e071e36a5e8dab24be68baa6c40d74adda1a9c6f75bdc03210135d57b8ff0e

查看容器

[root@kube-master ~]# nerdctl  ps
CONTAINER ID    IMAGE                                                   COMMAND                   CREATED           STATUS    PORTS                 NAMES
20e071e36a5e    ustc-edu-cn.mirror.aliyuncs.com/library/nginx:latest    "/docker-entrypoint.…"    14 seconds ago    Up                              ngx1 
b42999d7549b    ustc-edu-cn.mirror.aliyuncs.com/library/nginx:latest    "/docker-entrypoint.…"    2 minutes ago     Up        0.0.0.0:80->80/tcp    ngx

#查看所有容器
[root@kube-master ~]# nerdctl ps -a
CONTAINER ID    IMAGE                             COMMAND                   CREATED               STATUS                         PORTS                 NAMES
0c671d5ac3f1    docker.io/library/nginx:alpine    "/docker-entrypoint.…"    About a minute ago    Up                             0.0.0.0:80->80/tcp    ngx
5ff17a6ba473    docker.io/library/nginx:alpine    "/docker-entrypoint.…"    19 seconds ago        Exited (137) 14 seconds ago    0.0.0.0:80->80/tcp    ngx-1

#查看容器详细信息
[root@kube-master ~]# nerdctl inspect  ngx
[
    {
        "Id": "b42999d7549bff53767152a364d14438360b7a2751b8b89be9872c199ed121c5",
        "Created": "2024-06-24T03:38:51.736767639Z",
        "Path": "/docker-entrypoint.sh",
        "Args": [
            "nginx",
            "-g",
            "daemon off;"
        ],
        "State": {
            "Status": "running",
            "Running": true,
            "Paused": false,
            "Restarting": false,
            "Pid": 31207,
            "ExitCode": 0,
            "Error": "",
            "FinishedAt": "0001-01-01T00:00:00Z"
        },
        "Image": "ustc-edu-cn.mirror.aliyuncs.com/library/nginx:latest",
        "ResolvConfPath": "/var/lib/nerdctl/1935db59/containers/default/b42999d7549bff53767152a364d14438360b7a2751b8b89be9872c199ed121c5/resolv.conf",
        "HostnamePath": "/var/lib/nerdctl/1935db59/containers/default/b42999d7549bff53767152a364d14438360b7a2751b8b89be9872c199ed121c5/hostname",
        "LogPath": "/var/lib/nerdctl/1935db59/containers/default/b42999d7549bff53767152a364d14438360b7a2751b8b89be9872c199ed121c5/b42999d7549bff53767152a364d14438360b7a2751b8b89be9872c199ed121c5-json.log",
        "Name": "ngx",
        "RestartCount": 0,
        "Driver": "overlayfs",
        "Platform": "linux",
        "AppArmorProfile": "",
        "Mounts": null,
        "Config": {
            "Hostname": "b42999d7549b",
            "AttachStdin": false,
            "Labels": {
                "io.containerd.image.config.stop-signal": "SIGQUIT",
                "nerdctl/extraHosts": "null",
                "nerdctl/hostname": "b42999d7549b",
                "nerdctl/log-uri": "binary:///usr/local/bin/nerdctl?_NERDCTL_INTERNAL_LOGGING=%2Fvar%2Flib%2Fnerdctl%2F1935db59",
                "nerdctl/name": "ngx",
                "nerdctl/namespace": "default",
                "nerdctl/networks": "[\"bridge\"]",
                "nerdctl/platform": "linux/amd64",
                "nerdctl/ports": "[{\"HostPort\":80,\"ContainerPort\":80,\"Protocol\":\"tcp\",\"HostIP\":\"0.0.0.0\"}]",
                "nerdctl/state-dir": "/var/lib/nerdctl/1935db59/containers/default/b42999d7549bff53767152a364d14438360b7a2751b8b89be9872c199ed121c5"
            }
        },
        "NetworkSettings": {
            "Ports": {
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "80"
                    }
                ]
            },
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "10.4.0.5",
            "IPPrefixLen": 24,
            "MacAddress": "b2:0f:dc:0d:9d:8a",
            "Networks": {
                "unknown-eth0": {
                    "IPAddress": "10.4.0.5",
                    "IPPrefixLen": 24,
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "b2:0f:dc:0d:9d:8a"
                }
            }
        }
    }
]

删除容器

1.stop 容器
2.删除 容器
或者
强制删除容器

[root@kube-master ~]# nerdctl stop ngx
ngx
[root@kube-master ~]# nerdctl rm  ngx
ngx
[root@kube-master ~]# nerdctl rm  ngx1
FATA[0000] 1 errors:
container 20e071e36a5e8dab24be68baa6c40d74adda1a9c6f75bdc03210135d57b8ff0e is in running status. unpause/stop container first or force removal 
[root@kube-master ~]# nerdctl rm  ngx1 -f 
ngx1

运行docker-compose

nerdctl 直接兼容了 docker-compose 。
docker-compose 配置清单文件

[root@kube-master ~]# cat > docker-compose.yml << EOF
version: "3.7"
services:
  ngx:
    container_name: "ngx"
    image: ustc-edu-cn.mirror.aliyuncs.com/library/nginx:latest
    restart: always
    networks:
    - test_net
    ports:
    - 80:80

networks:
  test_net:
    name: test_net
    driver: bridge
    ipam:
      config:
      - subnet: "172.100.0.0/16"
EOF

通过docker-compose 启动

[root@kube-master ~]# nerdctl compose up -d
INFO[0000] Creating network test_net                    
INFO[0000] Creating network root_default                
INFO[0000] Ensuring image ustc-edu-cn.mirror.aliyuncs.com/library/nginx:latest 
INFO[0000] Creating container ngx             

查看启动

[root@kube-master ~]# nerdctl compose ps
NAME    IMAGE                                                   COMMAND                   SERVICE    STATUS     PORTS
ngx     ustc-edu-cn.mirror.aliyuncs.com/library/nginx:latest    "/docker-entrypoint.…"    ngx        running    0.0.0.0:80->80/tcp

停止并删除

[root@kube-master ~]# nerdctl compose down
INFO[0000] Removing container ngx                       
INFO[0000] Removing network root_default                
INFO[0000] Removing network test_net                    
[root@kube-master ~]# nerdctl compose ps
NAME    IMAGE    COMMAND    SERVICE    STATUS    PORTS

更多关于containerd的知识分享,请前往博客主页。编写过程中,难免出现差错,敬请指出

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1865864.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

u盘插到另一台电脑上数据丢失怎么办?提供实用的解决方案

在现代数字化生活中&#xff0c;U盘作为一种便携式存储设备&#xff0c;承载着我们重要的数据和信息。然而&#xff0c;有时当我们将U盘插入另一台电脑时&#xff0c;可能会遇到数据丢失的棘手问题。这可能是由于多种原因造成的&#xff0c;那么&#xff0c;U盘插到另一台电脑上…

cs与msf权限传递,与mimikatz抓取win2012明文密码

CS与MSF的权限互相传递抓取windows2012的明文密码 CS与MSF的权限互相传递 1、启动cs服务端 2、客户端连接 3、配置监听&#xff0c;并设置监听端口为9999 4、生成脚本 5、开启服务&#xff0c;下载并运行木马 已获取权限 6、进入msf并设置监听 7、cs新建监听&#xff0c;与m…

20240626让飞凌的OK3588-C开发板在相机使用1080p60分辨率下预览

20240626让飞凌的OK3588-C开发板在相机使用1080p60分辨率下预览 2024/6/26 15:15 4.2.1 全编译测试 在源码路径内&#xff0c;提供了编译脚本 build.sh&#xff0c;运行该脚本对整个源码进行编译&#xff0c;需要在终端切换到解压 出来的源码路径&#xff0c;找到 build.sh 文件…

海纳斯 hinas 的hi3798mv100 华为悦盒 6108v9 安装wifi模块

hi3798mv100安装wifi模块 1.执行脚本 &#xff0c;执行完毕后重启服务器2. 继续执行脚本3.检查网卡驱动安装是否正确4.查看网卡安装状态5.连接wifi结尾 1.执行脚本 &#xff0c;执行完毕后重启服务器 bash <(curl -sSL https://gitee.com/xjxjin/scripts/raw/main/install_…

【LeetCode】三、队列相关:最近的请求次数

文章目录 1、队列结构2、leetcode933&#xff1a;最近的请求次数 1、队列结构 先进先出 时间复杂度&#xff1a; Java中&#xff0c;LinkedList集合可以当一个队列来用&#xff1a; 2、leetcode933&#xff1a;最近的请求次数 很逆天的题目描述&#xff0c;就是不同时间会有请…

关于WebSocket

WebSocket 与传统的 HTTP 协议对比 在实时通信领域&#xff0c;传统的 HTTP 协议存在以下一些问题&#xff1a; 频繁的请求和响应&#xff1a;每次通信都需要建立和关闭连接&#xff0c;带来额外的开销。高延迟&#xff1a;每次通信都需要经过多个网络层的传输&#xff0c;延…

【仿真】UR机器人手眼标定与实时视觉追踪(单目)

这段代码实现了一个机器人视觉引导系统,主要功能包括: 连接仿真环境,控制UR机器人。相机标定: 使用棋盘格图案进行相机内参标定通过移动机器人采集多组图像使用calibrateCamera函数计算相机内参 手眼标定: 采集机器人末端位姿和对应的棋盘格图像使用calibrateHandEye函数计算相…

外汇的基本面分析需要关注什么?

外汇基本面分析的核心在于关注可能影响单一货币供求及国家货币价值的经济、社会和地缘政治事件与趋势。但值得注意的是&#xff0c;这些事件和因素往往具有更广泛的影响力&#xff0c;不仅限于单一国家。它们可能是影响整个地区或国家集团的重要事件&#xff0c;甚至一些事件&a…

植物大战僵尸杂交版技巧大全(附下载攻略)

《植物大战僵尸杂交版》为策略游戏爱好者带来了全新的挑战和乐趣。如果你是新手玩家&#xff0c;可能会对游戏中的植物和僵尸感到困惑。以下是一些实用的技巧&#xff0c;帮助你快速掌握游戏并享受其中的乐趣。 技巧一&#xff1a;熟悉基本玩法 游戏的基本玩法与原版相似&…

大自然高清风景视频无水印素材在哪下载?下载视频素材网分享

在视频创作领域&#xff0c;一段高清的风景视频可以极大地提升你的作品质感。无论是作为背景、过渡片段还是主要内容&#xff0c;优质的风景视频素材都是必不可少的。然而&#xff0c;寻找既高清又无水印的风景视频素材并非易事。为了帮助大家轻松获取这类素材&#xff0c;我整…

七天速通javaSE:第四天 递归算法

文章目录 前言一、递归的介绍二、递归模型&#xff08;n!&#xff09;1 阶乘的定义&#xff1a;2. 阶乘的递归代码实现3. 递推与回归的内部逻辑三、练习 前言 本文将学习递归算法。在计算机科学中&#xff0c;递归算法是一种将问题不断分解 为同一类子问题来解决问题的方法。递…

网站推广如何做?这七个方法要知道

在出海独立站商家中&#xff0c;推广是必不可少的环节。在你完成网站的搭建&#xff0c;产品的上架&#xff0c;以及网站的运营和优化后&#xff0c;你就可以开始着手推广你的网站了。你的网站是承载你的品牌和产品的主要平台&#xff0c;因此&#xff0c;你需要根据你的品牌和…

Python+Vue+Springboot实现电脑端微信好友导入导出

主要实现思路是使用python的自动化库uiautomation进行客户端抓取联系人&#xff0c;vue做管理界面&#xff0c;springboot做后端服务。 截图如下 登录&#xff1a; 首页 好友导出 不足之处就是只有windows版本&#xff0c;mac上还不行 而且谷歌和edge浏览器的效果是最好的&a…

【C++】红黑树及其实现

目录 一、红黑树的定义1.为什么提出红黑树&#xff1f;2.红黑树的概念3.红黑树的性质 二、红黑树的实现1.红黑树的结构2.红黑树的插入2.1 uncle为红色2.2 uncle为黑色&#xff0c;且是grandfather的右孩子2.3 uncle为黑色&#xff0c;且是grandfather的左孩子 3.红黑树的验证 4…

PTL系统电子标签让工厂仓储出入库管理更高效

在现代工厂的运营中&#xff0c;仓储管理的效率直接影响着整个生产流程的顺畅与企业的竞争力。PTL 系统&#xff08;Pick-to-Light&#xff09;的应用&#xff0c;为工厂仓储的出入库管理带来了革命性的变化&#xff0c;使其更加高效、精准和智能化。 一、仓库安装使用PTL系统电…

TF-IDF、BM25传统算法总结

1. TF-IDF算法 F-IDF&#xff08;词频-逆文档频率&#xff09;是一种用于衡量文本中词语重要性的方法&#xff0c;特别适用于信息检索和文本挖掘任务。下面会拆分为两部分深入讲解TF-IDF的计算过程&#xff0c;以便更好地理解。 TF-IDF的计算过程可以分为两个主要部分&#xf…

2.移植freertos到stm32f103c8t6

目录 1.步骤 2.freertos配置时常见的选项卡意思 1.步骤 2.freertos配置时常见的选项卡意思

typescript学习回顾(三)

今天继续来分享ts的相关概念&#xff0c;枚举&#xff0c;ts模块化&#xff0c;接口和类型兼容性 ts的扩展类型&#xff1a;类型别名&#xff0c;枚举&#xff0c;接口和类 枚举 基础概念 枚举通常用于约束某个变量的取值范围。当然字面量和联合类型配合使用&#xff0c;也可…

动态规划——123. 买卖股票的最佳时机 III

目录 1、题目链接 2、题目分析 1.状态表示 2.状态转移方程 3.初始化 4.填表 5.返回值 3、代码解析 1、题目链接 123. 买卖股票的最佳时机 III 2、题目分析 1.状态表示 由题目可知&#xff0c;我们分为两种状态&#xff0c;买入和卖出&#xff0c;又因为只能完成两次交易…

如何从iPhone恢复错误删除的照片

嘿&#xff0c;iPhone 用户&#xff01;作为一名苹果专业人士&#xff0c;我见过相当多的“哎呀&#xff0c;我删除了它&#xff01;”的时刻。今天&#xff0c;我在这里指导您完成从iPhone中恢复那些珍贵的&#xff0c;错误删除的照片的迷宫。坐下来&#xff0c;拿起你的设备&…