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

谁能给我点灯?


当前回答

The official difference is that the container is the last layer which is writable whereas the layers below are only readable and they belong to your image. The intuitive difference is that the docker instance is the instance virtualized by your docker daemon and the running your image, it operates within an isolated section of your kernel (this process is hidden from you). The image however is static, it doesn't run, it is just a pile of layers (static files). If we would relate this paradigm to object-oriented programming, the image is your class definition, whereas your docker instance is your class spawned object that resides in memory.

我写了一个教程来加强你的docker知识直觉:

http://javagoogleappspot.blogspot.com/2018/07/docker-basics.html

其他回答

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

例子:

BusyBox映像

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

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

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

码头工人图片: 它包含一个关于如何构建和运行容器的命令和指令列表。因此,基本上Images包含启动容器(也称为blueprint)所需的所有数据和元数据。我们不能在没有指定图像的情况下将一个容器送入午餐。

$docker images centos

列出所有可用的centos版本。

码头工人容器: 容器是图像的午餐,因此我们可以说容器是图像的运行实例。 Container是一个运行时构造,不像Images是构建时构造。

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

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

The official difference is that the container is the last layer which is writable whereas the layers below are only readable and they belong to your image. The intuitive difference is that the docker instance is the instance virtualized by your docker daemon and the running your image, it operates within an isolated section of your kernel (this process is hidden from you). The image however is static, it doesn't run, it is just a pile of layers (static files). If we would relate this paradigm to object-oriented programming, the image is your class definition, whereas your docker instance is your class spawned object that resides in memory.

我写了一个教程来加强你的docker知识直觉:

http://javagoogleappspot.blogspot.com/2018/07/docker-basics.html