在使用Docker时,我们从一个基本映像开始。我们启动它,创建更改,这些更改被保存在图层中形成另一个图像。
因此,最终我为我的PostgreSQL实例和我的web应用程序提供了一个图像,对这些图像的更改将持续保存。
什么是容器?
在使用Docker时,我们从一个基本映像开始。我们启动它,创建更改,这些更改被保存在图层中形成另一个图像。
因此,最终我为我的PostgreSQL实例和我的web应用程序提供了一个图像,对这些图像的更改将持续保存。
什么是容器?
当前回答
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]删除无用的、停止的容器后面的映像。
Dockerfile→(Build)→Image→(Run)→Container。
Dockerfile:包含一组Docker指令,以您喜欢的方式配置您的操作系统,并安装/配置所有软件。 图片:编译好的Dockerfile。节省了每次需要运行容器时重新构建Dockerfile的时间。这是一种隐藏供应代码的方法。 容器:虚拟操作系统本身。您可以ssh进入其中并运行任何命令,就像它是一个真实的环境一样。您可以从同一个映像运行1000多个容器。
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.
Docker容器正在运行一个映像的实例。你可以将图像与程序关联起来,将容器与进程关联起来:)
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或其他什么它可能需要这些资源的一部分用于奇异的过程。