在使用Docker时,我们从一个基本映像开始。我们启动它,创建更改,这些更改被保存在图层中形成另一个图像。

因此,最终我为我的PostgreSQL实例和我的web应用程序提供了一个图像,对这些图像的更改将持续保存。

什么是容器?


当前回答

我认为最好一开始就解释清楚。

假设你运行命令docker run hello-world。会发生什么呢?

它调用Docker CLI,负责接收Docker命令并转换为调用Docker服务器命令。一旦Docker服务器得到一个运行映像的命令,它就会检查映像缓存中是否有这样一个名称的映像。

假设hello-world不存在。Docker服务器访问Docker Hub (Docker Hub只是一个免费的图像存储库)并询问,嘿,Hub,你有一个名为hello-world的图像吗? 枢纽响应-是的,我有。那就请给我吧。下载过程开始了。一旦Docker镜像下载完成,Docker服务器就会将其放入镜像缓存中。

所以在我们解释Docker镜像和Docker容器是什么之前,让我们先来介绍一下你电脑上的操作系统以及它是如何运行软件的。

例如,当你在电脑上运行Chrome时,它会调用操作系统,操作系统自己会调用内核并请求,嘿,我想运行这个程序。内核设法从您的硬盘运行文件。

现在假设你有两个程序,Chrome和Node.js。Chrome需要Python版本2才能运行,Node.js需要Python版本3才能运行。如果您的计算机上只安装了Python v2,则只能运行Chrome。

为了使这两种情况都能工作,您需要以某种方式使用称为名称空间的操作系统特性。命名空间是一种特性,它使您有机会隔离进程、硬盘驱动器、网络、用户、主机名等。

当我们讨论映像时,我们实际上是在讨论文件系统快照。映像是一个物理文件,其中包含构建特定容器的方向和元数据。容器本身是一个映像的实例;它使用名称空间隔离硬盘驱动器,该名称空间仅对该容器可用。因此容器是一个进程或一组进程,它将分配给它的不同资源分组。

其他回答

虽然将容器看作一个运行的映像是最简单的,但这并不十分准确。

An image is really a template that can be turned into a container. To turn an image into a container, the Docker engine takes the image, adds a read-write filesystem on top and initialises various settings including network ports, container name, ID and resource limits. A running container has a currently executing process, but a container can also be stopped (or exited in Docker's terminology). An exited container is not the same as an image, as it can be restarted and will retain its settings and any filesystem changes.

Dockerfile类似于生成tarball (Docker映像)的Bash脚本。

Docker容器就像tarball的提取版本。您可以在不同的文件夹(容器)中拥有尽可能多的副本。

简单地说,如果一个图像是一个类,那么容器就是一个类的实例,一个运行时对象。

长话短说。

码头工人图片:

用于创建容器的文件系统和配置(只读)应用程序。

码头工人的容器:

容器和映像之间的主要区别是最上面的可写层。容器是顶层可写层的Docker映像的运行实例。容器运行实际的应用程序。容器包括应用程序及其所有依赖项。当容器被删除时,可写层也会被删除。底层图像保持不变。


其他需要注意的重要术语:

码头工人守护进程:

运行在主机上的后台服务,它管理构建,运行和分发Docker容器。

码头工人客户:

允许用户与Docker守护进程交互的命令行工具。

码头工人商店:

Store是一个Docker映像的注册表。您可以将注册表视为所有可用Docker映像的目录

这篇博文中的一张图片胜过千言万语。

简介:

从Docker中心提取图像或从Dockerfile构建=>给出一个 Docker图像(不可编辑)。 运行image (docker Run image_name:tag_name) =>给出一个运行 图片即容器(可编辑)

An image is like a class and container is like an object that class and so you can have an infinite number of containers behaving like the image. A class is a blueprint which isnt doing anything on its own. You have to create instances of the object un your program to do anything meaningful. And so is the case with an image and a container. You define your image and then create containers running that image. It isnt exactly similar because object is an instance of a class whereas a container is something like an empty hollow place and you use the image to build up a running host with exactly what the image says