How to use the command 'ctr' (with examples)
- Linux
- December 25, 2023
The ctr
command is used to manage containers and images in containerd
, which is a high-performance container runtime. It provides a set of commands to list containers and images, pull images, and tag images.
Use case 1: List all containers (running and stopped)
Code:
ctr containers list
Motivation: This use case is useful when you want to get an overview of all the containers that are currently running or have been stopped.
Explanation:
ctr
- The command itself.containers
- The subcommand that operates on containers.list
- The specific action to list containers.
Example output:
CONTAINER ID IMAGE NAME PID STATUS
8e7ad3f20739 registry.aliyuncs.com/library/busybox:latest 20417 Running
3a3cf70adb56 registry.aliyuncs.com/library/nginx:latest 23579 Exited
Use case 2: List all images
Code:
ctr images list
Motivation: This use case is helpful when you want to see a list of all the images available in containerd
.
Explanation:
ctr
- The command itself.images
- The subcommand that operates on images.list
- The specific action to list images.
Example output:
REF TYPE DIGEST SIZE PLATFORMS
registry.aliyuncs.com/library/busybox:latest application/vnd.docker.distribution.manifest.list.v2+json sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 2.6 MiB linux/amd64
registry.aliyuncs.com/library/nginx:latest application/vnd.docker.distribution.manifest.list.v2+json sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 191 MiB linux/amd64
Use case 3: Pull an image
Code:
ctr images pull image
Motivation: Use this use case when you want to retrieve an image from a container registry or repository and make it available in containerd
.
Explanation:
ctr
- The command itself.images
- The subcommand that operates on images.pull
- The specific action to pull an image.image
- The name or reference of the image to pull.
Example output:
docker.io/library/alpine:latest: resolved |+++++++++++++++++++++++++++++++++++++|
docker.io/library/alpine:latest: download started |+++++++++++++++++++++++++++++++++++++|
docker.io/library/alpine:latest: download complete |+++++++++++++++++++++++++++++++++++++| 2/2 MB
docker.io/library/alpine:latest:import complete
Use case 4: Tag an image
Code:
ctr images tag source_image:source_tag target_image:target_tag
Motivation: This use case is handy when you want to assign a different name and tag to an existing image.
Explanation:
ctr
- The command itself.images
- The subcommand that operates on images.tag
- The specific action to tag an image.source_image:source_tag
- The original image and tag that you want to tag.target_image:target_tag
- The new image and tag that you want to assign.
Example output:
Tagged [source_image:source_tag] as [target_image:target_tag]
Conclusion:
In this article, we explored the various use cases of the ctr
command. We learned how to list containers, list images, pull images, and tag images using ctr
. Each of these use cases is helpful in managing containers and images effectively in containerd
.