nerdctl的使用

什么是nerdctl

nerdctl是containerd官方提供的兼容Docker命令行的工具,支持Docker Cli所有命令,并且支持dokcer-compose

设计初衷

不是Docker Cli复制品,希望促进containerd创新实验的发展,Docker不支持这些实验特性如镜像延迟加载,镜像加密

安装nerdctl

官方提供俩种安装包

  • 精简安装包:nerdctl--linux-amd64.tar.gz 仅包含nerdctl的二进制文件,以及rootless模式下的辅助安装脚本,需要解压到/user/local/bin目录
  • 完整安装包:nerdctl-full--linux-amd64-.tar.gz。包含containerd、CNI、runc、BUildKit、rootlesskit等完整组件,需要解压到/user/local目录

cat share/doc/nerdctl-full/README.md可以看见组件版本

  1. 下载nerdctl完整安装包

    1
    2
    wget https://github.com/containerd/nerdctl/releases/download/v2.0.2/nerdctl-full-2.0.2-linux-amd64.tar.gz

  2. 解压到/usr/local/

1
tar -zxvf nerdctl-full-2.0.2-linux-amd64.tar.gz -C /usr/local
  1. 查看是否安装成功
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@iZbp1ebizftw2vpbpm737wZ ~]# nerdctl info
Client:
Namespace: default
Debug Mode: false

Server:
Server Version: v1.7.24
Storage Driver: overlayfs
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Log: fluentd journald json-file none syslog
Storage: native overlayfs
Security Options:
seccomp
Profile: builtin
Kernel Version: 3.10.0-957.21.3.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.716GiB
Name: iZbp1ebizftw2vpbpm737wZ
ID: 312e2c09-efc8-41e7-9d4b-761e8a1699e6

WARNING: IPv4 forwarding is disabled
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
[root@iZbp1ebizftw2vpbpm737wZ ~]#

mac也支持containerd,但是需要借助lima

Lima是专门在mac上的contained和nerdctl的项目

1
2
3
brew install lima
limactl start
lim nerdctl run -d --name nginx -p 127.0.0.1:8080:80 nginx:alpine

命令行的使用

和Docker使用相当

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
[root@iZbp1ebizftw2vpbpm737wZ ~]# nerdctl help                                                                                                                                                             
nerdctl is a command line interface for containerd

Config file ($NERDCTL_TOML): /etc/nerdctl/nerdctl.toml

Usage: nerdctl [flags]

helpers.Management commands:
apparmor Manage AppArmor profiles
builder Manage builds
container Manage containers
image Manage images
ipfs Distributing images on IPFS
namespace Manage containerd namespaces
network Manage networks
system Manage containerd
volume Manage volumes

Commands:
attach Attach stdin, stdout, and stderr to a running container.
build Build an image from a Dockerfile. Needs buildkitd to be running.
commit Create a new image from a container's changes
completion Generate the autocompletion script for the specified shell
compose Compose
cp Copy files/folders between a running container and the local filesystem.
create Create a new container. Optionally specify "ipfs://" or "ipns://" scheme to pull image from IPFS.
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
help Help about any command
history Show the history of an image
images List images
info Display system-wide information
inspect Return low-level information on objects.
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a container registry
logout Log out from a container registry
logs Fetch the logs of a container. Expected to be used with 'nerdctl run -d'.
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image from a registry. Optionally specify "ipfs://" or "ipns://" scheme to pull image from IPFS.
push Push an image or a repository to a registry. Optionally specify "ipfs://" or "ipns://" scheme to push image to IPFS.
rename rename a container
restart Restart one or more running containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container. Optionally specify "ipfs://" or "ipns://" scheme to pull image from IPFS.
save Save one or more images to a tar archive (streamed to STDOUT by default)
start Start one or more running containers
stats Display a live stream of container(s) resource usage statistics.
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update one or more running containers
version Show the nerdctl version information
wait Block until one or more containers stop, then print their exit codes.

Flags:
-H, --H string Alias of --address (default "/run/containerd/containerd.sock")
-a, --a string Alias of --address (default "/run/containerd/containerd.sock")
--address string containerd address, optionally with "unix://" prefix [$CONTAINERD_ADDRESS] (default "/run/containerd/containerd.sock")
--bridge-ip string IP address for the default nerdctl bridge network [$NERDCTL_BRIDGE_IP]
--cgroup-manager string Cgroup manager to use ("cgroupfs"|"systemd") (default "cgroupfs")
--cni-netconfpath string cni config directory [$NETCONFPATH] (default "/etc/cni/net.d")
--cni-path string cni plugins binary directory [$CNI_PATH] (default "/opt/cni/bin")
--data-root string Root directory of persistent nerdctl state (managed by nerdctl, not by containerd) (default "/var/lib/nerdctl")
--debug debug mode
--debug-full debug mode (with full output)
--experimental Control experimental: https://github.com/containerd/nerdctl/blob/main/docs/experimental.md [$NERDCTL_EXPERIMENTAL] (default true)
-h, --help help for nerdctl
--host string Alias of --address (default "/run/containerd/containerd.sock")
--host-gateway-ip string IP address that the special 'host-gateway' string in --add-host resolves to. Defaults to the IP address of the host. It has no effect without setting --add-host [$NERDCTL
_HOST_GATEWAY_IP] (default "172.29.67.77")
--hosts-dir strings A directory that contains /hosts.toml (containerd style) or /{ca.cert, cert.pem, key.pem} (docker style) (default [/etc/containerd/certs.d,/etc/dock
er/certs.d])
--insecure-registry skips verifying HTTPS certs, and allows falling back to plain HTTP
-n, --n string Alias of --namespace (default "default")
--namespace string containerd namespace, such as "moby" for Docker, "k8s.io" for Kubernetes [$CONTAINERD_NAMESPACE] (default "default")
--snapshotter string containerd snapshotter [$CONTAINERD_SNAPSHOTTER] (default "overlayfs")
--storage-driver string Alias of --snapshotter (default "overlayfs")
-v, --version version for nerdctl

Run 'nerdctl COMMAND --help' for more information on a command.

[root@iZbp1ebizftw2vpbpm737wZ ~]#

nerdctl但是比Docker多了一个命名空间的概念,通过–namespace

  • 如果是docker可以设置:alias docker=nerdctl
  • 如果是K8s环境:alias docker=’nerdctl -n k8s.io’

运行容器

1
nerdctl run -d --name nginx -p 80:80 nginx:alpine
  • 与Docker不同的是,nerdctl运行容器时,使用CNI插件,默认使用bridgeCNI插件,默认网段10.4.0.0/24

构建镜像

nerdctl构建镜像的能力,依赖于BuildKit

构建镜像之前启动BuildKit

1
systemctl enable --now buildkit

和Docker一样打包

1
2
FROM nginx
RUN echoc 'this is a nginx image' > /usr/share/nginx/html/index.html
1
nerdctl build -t mynginx.

更多的build 命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
[root@iZbp1ebizftw2vpbpm737wZ ~]# nerdctl build -h
Build an image from a Dockerfile. Needs buildkitd to be running.
If Dockerfile is not present and -f is not specified, it will look for Containerfile and build with it.

Usage: nerdctl build [flags] PATH

Flags:
--allow stringArray Allow extra privileged entitlement, e.g. network.host, security.insecure
--attest stringArray Attestation parameters (format: "type=sbom,generator=image")
--build-arg stringArray Set build-time variables
--build-context stringArray Additional build contexts (e.g., name=path)
--buildkit-host string BuildKit address [$BUILDKIT_HOST]
--cache-from stringArray External cache sources (eg. user/app:cache, type=local,src=path/to/dir)
--cache-to stringArray Cache export destinations (eg. user/app:cache, type=local,dest=path/to/dir)
-f, --file string Name of the Dockerfile
-h, --help help for build
--iidfile string Write the image ID to the file
--label stringArray Set metadata for an image
--network string Set type of network for build (format:network=default|none|host) (default "default")
--no-cache Do not use cache when building the image
-o, --output string Output destination (format: type=local,dest=path)
--platform strings Set target platform for build (e.g., "amd64", "arm64")
--progress string Set type of progress output (auto, plain, tty). Use plain to show container output (default "auto")
--provenance string Shorthand for "--attest=type=provenance"
--pull On true, always attempt to pull latest image version from remote. Default uses buildkit's default.
-q, --quiet Suppress the build output and print image ID on success
--rm Remove intermediate containers after a successful build (default true)
--sbom string Shorthand for "--attest=type=sbom"
--secret stringArray Secret file to expose to the build: id=mysecret,src=/local/secret
--ssh stringArray SSH agent socket or keys to expose to the build (format: default|<id>[=<socket>|<key>[,<key>]])
-t, --tag stringArray Name and optionally a tag in the 'name:tag' format
--target string Set the target build stage to build

See also 'nerdctl --help' for the global flags such as '--namespace', '--snapshotter', and '--cgroup-manager'.
[root@iZbp1ebizftw2vpbpm737wZ ~]#