Docker中容器和图像的区别是什么?在Docker入门教程中,这两个术语都被使用了,但我不明白其中的区别。

谁能给我点灯?


当前回答

映像基本上是用于创建容器的不可变模板。通过考虑将图像转换为容器所发生的变化,可以更容易地理解图像和容器之间的区别。

The Docker engine takes the image and adds a read-write filesystem on top, then initialises various settings. These settings include network options (IP, port, etc.), name, ID, and any resource limits (CPU, memory). If the Docker engine has been asked to run the container it will also initialise a process inside it. A container can be stopped and restarted, in which case it will retain all settings and filesystem changes (but will lose anything in memory and all processes will be restarted). For this reason a stopped or exited container is not the same as an image.

其他回答

在Docker中,一切都从图像开始。映像是构成操作系统所需的所有文件。传统上,您需要为每个应用程序安装一个完整的操作系统。使用Docker,你可以把它配对,这样你就有一个小容器,里面有足够的操作系统来做你需要做的事情,你可以在一台计算机上高效地拥有很多很多这样的容器。

使用docker images查看已安装的映像,使用docker ps查看正在运行的映像。 当你输入docker run时,它会获取映像,并使其成为一个带有运行进程的活容器。我倾向于使用:

Docker运行-ti <image>:<tag> bash

最后,图像有它们自己的id集,容器也有它们自己的id集——它们不重叠。

映像基本上是用于创建容器的不可变模板。通过考虑将图像转换为容器所发生的变化,可以更容易地理解图像和容器之间的区别。

The Docker engine takes the image and adds a read-write filesystem on top, then initialises various settings. These settings include network options (IP, port, etc.), name, ID, and any resource limits (CPU, memory). If the Docker engine has been asked to run the container it will also initialise a process inside it. A container can be stopped and restarted, in which case it will retain all settings and filesystem changes (but will lose anything in memory and all processes will be restarted). For this reason a stopped or exited container is not the same as an image.

使用面向对象编程的类比,Docker镜像和Docker容器之间的区别就像类和对象之间的区别一样。对象是类的运行时实例。类似地,容器是映像的运行时实例。

对象在实例化时只创建一次。类似地,容器可以运行也可以停止。容器是根据映像创建的,尽管情况可能并不总是如此。下面的例子创建一个Apache服务器镜像,运行镜像,列出镜像,然后列出容器:

创建一个Dockerfile,内容如下: 从httpd: 2.4 安装Apache服务器 Sudo docker build -t my-apache2。 运行映像 Sudo docker运行-it——rm——命名my-running-app my-apache2 列出Docker映像 Sudo docker图像 列出正在运行的Docker容器 码头工人ps 列出所有容器 Docker ps a 列出最新创建的容器 Docker ps -l

映像是活动容器的冻结的不可变快照。容器是正在运行(或已停止)某个映像的实例。

从名为“ubuntu”的基本图像开始。让我们在ubuntu映像中交互式地运行bash并创建一个文件。我们将使用-i和-t标志来提供一个交互式bash shell。

$ docker run -i -t ubuntu  /bin/bash
root@48cff2e9be75:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@48cff2e9be75:/# cat > foo
This is a really important file!!!!
root@48cff2e9be75:/# exit

退出并重新启动映像时,不要期望该文件仍然存在。您将从与之前开始时完全相同的定义状态重新开始,而不是从您离开的地方重新开始。

$ docker run -i -t ubuntu  /bin/bash
root@abf181be4379:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@abf181be4379:/# exit

但是,容器现在不再运行,它有状态,可以保存(提交)到一个映像。

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED              STATUS                          PORTS                      NAMES
abf181be4379        ubuntu:14.04        /bin/bash              17 seconds ago       Exited (0) 12 seconds ago                                  elegant_ardinghelli    
48cff2e9be75        ubuntu:14.04        /bin/bash              About a minute ago   Exited (0) 50 seconds ago                                  determined_pare        
...

让我们从创建文件的容器ID为48cff2e9be75中创建一个图像:

$ docker commit 48cff2e9be75 ubuntu-foo
d0e4ae9a911d0243e95556e229c8e0873b623eeed4c7816268db090dfdd149c2

现在,我们有了一个非常重要的文件的新图像:

$ docker run ubuntu-foo /bin/cat foo
This is a really important file!!!!

尝试命令docker映像。你应该会看到你的新映像ubuntu-foo与我们开始时的ubuntu标准映像一起列出。

容器是基于图像的。需要将图像传递给Dockers run命令。

例子:

BusyBox映像

http://i.stack.imgur.com/eK9dC.png

这里我们指定一个名为busybox的映像。Docker在本地没有这个映像,而是从公共注册表中获取它。

注册表是Docker镜像的目录,Docker客户端可以与之通信并从中下载镜像。一旦图像被拉出,Docker启动一个容器并执行echo hello world命令。