JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

The difference between Docker containers and Docker images

Author:JIYIK Last Updated:2025/03/25 Views:

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.

image and container layers


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.

Containers share base images


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.

Article URL:

Related Articles

Docker deploys nginx php application

Publish Date:2025/03/26 Views:131 Category: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

Publish Date:2025/03/26 Views:107 Category: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

Publish Date:2025/03/26 Views:97 Category: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

Publish Date:2025/03/26 Views:125 Category: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

Publish Date:2025/03/26 Views:202 Category: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

Publish Date:2025/03/26 Views:88 Category: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

Publish Date:2025/03/26 Views:140 Category: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

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial