本文介绍了如何在CLI中使用ctr在容器中运行docker镜像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在探索如何使用 containerd 代替dockerd.这仅用于学习,并且作为cli工具,而不是用于任何管道或自动化.

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.

到目前为止,有关使用cli中的容器(通过 ctr )的文档非常有限.甚至官方文档也使用Go lang直接使用 containerd .

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.

我了解到的是 ctr 命令扮演了 docker 命令来控制 containerd 的角色.到目前为止,我已经创建了一个docker映像并将其导出为.tar格式.现在使用 ctr导入hello.tar ,将其作为图像导入.

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.

现在 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 -

尝试运行容器会要求我提供图像ID:

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

我不确定从何处获取图像ID.是否有任何与 ctr containerd 相关的文档对初学者有帮助?

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?

仅将图像作为容器运行就足够了.

推荐答案

ctr run 命令创建并执行一个容器

The ctr run command creates a container and executes it

ctr run <imageName> <uniqueValue>

这将使用打印语句执行我的基本docker java映像:

This executes my basic docker java image with a print statement:

~~~~
HelloWorld from Java Application running in Docker.
~~~~

已执行的步骤:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("~~~~\nHelloWorld from Java Application running in Docker.\n~~~~");
    }
}

2-图片:

FROM java:8
COPY HelloWorld.java .
RUN javac HelloWorld.java
CMD ["java", "HelloWorld"]

3-构建图像并将其导出为.tar

  • docker build -t hello-java-app.
  • docker save -o〜/images/sample/hello-java-app.tar hello-java-app
    • ctr我导入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-运行图像:

      • ctr运行docker.io/library/hello-java-app:latest v1 --rm
      • ~~~~
        HelloWorld from Java Application running in Docker.
        ~~~~
        



        PS:如果没有--rm标志,则由于保留了旧容器,因此每个 run 都需要输入一个新的唯一值,并且会收到错误消息: ctr:快照"v1":已经存在


        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在容器中运行docker镜像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-01 11:36