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

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

什么是容器?


当前回答

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

其他回答

Image相当于OOP中的类定义,层是该类的不同方法和属性。

容器是图像的实际实例化,就像对象是实例化或类的实例一样。

正如许多回答指出的那样:你构建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

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

*在docker中,镜像是一个不可变的文件,它包含docker应用程序运行所需的源代码和信息。它可以独立于容器而存在。

Docker容器是在运行时创建的虚拟化环境,需要镜像才能运行。docker网站上有一张图片显示了这种关系:

就像对象是面向对象编程语言中类的实例一样,Docker容器也是Docker映像的实例。

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为映像创建和容器运行提供的标准。