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

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

什么是容器?


当前回答

A docker image or a container image in general is a software package which wraps your application executable, dependencies, configurations and application runtime into a secure and immutable unit. When I am saying application executable it differs from application to application. For example if it is a java application it would a jar file, for node application may be a js file. Similarly the application runtime depends on your application itself. For java application it will be JRE, for node application it is node binaries.

您可以根据映像清单文件中提供的说明构建映像。Dockerfile)。构建映像后,可以将其存储在本地或一些容器映像存储库(如hub.docker.com)中。

当您希望将映像作为应用程序工作负载运行时,您可以启动一个需要映像的容器。容器是映像的运行时实例。

要构建一个映像,存储它并将其作为容器运行,你需要一个名为容器运行时的软件,比如Docker。这些容器运行时符合Open container Initiative为映像创建和容器运行提供的标准。

其他回答

摘自我关于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]删除无用的、停止的容器后面的映像。

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 Client, Server, Machine, Images, Hub, composites都是项目工具和软件,聚在一起形成了一个平台,在这个平台上,围绕着创建和运行一些叫做容器的东西,现在如果你运行Docker run redis命令,一个叫做Docker CLI的东西到达了一个叫做Docker Hub的东西,它下载了一个叫做image的文件。

码头工人形象:

一个镜像是一个单独的文件,包含运行一个非常特定的程序所需的所有依赖项和所有配置,例如redis是你刚刚下载的(通过运行docker run redis命令)应该运行的镜像。

这是存储在硬盘上的单个文件,在某些时候,您可以使用此映像来创建称为容器的东西。

容器是一个映像的实例你可以把它想象成一个运行的程序它有自己孤立的一组硬件资源它有自己的一小块内存有自己的一小块网络技术空间也有自己的一小块硬盘空间。

现在让我们看看当你发出下面的命令时: Sudo docker run hello-world

Above command will starts up the docker client or docker CLI, Docker CLI is in charge of taking commands from you kind of doing a little bit of processing on them and then communicating the commands over to something called the docker server, and docker server is in charge of the heavy lifting when we ran the command Docker run hello-world, That meant that we wanted to start up a new container using the image with the name of hello world, the hello world image has a tiny tittle program inside of it whose sole purpose or sole job is to print out the message that you see in the terminal.

现在,当我们运行该命令并将其发送到docker服务器时,后台很快就会发生一系列操作。Docker服务器看到我们试图使用名为hello world的映像启动一个新容器。

docker服务器做的第一件事是检查它是否已经有一个本地副本,比如你个人机器上的hello world映像或那个hello world文件的副本。因此,docker服务器研究了一种叫做图像缓存的东西。

现在,因为你和我刚刚在我们的个人电脑上安装了Docker,图像缓存目前是空的,我们没有之前已经下载过的图像。

因此,由于映像缓存是空的,docker服务器决定求助于一个名为docker hub的免费服务。Docker Hub是一个免费的公共映像库,您可以免费下载并在个人计算机上运行。所以Docker服务器联系了Docker Hub,下载了hello world文件,并将其存储在你的计算机上的image-cache中,现在它可以在未来的某个时候重新运行,而不必从Docker Hub重新下载它。

之后,docker服务器将使用它创建一个容器的实例,我们知道容器是一个映像的实例,它的唯一目的是运行一个非常特定的程序。因此,docker服务器从图像缓存中取出图像文件,并将其加载到内存中,从中创建一个容器,然后在其中运行一个程序。这个程序的目的就是打印出你看到的信息。

什么是容器: 首先,图像是如何创建容器的蓝图。

容器是一个进程或一组进程的资源专门分配给它的分组,在波形图,当我们考虑一个集装箱我们有一些正在运行的进程发送一个系统调用内核,内核会看那个传入系统调用,直接一个非常具体的部分硬盘,内存,CPU或其他什么它可能需要这些资源的一部分用于奇异的过程。

映像的实例称为容器。你有一个图像,它是你描述的一组图层。如果启动这个映像,就有了这个映像的运行容器。同一个映像可以有多个正在运行的容器。

你可以用docker images看到你所有的图像,而你可以用docker ps看到你正在运行的容器(你可以用docker ps -a看到所有的容器)。

因此,映像的运行实例就是一个容器。

正如许多回答指出的那样:你构建Dockerfile来获取一个图像,然后运行image来获取一个容器。

但是,下面的步骤帮助我更好地了解Docker映像和容器是什么:

1)构建Dockerfile:

Docker build -t my_image dir_with_dockerfile

2)保存镜像到.tar文件

-o my_file.tar my_image_id

My_file.tar将存储映像。使用tar -xvf my_file.tar打开它,您将看到所有的层。如果你深入每一层,你可以看到每一层添加了什么变化。(它们应该非常接近Dockerfile中的命令)。

3)要查看容器内部,您可以:

Sudo docker运行- my_image bash

你可以看到它很像一个操作系统。