📜  Docker 镜像和容器的区别

📅  最后修改于: 2021-09-12 11:07:24             🧑  作者: Mango

1. Docker 镜像:
图像和容器的概念就像类和对象,其中对象是类的实例,类是对象的蓝图。虚拟机和 Docker 中的镜像不同,在虚拟机中,镜像只是虚拟机在不同时间点运行的快照,但 Docker 镜像与它们略有不同,最重要和主要的区别在于 docker 镜像是不可变的,即它们可以不被改变。

在现实世界中,一个软件在一台计算机上运行但由于环境不同而无法在其他计算机上运行的情况很多,这个问题通过 docker 镜像完全解决,使用它,应用程序将在每个人的 PC 上运行相同。团队中的每个开发人员都将拥有完全相同的开发实例。每个测试实例与开发实例完全相同。您的生产实例与测试实例完全相同。此外,世界各地的开发人员可以在名为 Docker HUB 的平台上共享他们的 Docker 镜像。

2. Docker 容器:
它们实际上是 Docker 虚拟机,但通常称为 Docker 容器。如果 Docker 镜像是房屋的地图,那么 Docker Container 就是实际的构建房屋,或者换句话说,我们可以将其称为镜像的实例。根据官方网站,容器是镜像的可运行实例。您可以使用 Docker API 或 CLI 创建、启动、停止、移动或删除容器。您可以将容器连接到一个或多个网络,为其附加存储,甚至可以根据其当前状态创建新映像。

应用程序使用一组容器运行,这些容器彼此独立,也与运行它们的主机隔离,例如,如果后端应用程序在端口 8000 的 docker 容器上运行,并且您尝试从主机访问它,则不会能够访问,因为容器是自隔离的,在这种情况下,您必须在某个端口显式公开您的应用程序并将您的机器端口连接到该端口。

例子 –

docker run --publish 8000:8080 --detach --name alias_name application_name:1.0 

这里运行在容器中的 8080 端口的应用程序连接到主机的 8000 端口。现在它可以使用 URL localhost:8000 访问应用程序

Docker Image 和 Docker Container 的区别:

S.NO Docker Image Docker Container
1 It is Blueprint of the Container. It is instance of the Image.
2 Image is a logical entity. Container is a real world entity.
3 Image is created only once. Containers are created any number of times using image.
4 Images are immutable. Containers changes only if old image is deleted and new is used to build the container.
5 Images does not require computing resource to work. Containers requires computing resources to run as they run as Docker Virtual Machine.
6 To make a docker image, you have to write script in Dockerfile. To make container from image, you have to run “docker build .” command
7 Docker Images are used to package up applications and pre-configured server environments. Containers use server information and file system provided by image in order to operate.
8 Images can be shared on Docker Hub. It makes no sense in sharing a running entity, always docker images are shared.
9 There is no such running state of Docker Image. Containers uses RAM when created and in running state.