Docker 容器和 Docker 镜像的区别
在本文中,我们将通过展示构成容器和图像的组件以及使它们不同的功能来了解容器和图像之间的区别。
了解 Docker 中的层
要创建自定义镜像,我们通常使用 Dockerfile,它定义了使用基础镜像创建自定义镜像的指令。 可用于在 Dockerfile 中构建映像的指令如下。
FROM node:16.17.0-alpine
WORKDIR /app
ADD package*.json ./
RUN npm install
ADD . .
CMD node index.js
Docker 镜像是使用彼此堆叠的层创建的。 当 Dockerfile 中的指令操作能够添加或删除文件时,将向堆栈添加一个新层。
请注意
,并非 Dockerfile 中定义的所有指令都会创建一个新层。 例如,CMD 指令将有关要在容器内运行的命令的元数据添加到图像中。
另请注意,使用 Dockerfile 创建的层除最后一层外都是只读的。
当我们创建容器时,会创建一个新的薄层。 这一层和上一层的区别在于我们可以从这一层读写文件。
容器运行后,写入新文件、修改已有文件、删除文件等操作都在薄读写层进行。 下图显示了包含图像的只读层和容器的读写层的堆栈。
Docker 容器和镜像之间的区别
在上一节中,我们已经看到堆栈的顶层是容器使用的薄读写层,其他层是用于创建自定义图像的只读层。
这是 Docker 容器和镜像之间的主要区别。 在容器中添加或删除文件的任何更改都会对特定容器的薄读写层进行。
请注意,删除容器会删除其关联层。 但是,基本映像不会被删除。
创建的每个容器都有其读写层,允许它们共享基础映像但保持其数据状态。 下图展示了多个容器如何共享一个基础镜像。
总结
我们在本文中学习了如何通过使用层来区分 docker 容器和 docker 镜像。 我们已经了解到,镜像是使用名为 Dockerfile 的文件创建的,文件中的每条指令形成一个堆叠在另一层之上的只读层。
最后,我们了解到最上层是一个读写层,供容器用来添加或修改文件变化。
相关文章
Get the IP address of the Docker container from the host using docker inspect
发布时间:2025/03/26 浏览次数:102 分类:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
发布时间:2025/03/26 浏览次数:165 分类:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
Docker deploys nginx php application
发布时间:2025/03/26 浏览次数:131 分类:Docker
-
I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam
How to use Docker to image a Node.js web application
发布时间:2025/03/26 浏览次数:107 分类:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
Start a Bash terminal in a new Docker container
发布时间:2025/03/26 浏览次数:97 分类:Docker
-
Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers
Passing environment variables to containers in Docker
发布时间:2025/03/26 浏览次数:124 分类:Docker
-
This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t
Install Docker using Homebrew
发布时间:2025/03/26 浏览次数:202 分类:Docker
-
There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide
Enforce clean build of images in Docker
发布时间:2025/03/26 浏览次数:87 分类:Docker
-
This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho
Running a Docker instance from a Dockerfile
发布时间:2025/03/26 浏览次数:140 分类:Docker
-
Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application