问题描述
正在探索如何使用 containerd
代替 dockerd.这仅用于学习和作为 cli 工具,而不是用于任何管道或自动化.
到目前为止,关于在 cli 中使用 containerd(通过 ctr
)的文档非常有限.甚至官方文档都在使用 Go lang 直接利用 containerd
.
我学到的是ctr
命令扮演docker
命令的角色来控制containerd
.到目前为止,我已经创建了一个 docker 映像并将其导出为 .tar 格式.现在使用 ctr i import hello.tar
我已将其作为图像导入.
现在 ctr i ls
给我以下输出:
参考类型摘要尺寸平台标签docker.io/library/hello-java-app:latest application/vnd.oci.image.manifest.v1+json sha256:ef4acfd85c856ea020328959ff3cac23f37fa639b7edfb1691619d9bfe1e06c7 628.7 MiB linux/amd64 -
尝试运行容器时要求我提供图像 ID:
root@slave-node:~/images/sample# ctr runctr:必须提供图像参考root@slave-node:~/images/sample# ctr run docker.io/library/hello-java-app:latestctr:必须提供容器 ID
我不确定从哪里获取图像 ID.是否有任何与 ctr
或 containerd
相关的文档对初学者有帮助?
仅将图像作为容器运行就足够了.
ctr run
命令创建一个容器并执行它
ctr run <imageName><唯一值>
例如,ctr run docker.io/library/hello-java-app:latest v1 --rm
这会使用打印语句执行我的基本 docker java 映像:
~~~~来自在 Docker 中运行的 Java 应用程序的 HelloWorld.~~~~
后续步骤:
1 - 一个 java 文件:
公共类 HelloWorld {公共静态无效主要(字符串[]参数){System.out.println("~~~~
来自 Docker 中运行的 Java 应用程序的 HelloWorld.
~~~~");}}
2 - 图片:
FROM java:8复制 HelloWorld.java .运行 javac HelloWorld.javaCMD ["java", "HelloWorld"]
3 - 构建镜像并导出为 .tar
docker build -t hello-java-app .
docker save -o ~/images/sample/hello-java-app.tar hello-java-app
4 - 将图像 (.tar) 导入到 containerd:
ctr i import hello-java-app.tar
解压 docker.io/library/hello-java-app:latest (sha256:ef4acfd85c856ea020328959ff3cac23f37fa639b7edfb1691619d9bfe1e06c7)...完成
ctr i ls
REF TYPE DIGEST SIZE PLATFORMS LABELSdocker.io/library/hello-java-app:latest application/vnd.oci.image.manifest.v1+json sha256:ef4acfd85c856ea020328959ff3cac23f37fa639b7edfb1691619d9bfe1e06c7 628.7 MiB linux/amd64 -
5 - 运行镜像:
ctr run docker.io/library/hello-java-app:latest v1 --rm
~~~~来自在 Docker 中运行的 Java 应用程序的 HelloWorld.~~~~
我仍然不确定创建容器的用途.run 命令创建一个容器并执行一次.ctr c create
只是创建一个容器,然后可以使用 ctr c ls
列出该容器,但我无法以任何有意义的方式使用它们.谁能澄清一下它的目的?
PS:如果没有 --rm 标志,则需要为每个 run
输入一个新的唯一值,因为旧容器被保留并且我们得到一个错误:ctr: snapshot "v1": already exists
Am exploring on how to use containerd
in place of dockerd. This is for learning only and as a cli tool rather than with any pipelines or automation.
So far, documentation in regards to using containerd in cli (via ctr
) is very limited. Even the official docs are using Go lang to utilize containerd
directly.
What I have learnt is ctr
command plays the role of docker
command to control containerd
. I have thus far created a docker image and exported it to .tar format. Now using ctr i import hello.tar
I have imported it as an image.
Now ctr i ls
gives me the following output:
REF TYPE DIGEST SIZE PLATFORMS LABELS
docker.io/library/hello-java-app:latest application/vnd.oci.image.manifest.v1+json sha256:ef4acfd85c856ea020328959ff3cac23f37fa639b7edfb1691619d9bfe1e06c7 628.7 MiB linux/amd64 -
Trying to run a container asks me for the image id:
root@slave-node:~/images/sample# ctr run
ctr: image ref must be provided
root@slave-node:~/images/sample# ctr run docker.io/library/hello-java-app:latest
ctr: container id must be provided
I am not sure on where to get the image id from. Are there any docs related to ctr
or containerd
that could be helpful for a beginner?
Just running the image as a container would be sufficient for me.
The ctr run
command creates a container and executes it
ctr run <imageName> <uniqueValue>
This executes my basic docker java image with a print statement:
~~~~
HelloWorld from Java Application running in Docker.
~~~~
Steps followed:
1 - A java file:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("~~~~
HelloWorld from Java Application running in Docker.
~~~~");
}
}
2 - An image:
FROM java:8
COPY HelloWorld.java .
RUN javac HelloWorld.java
CMD ["java", "HelloWorld"]
3 - Build image and export as .tar
docker build -t hello-java-app .
docker save -o ~/images/sample/hello-java-app.tar hello-java-app
4 - Import image (.tar) into containerd:
ctr i import hello-java-app.tar
unpacking docker.io/library/hello-java-app:latest (sha256:ef4acfd85c856ea020328959ff3cac23f37fa639b7edfb1691619d9bfe1e06c7)...done
ctr i ls
REF TYPE DIGEST SIZE PLATFORMS LABELS
docker.io/library/hello-java-app:latest application/vnd.oci.image.manifest.v1+json sha256:ef4acfd85c856ea020328959ff3cac23f37fa639b7edfb1691619d9bfe1e06c7 628.7 MiB linux/amd64 -
5 - Run the image:
ctr run docker.io/library/hello-java-app:latest v1 --rm
~~~~
HelloWorld from Java Application running in Docker.
~~~~
PS:Without the --rm flag, a new unique value is needed to be entered for every run
as the old container is retained and we get an error: ctr: snapshot "v1": already exists
这篇关于如何在 CLI 中使用 ctr 在 containerd 中运行 docker 映像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!