且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 CLI 中使用 ctr 在 containerd 中运行 docker 映像?

更新时间:2023-11-24 20:35:22

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>

e.g., ctr run docker.io/library/hello-java-app:latest v1 --rm

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.
~~~~


I am still unsure of the use of creating a container. The run command creates a container and executes it once. ctr c create just creates a container which can then be listed with ctr c ls but I am not able to utilize them in any meaningful way. Can anyone clarify its purpose?


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