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

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

什么是容器?


当前回答

我想用以下类比来说明:

+-----------------------------+-------+-----------+
|             Domain          | Meta  | Concrete  |
+-----------------------------+-------+-----------+
| Docker                      | Image | Container |
| Object oriented programming | Class | Object    |
+-----------------------------+-------+-----------+

其他回答

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

工作流

下面是端到端的工作流,显示各种命令及其相关的输入和输出。这应该澄清了映像和容器之间的关系。

+------------+  docker build   +--------------+  docker run -dt   +-----------+  docker exec -it   +------+
| Dockerfile | --------------> |    Image     | --------------->  | Container | -----------------> | Bash |
+------------+                 +--------------+                   +-----------+                    +------+
                                 ^
                                 | docker pull
                                 |
                               +--------------+
                               |   Registry   |
                               +--------------+

要列出你可以运行的图像,执行:

docker image ls

列出你可以执行命令的容器:

docker ps

映像或容器映像是一个包含应用程序代码、应用程序运行时、配置和依赖库的文件。映像基本上是将所有这些包装成一个单一的、安全的不可变单元。使用适当的docker命令构建映像。图像有图像id和图像标签。标签的格式通常为<docker-user-name>/image-name:tag。

当您开始使用映像运行应用程序时,实际上是启动了一个容器。你的容器是一个运行图像的沙盒。Docker软件用于管理映像和容器。

Image是一个安全的包,其中包含应用程序工件、库、配置和应用程序运行时。容器是映像的运行时表示形式。

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

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

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

摘自我关于Docker部署自动化的文章(存档):

Docker Images vs. Containers

在Dockerland,有图像,也有容器。这两者密切相关,但又截然不同。对我来说,把握这种二分法极大地澄清了Docker。

什么是图像?

映像是惰性的、不可变的文件,本质上是容器的快照。映像是用build命令创建的,当使用run启动时,映像将生成一个容器。图像存储在Docker注册表中,例如registry.hub.docker.com。由于图像可以变得相当大,因此图像被设计成由其他图像的层组成,在通过网络传输图像时允许发送最少的数据。

本地映像可以通过运行docker映像列出:

REPOSITORY                TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ubuntu                    13.10               5e019ab7bf6d        2 months ago        180 MB
ubuntu                    14.04               99ec81b80c55        2 months ago        266 MB
ubuntu                    latest              99ec81b80c55        2 months ago        266 MB
ubuntu                    trusty              99ec81b80c55        2 months ago        266 MB
<none>                    <none>              4ab0d9120985        3 months ago        486.5 MB

需要注意的一些事情:

IMAGE ID is the first 12 characters of the true identifier for an image. You can create many tags of a given image, but their IDs will all be the same (as above). VIRTUAL SIZE is virtual because it's adding up the sizes of all the distinct underlying layers. This means that the sum of all the values in that column is probably much larger than the disk space used by all of those images. The value in the REPOSITORY column comes from the -t flag of the docker build command, or from docker tag-ing an existing image. You're free to tag images using a nomenclature that makes sense to you, but know that docker will use the tag as the registry location in a docker push or docker pull. The full form of a tag is [REGISTRYHOST/][USERNAME/]NAME[:TAG]. For ubuntu above, REGISTRYHOST is inferred to be registry.hub.docker.com. So if you plan on storing your image called my-application in a registry at docker.example.com, you should tag that image docker.example.com/my-application. The TAG column is just the [:TAG] part of the full tag. This is unfortunate terminology. The latest tag is not magical, it's simply the default tag when you don't specify a tag. You can have untagged images only identifiable by their IMAGE IDs. These will get the <none> TAG and REPOSITORY. It's easy to forget about them.

更多关于图像的信息可以从Docker文档和术语表中获得。

容器是什么?

用一个编程的比喻来说,如果一个图像是一个类,那么容器就是一个类的实例——一个运行时对象。容器是你使用Docker的原因;它们是运行应用程序的环境的轻量级和可移植封装。

使用docker ps查看本地运行容器:

CONTAINER ID        IMAGE                               COMMAND                CREATED             STATUS              PORTS                    NAMES
f2ff1af05450        samalba/docker-registry:latest      /bin/sh -c 'exec doc   4 months ago        Up 12 weeks         0.0.0.0:5000->5000/tcp   docker-registry

这里我运行dockerized版本的docker注册表,这样我就有一个私有的地方来存储我的图像。这里有几点需要注意:

和IMAGE ID一样,CONTAINER ID是容器的真实标识符。它有相同的形式,但它识别了不同种类的物体。 Docker ps只输出正在运行的容器。你可以使用docker ps -a查看所有的容器(正在运行或已停止)。 NAMES可以通过——name标志来标识一个启动的容器。

如何避免图像和容器堆积

我早期使用Docker时遇到的一个挫折是,未标记的图像和停止的容器似乎不断堆积。在少数情况下,这种堆积导致硬盘驱动器过载,使我的笔记本电脑变慢或停止自动构建管道。谈论“无处不在的容器”!

我们可以通过结合docker rmi和最近的悬挂=true查询来删除所有未标记的图像:

docker images -q --filter "dangling=true" | xargs docker rmi

Docker将不能删除现有容器后面的图像,所以你可能必须先用Docker rm删除停止的容器:

docker rm `docker ps --no-trunc -aq`

这些都是Docker的已知痛点,可能会在未来的版本中得到解决。然而,通过对映像和容器的清晰理解,可以通过以下几个实践来避免这些情况:

总是使用docker rm [CONTAINER_ID]删除无用的、停止的容器。 总是使用docker rmi [IMAGE_ID]删除无用的、停止的容器后面的映像。