Docker映像和容器之间有什么区别?

本文翻译自:What is the difference between a Docker image and a container?

When using Docker, we start with a base image. 使用Docker时,我们从基础映像开始。 We boot it up, create changes and those changes are saved in layers forming another image. 我们启动它,创建更改,并将这些更改保存在形成另一个映像的层中。

So eventually I have an image for my PostgreSQL instance and an image for my web application, changes to which keep on being persisted. 因此,最终我为自己的PostgreSQL实例提供了一个映像,为我的Web应用程序提供了一个映像,对它们的更改将继续保留。

What is a container? 什么是容器?


#1楼

参考:https://stackoom.com/question/1baaz/Docker映像和容器之间有什么区别


#2楼

An instance of an image is called a container. 图像的实例称为容器。 You have an image, which is a set of layers as you describe. 您有一张图像,该图像是您描述的一组图层。 If you start this image, you have a running container of this image. 如果启动此映像,则该映像具有正在运行的容器。 You can have many running containers of the same image. 您可以有多个运行中的同一图像容器。

You can see all your images with docker images whereas you can see your running containers with docker ps (and you can see all containers with docker ps -a ). 您可以看到带有docker images所有映像,而可以看到带有docker ps正在运行的容器(并且可以看到带有docker ps -a所有容器)。

So a running instance of an image is a container. 因此,映像的运行实例是一个容器。


#3楼

While it's simplest to think of a container as a running image, this isn't quite accurate. 虽然将容器视为运行映像是最简单的,但这并不是准确。

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. 为了将映像转换为容器,Docker引擎会获取映像,在顶部添加一个读写文件系统,并初始化各种设置,包括网络端口,容器名称,ID和资源限制。 A running container has a currently executing process, but a container can also be stopped (or exited in Docker's terminology). 正在运行的容器具有当前正在执行的进程,但是也可以停止容器(或在Docker的术语中退出容器)。 An exited container is not the same as an image, as it can be restarted and will retain its settings and any filesystem changes. 一个退出容器是一样的图像,因为它可以重新启动,将保留其设置和文件系统的任何变化。


#4楼

From my article on Automating Docker Deployments : 从我有关自动化Docker部署的文章中:

Docker Images vs. Containers Docker映像与容器

In Dockerland, there are images and there are containers . 在Dockerland中,有图像并且有容器 The two are closely related, but distinct. 两者密切相关,但又截然不同。 For me, grasping this dichotomy has clarified Docker immensely. 对我而言,掌握这一二分法极大地阐明了Docker。

What's an Image? 什么是图片?

An image is an inert, immutable, file that's essentially a snapshot of a container. 图像是惰性的,不可变的文件,本质上是容器的快照。 Images are created with the build command, and they'll produce a container when started with run . 图像是使用build命令创建的,并且从run开始会生成一个容器。 Images are stored in a Docker registry such as registry.hub.docker.com . 图像存储在Docker注册表中,例如Registry.hub.docker.com Because they can become quite large, images are designed to be composed of layers of other images, allowing a minimal amount of data to be sent when transferring images over the network. 由于图像可能会变得很大,因此图像被设计为由其他图像层组成,从而在通过网络传输图像时允许发送最少量的数据。

Local images can be listed by running docker images : 可以通过运行docker images列出本地docker images

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

Some things to note: 注意事项:

  1. IMAGE ID is the first 12 characters of the true identifier for an image. IMAGE ID是图像的真实标识符的前12个字符。 You can create many tags of a given image, but their IDs will all be the same (as above). 您可以为给定图像创建许多标签,但它们的ID都相同(如上所述)。
  2. 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. 这意味着该列中所有值的总和可能比所有这些映像使用的磁盘空间大得多。
  3. The value in the REPOSITORY column comes from the -t flag of the docker build command, or from docker tag -ing an existing image. REPOSITORY列中的值来自docker build命令的-t标志,或者来自docker tag现有映像。 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 . 您可以使用对您有意义的命名法来随意标记图像,但是知道docker将在docker pushdocker pull中将标记用作注册表位置。
  4. The full form of a tag is [REGISTRYHOST/][USERNAME/]NAME[:TAG] . 标记的完整格式为[REGISTRYHOST/][USERNAME/]NAME[:TAG] For ubuntu above, REGISTRYHOST is inferred to be registry.hub.docker.com . 对于上述ubuntu ,推断REGISTRYHOST为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 . 因此,如果计划在docker.example.com的注册表中存储名为my-application docker.example.com ,则应标记该映像docker.example.com/my-application
  5. The TAG column is just the [:TAG] part of the full tag. TAG列只是完整标签的[:TAG]部分。 This is unfortunate terminology. 这是不幸的术语。
  6. The latest tag is not magical, it's simply the default tag when you don't specify a tag. latest标签不是神奇的标签,它只是未指定标签时的默认标签。
  7. You can have untagged images only identifiable by their IMAGE IDs. 您可以具有只能通过其图像ID识别的未标记图像。 These will get the <none> TAG and REPOSITORY. 这些将获得<none>标记和存储库。 It's easy to forget about them. 忘记它们很容易。

More information on images is available from the Docker documentation and glossary . 有关映像的更多信息,请参阅Docker文档词汇表

What's a container? 什么是容器?

To use a programming metaphor, if an image is a class, then a container is an instance of a class—a runtime object. 要使用编程隐喻,如果图像是类,则容器是类的实例-运行时对象。 Containers are hopefully why you're using Docker; 容器是希望您使用Docker的原因。 they're lightweight and portable encapsulations of an environment in which to run applications. 它们是用于运行应用程序的环境的轻巧且可移植的封装。

View local running containers with docker ps : 使用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

Here I'm running a dockerized version of the docker registry, so that I have a private place to store my images. 在这里,我正在运行docker注册表的dockerized版本,因此我有一个私人位置来存储我的图像。 Again, some things to note: 同样,需要注意一些事项:

  1. Like IMAGE ID, CONTAINER ID is the true identifier for the container. 与IMAGE ID相似,CONTAINER ID是容器的真实标识符。 It has the same form, but it identifies a different kind of object. 它具有相同的形式,但是它标识不同种类的对象。
  2. docker ps only outputs running containers. docker ps仅输出正在运行的容器。 You can view all containers ( running or stopped ) with docker ps -a . 您可以使用docker ps -a查看所有容器( 正在运行或已停止 )。
  3. NAMES can be used to identify a started container via the --name flag. NAMES可通过--name标志用于标识已启动的容器。

How to avoid image and container buildup 如何避免图像和容器堆积

One of my early frustrations with Docker was the seemingly constant buildup of untagged images and stopped containers . 我对Docker的早期挫败之一是看似不间断的未标记映像和停止的容器的堆积 On a handful of occasions this buildup resulted in maxed out hard drives slowing down my laptop or halting my automated build pipeline. 在少数情况下,这种堆积会导致硬盘驱动器用尽,从而减慢我的笔记本电脑速度或停止自动构建流程。 Talk about "containers everywhere"! 谈论“无处不在的容器”!

We can remove all untagged images by combining docker rmi with the recent dangling=true query: 我们可以通过结合docker rmi和最近的dangling=true查询来删除所有未标记的图像:

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

Docker won't be able to remove images that are behind existing containers, so you may have to remove stopped containers with docker rm first: Docker无法删除现有容器后面的映像,因此您可能必须首先使用docker rm删除已停止的容器:

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

These are known pain points with Docker and may be addressed in future releases. 这些是Docker的已知痛点 ,可能会在将来的版本中解决。 However, with a clear understanding of images and containers, these situations can be avoided with a couple of practices: 但是,只要对图像和容器有清楚的了解,可以通过以下两种方法避免这些情况:

  1. Always remove a useless, stopped container with docker rm [CONTAINER_ID] . 始终使用docker rm [CONTAINER_ID]删除无用的,停止的容器。
  2. Always remove the image behind a useless, stopped container with docker rmi [IMAGE_ID] . 始终使用docker rmi [IMAGE_ID]删除无用的,已停止的容器后面的图像。

#5楼

The core concept of Docker is to make it easy to create "machines" which in this case can be considered containers. Docker的核心概念是简化创建“机器”的过程,在这种情况下可以将其视为容器。 The container aids in reusability, allowing you to create and drop containers with ease. 容器有助于重用性,使您可以轻松地创建和删除容器。

Images depict the state of a container at every point in time. 图像描述了每个时间点的容器状态。 So the basic workflow is: 所以基本的工作流程是:

  1. create an image 创建图像
  2. start a container 启动一个容器
  3. make changes to the container 对容器进行更改
  4. save the container back as an image 将容器另存为图像

#6楼

Maybe explaining the whole workflow can help. 也许解释整个工作流程会有所帮助。

Everything starts with the Dockerfile . 一切都始于Dockerfile The Dockerfile is the source code of the image. Dockerfile是映像的源代码。

Once the Dockerfile is created, you build it to create the image of the container. 创建Dockerfile后,您将对其进行构建以创建容器的映像 The image is just the "compiled version" of the "source code" which is the Dockerfile. 该图像只是“源代码”(即Dockerfile)的“编译版本”。

Once you have the image of the container, you should redistribute it using the registry . 有了容器的映像后,应使用注册表重新分配它。 The registry is like a Git repository -- you can push and pull images. 注册表就像一个Git存储库-您可以推送和拉取图像。

Next, you can use the image to run containers . 接下来,您可以使用该图像运行container A running container is very similar, in many aspects, to a virtual machine (but without the hypervisor ). 在许多方面,一个正在运行的容器与虚拟机非常相似(但没有虚拟机管理程序 )。

发布了0 篇原创文章 · 获赞 7 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/105408300