简体   繁体   English

为什么 docker 必须从 dockerfile 创建映像,然后从映像创建容器,而不是从 Dockerfile 创建容器?

[英]Why does docker have to create an image from a dockerfile then create a container from the image instead of creating a container from a Dockerfile?

Why does docker have to create an image from a dockerfile then create a container from the image instead of creating a container directly from a Dockerfile?为什么 docker 必须从 dockerfile 创建映像,然后从映像创建容器,而不是直接从 Dockerfile 创建容器?

What is the purpose/benefit of creating the image first from the Dockerfile then from that create a container?首先从 Dockerfile 创建图像然后从创建容器的目的/好处是什么?

-----EDIT----- - - -编辑 - - -

This question What is the difference between a Docker image and a container?这个问题Docker 镜像和容器有什么区别? Does not answer my question.不回答我的问题。

My question is: Why do we need to create a container from an image and not a dockerfile?我的问题是:为什么我们需要从图像而不是 dockerfile 创建容器? What is the purpose/benefit of creating the image first from the Dockerfile then from that create a container?首先从 Dockerfile 创建图像然后从创建容器的目的/好处是什么?

  • the Dockerfile is the recipe to create an image Dockerfile 是创建图像的配方
  • the image is a virtual filesystem图像是一个虚拟文件系统
  • the container is the a running process on a host machine容器是主机上正在运行的进程

You don't want every host to build its own image based on the recipe.您不希望每个主机都根据配方构建自己的映像。 It's easier for some hosts to just download an image and work with that.对于某些主机来说,只需下载图像并使用它会更容易。

Creating an image can be very expensive.创建图像可能非常昂贵。 I have complicated Dockerfiles that may take hours to build, may download 50 GB of data, yet still only create a 200 MB image that I can send to different hosts.我有复杂的 Dockerfile,可能需要几个小时才能构建,可能会下载 50 GB 的数据,但仍然只能创建一个 200 MB 的图像,我可以将其发送到不同的主机。

Spinning up a container from an existing image is very cheap.从现有图像启动容器非常便宜。

If all you had was the Dockerfile in order to spin up image-containers, the entire workflow would become very cumbersome.如果您只有 Dockerfile 来启动图像容器,那么整个工作流程将变得非常繁琐。

Images and Containers are two different concepts.图像和容器是两个不同的概念。

Basically, images are like a snapshot of a filesystem, along with some meta-data.基本上,图像就像文件系统的快照,还有一些元数据。

A container is one of several process that are actually running (and which is based on an image).容器是实际运行的几个进程之一(并且基于图像)。 As soon as the processes end, your container do not exist anymore (well, it is stopped to be exact)一旦进程结束,您的容器就不再存在(准确地说,它已停止)

You can view the image as the base that you will make your container run on.您可以将映像视为运行容器的基础。

Thus, you Dockerfile will create an image (which is static) which you can store locally or push on a repository, to be able to use it later.因此,您 Dockerfile 将创建一个图像(静态的),您可以将其存储在本地或推送到存储库,以便以后使用。

The container cannot be "stored" because it is a "living" thing.容器不能“储存”,因为它是“活”的东西。

You can think of Images vs Containers similar to Classes vs Objects or the Definition vs Instance.您可以将图像与容器视为类似于类与对象或定义与实例。 The image contains the filesystem and default settings for creating the container.该映像包含用于创建容器的文件系统和默认设置。 The container contains the settings for a specific instance, and when running, the namespaces and running process.容器包含特定实例的设置,以及运行时的命名空间和运行进程。

As for why you'd want to separate them, efficiency and portability.至于为什么要将它们分开,效率和可移植性。 Since we have separate images, we also have inheritance, where one image extends another.由于我们有单独的图像,因此我们还有 inheritance,其中一个图像扩展了另一个图像。 The key detail of that inheritance is that filesystem layers in the image are not copied for each image. inheritance 的关键细节是不会为每个图像复制图像中的文件系统层。 Those layers are static, and you can them by creating a new image with new layers.这些层是 static,您可以通过使用新层创建新图像来实现它们。 Using the overlay filesystem (or one of the other union filesystem drivers) we can append additional changes to that filesystem with our new image.使用覆盖文件系统(或其他联合文件系统驱动程序之一),我们可以使用我们的新映像 append 对该文件系统进行额外更改。 Containers do the same when the run the image.容器在运行映像时执行相同的操作。 That means you can have a 1 Gig base image, extend it with a child image with 100 Megs of changes, and run 5 containers that each write 1 Meg of files, and the overall disk space used on the docker host is only 1.105 Gigs rather than 7.6 Gigs.这意味着您可以拥有一个 1 Gig 的基础映像,使用具有 100 Megs 更改的子映像对其进行扩展,并运行 5 个容器,每个容器写入 1 Meg 的文件,docker 主机上使用的总磁盘空间仅为 1.105 Gigs超过 7.6 演出。

The portability part comes into play when you use registries, eg Docker Hub.当您使用注册表时,可移植性部分会发挥作用,例如 Docker Hub。 The image is the part of the container that is generic, reusable, and transferable.图像是容器的通用、可重用和可转移的部分。 It's not associated with an instance on any host.它不与任何主机上的实例相关联。 So you can push and pull images, but containers are tightly bound to the host they are running on, named volumes on that host, networks defined on that host, etc.因此,您可以推送和拉取映像,但容器与它们正在运行的主机、该主机上的命名卷、该主机上定义的网络等紧密绑定。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM