The difference between Docker containers and Docker images
In this article, we will understand the difference between containers and images by showing the components that make up containers and images and the features that make them different.
Understanding Docker layers
To create a custom image, we usually use a Dockerfile, which defines the instructions for creating a custom image using a base image. The instructions that can be used to build an image in a Dockerfile are as follows.
FROM node:16.17.0-alpine
WORKDIR /app
ADD package*.json ./
RUN npm install
ADD . .
CMD node index.js
Docker images are created using layers stacked on top of each other. When the instructions in the Dockerfile act to add or remove files, a new layer is added to the stack.
请注意
, not all instructions defined in a Dockerfile create a new layer. For example, the CMD instruction adds metadata about the command to be run inside the container to the image.
Also note that all layers created using a Dockerfile are read-only except for the last layer.
When we create a container, a new thin layer is created. The difference between this layer and the previous one is that we can read and write files from this layer.
After the container is running, operations such as writing new files, modifying existing files, and deleting files are all performed in the thin read-write layer. The following figure shows the stack containing the read-only layer of the image and the read-write layer of the container.
The difference between Docker containers and images
In the previous section, we have seen that the top layer of the stack is a thin read-write layer used by containers, and the other layers are read-only layers used to create custom images.
This is the main difference between Docker containers and images. Any changes that add or remove files in a container are made to a thin read-write layer for that specific container.
Note that deleting a container deletes its associated layers. However, the base image is not deleted.
Each container created has its own read-write layer, allowing them to share the base image but maintain their data state. The following diagram shows how multiple containers can share a base image.
Summarize
In this article, we have learned how to differentiate between docker containers and docker images by using layers. We have learned that images are created using a file called Dockerfile and each instruction in the file forms a read-only layer stacked on top of another layer.
Finally, we learned that the top layer is a read-write layer that is used by containers to add or modify file changes.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Copy files from host to Docker container
Publish Date:2025/03/25 Views:126 Category:Docker
-
This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest
Get the IP address of the Docker container
Publish Date:2025/03/25 Views:102 Category:Docker
-
This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con
Uninstalling Docker on macOS
Publish Date:2025/03/25 Views:95 Category:Docker
-
Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p
Enter the Docker container's shell
Publish Date:2025/03/25 Views:98 Category:Docker
-
This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t
Listing containers in Docker
Publish Date:2025/03/25 Views:122 Category:Docker
-
This article will discuss various commands for listing containers created in our system. This means we should create some containers beforehand for these commands to return values. List all running containers in Docker We will start by list
Mount the host directory into the Docker container
Publish Date:2025/03/25 Views:189 Category:Docker
-
Docker provides, among other features, tools to work with the host operating system and the container file system. One of these features is the ability to persist data in containers and share data between containers by mounting directories
Docker build command with multiple parameters
Publish Date:2025/03/25 Views:138 Category:Docker
-
docker build The command allows us to create a Docker image from a Dockerfile. It also allows us to build an image from a context that references a set of files located in a location specified by a PATH or URL. A Dockerfile, on the other ha
The difference between CMD and ENTRYPOINT in Docker
Publish Date:2025/03/25 Views:141 Category:Docker
-
Docker containers have become the standard when it comes to managing software and dependencies in different environments. When working with real-world applications, you need to create a docker file before building your application container
Use the Dockerfile to create a directory in the container using the Mkdir command
Publish Date:2025/03/25 Views:73 Category:Docker
-
Docker containers have become the de facto way to manage software and dependencies in different environments. When working with real-world applications, you will undoubtedly need to create a Dockerfile before you can build your application