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

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

什么是容器?


当前回答

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

其他回答

I would like to fill the missing part here between docker images and containers. Docker uses a union file system (UFS) for containers, which allows multiple filesystems to be mounted in a hierarchy and to appear as a single filesystem. The filesystem from the image has been mounted as a read-only layer, and any changes to the running container are made to a read-write layer mounted on top of this. Because of this, Docker only has to look at the topmost read-write layer to find the changes made to the running system.

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

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

Docker映像打包了应用程序运行所需的应用程序和环境,容器是映像的运行实例。

图像是Docker的包装部分,类似于“源代码”或“程序”。容器是Docker的执行部分,类似于“进程”。

在这个问题中,只提到了“程序”部分,这就是图像。Docker的“运行”部分是容器。当容器运行并进行更改时,就好像进程对自己的源代码进行了更改,并将其保存为新的映像。

Dockerfile→(Build)→Image→(Run)→Container。

Dockerfile:包含一组Docker指令,以您喜欢的方式配置您的操作系统,并安装/配置所有软件。 图片:编译好的Dockerfile。节省了每次需要运行容器时重新构建Dockerfile的时间。这是一种隐藏供应代码的方法。 容器:虚拟操作系统本身。您可以ssh进入其中并运行任何命令,就像它是一个真实的环境一样。您可以从同一个映像运行1000多个容器。

容器只是一个可执行的二进制文件,由主机操作系统在一组限制下运行,这些限制是由应用程序(例如Docker)预先设置的,该应用程序知道如何告诉操作系统应用哪些限制。

典型的限制是与进程隔离相关的、与安全性相关的(比如使用SELinux保护)和与系统资源相关的(内存、磁盘、CPU和网络)。

直到最近,只有基于unix的系统中的内核支持在严格限制下运行可执行文件的能力。这就是为什么今天大多数容器讨论主要涉及Linux或其他Unix发行版的原因。

Docker是知道如何告诉操作系统(主要是Linux)在什么限制下运行可执行文件的应用程序之一。可执行文件包含在Docker映像中,它只是一个tarfile。该可执行文件通常是Linux发行版用户空间(Ubuntu、CentOS、Debian等)的精简版本,预先配置为在其中运行一个或多个应用程序。

尽管大多数人使用Linux基础文件作为可执行文件,但它可以是任何其他二进制应用程序,只要主机操作系统的内核可以运行它(参见使用scratch创建一个简单的基础映像)。无论Docker镜像中的二进制文件是一个OS用户空间还是一个简单的应用程序,对于OS主机来说,它只是另一个进程,一个被预设的OS边界所控制的包含进程。

其他应用程序,如Docker,可以告诉主机操作系统在进程运行时应用哪些边界,包括LXC、libvirt和systemd。Docker曾经使用这些应用程序间接地与Linux操作系统交互,但现在Docker使用自己的库“libcontainer”直接与Linux交互。

因此容器只是在受限模式下运行的进程,类似于chroot的功能。

在我看来,Docker与其他容器技术的区别在于它的存储库(Docker Hub)和管理工具,这些工具使得使用容器变得非常容易。

参见Docker(软件)。