简体   繁体   English

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

[英]How to run docker images in containerd using ctr in CLI?

Am exploring on how to use containerd in place of dockerd.我正在探索如何使用containerd代替 dockerd。 This is for learning only and as a cli tool rather than with any pipelines or automation.这仅用于学习和作为 cli 工具,而不是任何管道或自动化。

So far, documentation in regards to using containerd in cli (via ctr ) is very limited.到目前为止,关于在 cli(通过ctr )中使用 containerd 的文档非常有限。 Even the official docs are using Go lang to utilize containerd directly.甚至官方文档也在使用 Go lang 来直接使用containerd

What I have learnt is ctr command plays the role of docker command to control containerd .我的心得是ctr命令扮演的角色docker命令来控制containerd I have thus far created a docker image and exported it to .tar format.到目前为止,我已经创建了一个 docker 映像并将其导出为 .tar 格式。 Now using ctr i import hello.tar I have imported it as an image.现在使用ctr i import hello.tar我已将其作为图像导入。

Now ctr i ls gives me the following output:现在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 -

Trying to run a container asks me for the image id:尝试运行容器要求我提供图像 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.我不确定从哪里获取图像 ID。 Are there any docs related to ctr or containerd that could be helpful for a beginner?是否有任何与ctrcontainerd相关的文档对初学者有帮助?

Just running the image as a container would be sufficient for me.仅将图像作为容器运行对我来说就足够了。

The ctr run command creates a container and executes it ctr run命令创建一个容器并执行它

ctr run <imageName> <uniqueValue>

eg, ctr run docker.io/library/hello-java-app:latest v1 --rm例如,ctr 运行 docker.io/library/hello-java-app:latest v1 --rm

This executes my basic docker java image with a print statement:这将使用打印语句执行我的基本 docker java 图像:

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

Steps followed:步骤如下:

1 - A java file: 1 - 一个java文件:

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

2 - An image: 2 - 图像:

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

3 - Build image and export as .tar 3 - 构建映像并导出为 .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: 4 - 将图像 (.tar) 导入 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: 5 - 运行图像:

  • 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. run 命令创建一个容器并执行一次。 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. ctr c create只是创建一个容器,然后可以用ctr c ls列出它,但我无法以任何有意义的方式利用它们。 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 PS:如果没有 --rm 标志,每次run都需要输入一个新的唯一值,因为旧容器被保留,我们得到一个错误: ctr: snapshot "v1": already exists

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 containerd/ctr 中停止容器 - How to stop container in containerd/ctr Docker CLI &amp; containerd 在 Windows - Docker CLI & containerd on Windows 如何删除容器使用 containerd ctr? - How to delete container use containerd ctr? 无法直接使用基于容器的 CLI 从 Docker 管理的容器中检索数据 - Unable to retrieve data from Docker-managed containerd by using containerd-based CLI directly dockerd vs docker-containerd vs docker-runc vs docker-containerd-ctr vs docker-containerd-shim - dockerd vs docker-containerd vs docker-runc vs docker-containerd-ctr vs docker-containerd-shim 使用 containerd 的 ctr 作为容器中的另一个用户执行命令 - Execute command as another user in container using containerd's ctr 如何使用 nerdctl 加载多个 tar 图像? (容器) - How can I load multiple tar images using nerdctl? (containerd) 通过 uidmap 到 map 向主机上的非 root 用户运行具有 containerd 的 ctr 的容器 - run container with containerd's ctr by means of uidmap to map to non-root user on the host 是什么导致传输​​:“拨号 unix /var/run/docker/containerd/docker-containerd.sock: connect: connection denied”? - What causes transport: “dial unix /var/run/docker/containerd/docker-containerd.sock: connect: connection refused”? Containerd:相当于 docker 在 containerd 中检查 - Containerd: Equivalent of docker inspect in containerd
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM