What is the difference between containers and images in docker?
This article introduces the difference between containers and images in docker. I hope it can help you.
(Recommended tutorial: docker tutorial)
When I still had a little understanding of Docker technology, I found that I understood Docker's commands are very difficult. So, I spent a few weeks learning how Docker works, or more specifically, the union file system, and then looking back at the Docker commands, everything fell into place. , extremely simple.
Digression: Personally, the best way to master a technology and use it rationally is to have a deep understanding of the working principles behind the technology. Usually, the birth of a new technology is often accompanied by media hype and hype, which makes it difficult for users to see the true nature of the technology. Rather, new technologies always invent new terms or metaphors to help inform the
This is very helpful in the early stage, but it puts a layer of sandpaper on the principle of technology, which is not conducive to users mastering the true meaning of technology in the later stage.
Git is a good example. I couldn't use Git well before, so I spent some time learning the principles of Git. It was only then that I really understood the usage of Git. I firmly believe that only those who truly understand the internals of Git can master this tool.
Image Definition
Image is the unified perspective of a bunch of read-only layers. Maybe this definition is a bit difficult to understand. The following picture can help readers understand. Definition of mirror.
From the left we see multiple read-only layers, which overlap. Except for the bottom layer, all other layers will have a pointer pointing to the next layer. These layers are implementation details inside Docker and can be accessed on the file system of the host (Translator's Note: the machine running Docker). Union file system technology can integrate different layers into one file system, providing a unified perspective for these layers, thus hiding the existence of multiple layers. From the user's perspective, only one file exists system. We can see what this perspective looks like on the right side of the image.
You can find files about these layers on your host file system. Note that these layers are not visible inside a running container. On my host, I found that they exist in the /var/lib/docker/aufs directory.
sudo tree -L 1 /var/lib/docker/
/var/lib/docker/ ├── aufs ├── containers ├── graph ├── init ├── linkgraph.db ├── repositories-aufs ├── tmp ├── trust └── volumes 7 directories, 2 files
Container Definition
The definition of container (container) is almost exactly the same as that of image (image). It is also a unified perspective of a bunch of layers. The only difference is that the top layer of the container is readable and writable.
Careful readers may find that the definition of a container does not mention whether the container is running. Yes, this is intentional. It was this discovery that helped me understand a lot of my confusion.
Key points: container = image readable layer. And the definition of a container does not mention whether to run the container.
Next, we will discuss running containers.
Running Container Definition
A running container (running container) is defined as a readable and writable unified file system plus an isolated process space and the processes contained in it. The image below shows a running container.
It is the file system isolation technology that makes Docker a promising technology. A process in a container may modify, delete, or create files, and these changes will affect the read-write layer. The picture below demonstrates this behavior.
我们可以通过运行以下命令来验证我们上面所说的:
docker run ubuntu touch happiness.txt
即便是这个ubuntu容器不再运行,我们依旧能够在主机的文件系统上找到这个新文件。
find / -name happiness.txt
/var/lib/docker/aufs/diff/860a7b...889/happiness.txt
Image Layer Definition
为了将零星的数据整合起来,我们提出了镜像层(image layer)这个概念。下面的这张图描述了一个镜像层,通过图片我们能够发现一个层并不仅仅包含文件系统的改变,它还能包含了其他重要信息。
元数据(metadata)就是关于这个层的额外信息,它不仅能够让Docker获取运行和构建时的信息,还包括父层的层次信息。需要注意,只读层和读写层都包含元数据。
除此之外,每一层都包括了一个指向父层的指针。如果一个层没有这个指针,说明它处于最底层。
Metadata Location:
我发现在我自己的主机上,镜像层(image layer)的元数据被保存在名为”json”的文件中,比如说:
/var/lib/docker/graph/e809f156dc985.../json
e809f156dc985...就是这层的id
一个容器的元数据好像是被分成了很多文件,但或多或少能够在/var/lib/docker/containers/
全局理解(Tying It All Together)
现在,让我们结合上面提到的实现细节来理解Docker的命令。
docker create
docker create 命令为指定的镜像(image)添加了一个可读写层,构成了一个新的容器。注意,这个容器并没有运行。
docker start
Docker start命令为容器文件系统创建了一个进程隔离空间。注意,每一个容器只能够有一个进程隔离空间。
docker run
看到这个命令,读者通常会有一个疑问:docker start 和 docker run命令有什么区别。
As you can see from the picture, the docker run command first creates a container using the image, and then runs the container. This command is very convenient and hides the details of the two commands, but on the other hand, it is easy for users to misunderstand.
Digression: Continuing our previous topic about Git, I think the docker run command is similar to the git pull command. The git pull command is a combination of git fetch and git merge. Similarly, docker run is a combination of docker create and docker start.
docker ps
docker ps command will list all running containers. This hides the existence of non-running containers. If we want to find these containers, we need to use the following command.
docker ps –a
docker ps –a command will list all containers, whether they are running or stopped.
docker images
The docker images command will list all top-level images. In fact, here we have no way to distinguish between a mirror and a read-only layer, so we propose top-level mirroring. Only the image used when creating the container or the image directly pulled down can be called a top-level image, and there are multiple image layers hidden under each top-level image.
docker images –a
docker images –a command lists all images, which can also be said to list all readable images. layer. If you want to view all layers under a certain image-id, you can use docker history to view it.
docker stop
The docker stop command will send a SIGTERM signal to the running container and then stop all process.
docker kill
The docker kill command sends an unfriendly SIGKILL to all processes running in the container Signal.
docker pause
The docker stop and docker kill commands will send UNIX signals to the running process, docker The pause command is different. It uses the characteristics of cgroups to pause the running process space. You can find the specific internal principles here: www.kernel.org/doc/Doc..., but the disadvantage of this method is that sending a SIGTSTP signal is not simple enough for the process to understand, so that it cannot Halt all processes.
docker rm
The docker rm command will remove the read-write layer that makes up the container. Note that this command can only be executed on non-running containers.
docker rmi
The docker rmi command removes a read-only layer that makes up the image. You can only use docker rmi to remove the top level layer (which can also be said to be a mirror). You can also use the -f parameter to force the removal of the middle read-only layer.
docker commit
The docker commit command converts the read-write layer of the container into a read-only layer, so Convert a container into an immutable image.
docker build
The docker build command is very interesting, it will execute multiple commands repeatedly.
We can see from the picture above that the build command obtains the image according to the FROM instruction in the Dockerfile file, and then repeatedly 1) run (create and start) , 2) Modify, 3) Commit. Each step in the loop generates a new layer, so many new layers are created.
docker exec
The docker exec command will execute a new process in the running container.
docker inspect or
The docker inspect command will extract the top level of the container or image metadata.
docker save
The docker save command will create a compressed image file, which can be stored on another host Used on Docker. Unlike the export command, this command saves metadata for each layer. This command only takes effect on images.
docker export
The docker export command creates a tar file and removes metadata and unnecessary Layer, integrates multiple layers into one layer, and only saves the content seen from the current unified perspective (Translator's Note: The container after expoxt is then imported into Docker, and only one image can be seen through the docker images –tree command; The image after saving is different, it can see the historical image of this image).
docker history
The docker history command recursively outputs the historical image of the specified image.
The above is the detailed content of What is the difference between containers and images in docker?. For more information, please follow other related articles on the PHP Chinese website!

How to build portable applications with Docker and Linux? First, use Dockerfile to containerize the application, and then manage and deploy the container in a Linux environment. 1) Write a Dockerfile and package the application and its dependencies into a mirror. 2) Build and run containers on Linux using dockerbuild and dockerrun commands. 3) Manage multi-container applications through DockerCompose and define service dependencies. 4) Optimize the image size and resource configuration, enhance security, and improve application performance and portability.

Docker and Kubernetes improve application deployment and management efficiency through container orchestration. 1.Docker builds images through Dockerfile and runs containers to ensure application consistency. 2. Kubernetes manages containers through Pod, Deployment and Service to achieve automated deployment and expansion.

Docker and Kubernetes are leaders in containerization and orchestration. Docker focuses on container lifecycle management and is suitable for small projects; Kubernetes is good at container orchestration and is suitable for large-scale production environments. The combination of the two can improve development and deployment efficiency.

Docker and Linux are perfect matches because they can simplify the development and deployment of applications. 1) Docker uses Linux's namespaces and cgroups to implement container isolation and resource management. 2) Docker containers are more efficient than virtual machines, have faster startup speeds, and the mirrored hierarchical structure is easy to build and distribute. 3) On Linux, the installation and use of Docker is very simple, with only a few commands. 4) Through DockerCompose, you can easily manage and deploy multi-container applications.

The difference between Docker and Kubernetes is that Docker is a containerized platform suitable for small projects and development environments; Kubernetes is a container orchestration system suitable for large projects and production environments. 1.Docker simplifies application deployment and is suitable for small projects with limited resources. 2. Kubernetes provides automation and scalability capabilities, suitable for large projects that require efficient management.

Use Docker and Kubernetes to build scalable applications. 1) Create container images using Dockerfile, 2) Deployment and Service of Kubernetes through kubectl command, 3) Use HorizontalPodAutoscaler to achieve automatic scaling, thereby building an efficient and scalable application architecture.

The main difference between Docker and Kubernetes is that Docker is used for containerization, while Kubernetes is used for container orchestration. 1.Docker provides a consistent environment to develop, test and deploy applications, and implement isolation and resource limitation through containers. 2. Kubernetes manages containerized applications, provides automated deployment, expansion and management functions, and supports load balancing and automatic scaling. The combination of the two can improve application deployment and management efficiency.

Installing and configuring Docker on Linux requires ensuring that the system is 64-bit and kernel version 3.10 and above, use the command "sudoapt-getupdate" and install it with the command "sudoapt-getupdate" and verify it with "sudoapt-getupdate" and. Docker uses the namespace and control groups of the Linux kernel to achieve container isolation and resource limitation. The image is a read-only template, and the container can be modified. Examples of usage include running an Nginx server and creating images with custom Dockerfiles. common


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)
