[root@192 containerd]# nerdctl run --help Run a command in a new container. Optionally specify "ipfs://" or "ipns://" scheme to pull image from IPFS.
Usage: nerdctl run [flags] IMAGE [COMMAND] [ARG...]
Flags: --add-host strings Add a custom host-to-IP mapping (host:ip) --blkio-weight uint16 Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) --cap-add strings Add Linux capabilities --cap-drop strings Drop Linux capabilities --cgroup-conf strings Configure cgroup v2 (key=value) --cgroup-parent string Optional parent cgroup for the container --cgroupns string Cgroup namespace to use, the default depends on the cgroup version ("host"|"private") (default "private") --cidfile string Write the container ID to the file --cosign-certificate-identity string The identity expected in a valid Fulcio certificate for --verify=cosign. Valid values include email address, DNS names, IP addresses, and URIs. Either --cosign-certificate-identity or --cosign-certificate-identity-regexp must be set for keyless flows --cosign-certificate-identity-regexp string A regular expression alternative to --cosign-certificate-identity for --verify=cosign. Accepts the Go regular expression syntax described at https://golang.org/s/re2syntax. Either --cosign-certificate-identity or --cosign-certificate-identity-regexp must be set for keyless flows --cosign-certificate-oidc-issuer string The OIDC issuer expected in a valid Fulcio certificate for --verify=cosign, e.g. https://token.actions.githubusercontent.com or https://oauth2.sigstore.dev/auth. Either --cosign-certificate-oidc-issuer or --cosign-certificate-oidc-issuer-regexp must be set for keyless flows --cosign-certificate-oidc-issuer-regexp string A regular expression alternative to --certificate-oidc-issuer for --verify=cosign. Accepts the Go regular expression syntax described at https://golang.org/s/re2syntax. Either --cosign-certificate-oidc-issuer or --cosign-certificate-oidc-issuer-regexp must be set for keyless flows --cosign-key string Path to the public key file, KMS, URI or Kubernetes Secret for --verify=cosign --cpu-period uint Limit CPU CFS (Completely Fair Scheduler) period --cpu-quota int Limit CPU CFS (Completely Fair Scheduler) quota (default -1) --cpu-shares uint CPU shares (relative weight) --cpus float Number of CPUs --cpuset-cpus string CPUs in which to allow execution (0-3, 0,1) --cpuset-mems string MEMs in which to allow execution (0-3, 0,1) -d, --detach Run container in background and print container ID --detach-keys string Override the default detach keys (default "ctrl-p,ctrl-q") --device strings Add a host device to the container --dns strings Set custom DNS servers --dns-opt strings Set DNS options --dns-option strings Set DNS options --dns-search strings Set custom DNS search domains --entrypoint stringArray Overwrite the default ENTRYPOINT of the image -e, --env stringArray Set environment variables --env-file strings Set environment variables from file --gpus stringArray GPU devices to add to the container ('all' to pass all GPUs) --group-add strings Add additional groups to join --help show help -h, --hostname string Container host name --init Run an init process inside the container, Default to use tini --init-binary string The custom binary to use as the init process (default "tini") -i, --interactive Keep STDIN open even if not attached --ip string IPv4 address to assign to the container --ip6 string IPv6 address to assign to the container --ipc string IPC namespace to use ("host"|"private") --ipfs-address string multiaddr of IPFS API (default uses $IPFS_PATH env variable if defined or local directory ~/.ipfs) --isolation string Specify isolation technology for container. On Linux the only valid value is default. Windows options are host, process and hyperv with process isolation as the default (default "default") --kernel-memory string Kernel memory limit (deprecated) -l, --label stringArray Set metadata on container --label-file strings Set metadata on container from file --log-driver string Logging driver for the container. Default is json-file. It also supports logURI (eg: --log-driver binary://<path>) (default "json-file") --log-opt stringArray Log driver options --mac-address string MAC address to assign to the container -m, --memory string Memory limit --memory-reservation string Memory soft limit --memory-swap string Swap limit equal to memory plus swap: '-1' to enable unlimited swap --memory-swappiness int Tune container memory swappiness (0 to 100) (default -1) (default -1) --mount stringArray Attach a filesystem mount to the container --name string Assign a name to the container --net strings Connect a container to a network ("bridge"|"host"|"none"|<CNI>) (default [bridge]) --network strings Connect a container to a network ("bridge"|"host"|"none"|"container:<container>"|<CNI>) (default [bridge]) --oom-kill-disable Disable OOM Killer --oom-score-adj int Tune container’s OOM preferences (-1000 to 1000, rootless: 100 to 1000) --pid string PID namespace to use --pidfile string file path to write the task's pid --pids-limit int Tune container pids limit (set -1 for unlimited) (default -1) --platform string Set platform (e.g. "amd64", "arm64") --privileged Give extended privileges to this container -p, --publish strings Publish a container's port(s) to the host --pull string Pull image before running ("always"|"missing"|"never") (default "missing") --rdt-class string Name of the RDT class (or CLOS) to associate the container with --read-only Mount the container's root filesystem as read only --restart string Restart policy to apply when a container exits (implemented values: "no"|"always|on-failure:n|unless-stopped") (default "no") --rm Automatically remove the container when it exits --rootfs The first argument is not an image but the rootfs to the exploded container --runtime string Runtime to use for this container, e.g. "crun", or "io.containerd.runsc.v1" (default "io.containerd.runc.v2") --security-opt stringArray Security options --shm-size string Size of /dev/shm --stop-signal string Signal to stop a container (default "SIGTERM") --stop-timeout int Timeout (in seconds) to stop a container --sysctl stringArray Sysctl options --tmpfs stringArray Mount a tmpfs directory -t, --tty Allocate a pseudo-TTY --ulimit strings Ulimit options --umask string Set the umask inside the container. Defaults to 0022 -u, --user string Username or UID (format: <name|uid>[:<group|gid>]) --uts string UTS namespace to use --verify string Verify the image (none|cosign|notation) (default "none") -v, --volume stringArray Bind mount a volume --volumes-from stringArray Mount volumes from the specified container(s) -w, --workdir string Working directory inside the container
See also 'nerdctl --help' for the global flags such as '--namespace', '--snapshotter', and '--cgroup-manager'.
exec
同样也可以使用 exec 命令执行容器相关命令,例如:
1 2
➜ ~ nerdctl exec -it ea07355852eb date Mon Nov 18 03:15:06 UTC 2024
➜ ~ nerdctl -n k8s.io ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e7f156d31942 docker.io/prom/node-exporter:v1.4.0 "/bin/node_exporter …" 2 days ago Up k8s://node-exporter/node-exporter-5zhjt/main bf1704937991 hub.cloud.ctripcorp.com/k8s-mirror/pause-amd64:3.1 "/pause" 2 days ago Up k8s://node-exporter/node-exporter-5zhjt
Commands: build Build an image from a Dockerfile. Needs buildkitd to be running. convert convert an image decrypt decrypt an image encrypt encrypt image layers history Show the history of an image inspect Display detailed information on one or more images. load Load an image from a tar archive or STDIN ls List images prune Remove unused images 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. rm Remove one or more images save Save one or more images to a tar archive (streamed to STDOUT by default) tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
➜ ~ ➜ ~ nerdctl -n k8s.io images REPOSITORY TAG IMAGE ID CREATED SIZE prom/node-exporter <none> 4a2c72aa0e18 2 days ago linux/amd64 24.56MB 11.47MB <none> <none> 4a2c72aa0e18 2 days ago linux/amd64 24.56MB 11.47MB prom/node-exporter v1.4.0 4a2c72aa0e18 2 days ago linux/amd64 24.56MB 11.47MB
➜ ~ crictl pods POD ID CREATED STATE NAME NAMESPACE ATTEMPT RUNTIME 1f617ebf0524c 8 weeks ago Ready node-exporter-ggjwf node-exporter 0 (default) 4b93e3d6e8d1f 2 months ago Ready node-problem-detector-r6ps6 kube-system 0 (default)
还可以使用 --name 参数获取指定的 Pod,也可以根据标签来筛选 Pod 列表:
1 2 3
➜ ~ crictl pods --label app=node-exporter POD ID CREATED STATE NAME NAMESPACE ATTEMPT RUNTIME 1f617ebf0524c 8 weeks ago Ready node-exporter-ggjwf node-exporter 0 (default)
镜像管理
使用 crictl images 命令可以获取所有的镜像:
1 2 3
➜ ~ crictl images IMAGE TAG IMAGE ID SIZE hub.cloud.ctripcorp.com/prom/node-exporter v1.4.0 d3e443c987ef4 11.5MB
➜ ~ crictl ps CONTAINER IMAGE CREATED STATE NAME ATTEMPT POD ID e7f156d31942f d3e443c987ef4 2 days ago Running main 2 bf1704937991a node-exporter-5zhjt
还有更多其他可选参数,使用 -s 选项按照状态进行过滤:
1 2 3
➜ ~ crictl ps -s Exited CONTAINER IMAGE CREATED STATE NAME ATTEMPT POD ID POD 22a0adacf702f d3e443c987ef4 2 days ago Exited main 1 bf1704937991a node-exporter-5zhjt
crictl 也有类似 exec 的命令支持,比如在容器 ID 为 e7f156d31942f 的容器中执行一个 whoami 命令:
1 2
➜ ~ crictl exec -it e7f156d31942f whoami root
还可以获取容器日志信息:
1 2 3 4 5
➜ ~ crictl logs e7f156d31942f ts=2024-11-15T08:37:07.980Z caller=node_exporter.go:182 level=info msg="Starting node_exporter" version="(version=1.4.0, branch=HEAD, revision=7da1321761b3b8dfc9e496e1a60e6a476fec6018)" ts=2024-11-15T08:37:07.980Z caller=node_exporter.go:183 level=info msg="Build context" build_context="(go=go1.19.1, user=root@83d90983e89c, date=20220926-12:32:56)" ts=2024-11-15T08:37:07.980Z caller=node_exporter.go:185 level=warn msg="Node Exporter is running as root user. This exporter is designed to run as unprivileged user, root is not required." ...
和 kubectl logs 类似于,还可以使用 -f 选项来 Follow 日志输出,--tail N 也可以指定输出最近的 N 行日志。
使用 crictl stats 命令可以列出所有或者单一容器资源的使用情况:
1 2 3 4
➜ ~ crictl stats e7f156d31942f
CONTAINER NAME CPU % MEM DISK INODES e7f156d31942f main 0.00 37.13MB 0B 18