Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Docker | Technical fundamentals of Docker (3)-Container Life cycle Management

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

What is a container?

Container: a container is a lightweight, portable, and packaged technology that allows an application to run in the same way almost anywhere, and when Docker runs the image file, the resulting object is the container. The container is equivalent to an instance run by the mirror and the container has a certain life cycle.

Differences between Docker containers and virtual machines

Similarities:

Containers, like virtual machines, share physical hardware resources.

The lifecycles of containers and virtual machines are similar (create, run, pause, shut down, and so on).

A variety of applications, such as redis, mysql, nginx, can be installed in a container or in a virtual machine. In other words, the operation in the container is the same as in a virtual machine (operating system).

Like a virtual machine, after the container is created, it is stored on the host machine: on linux under / var/lib/docker/containers

Differences:

The creation, startup and shutdown of virtual machines are based on a complete operating system. A virtual machine is a complete operating system. The container runs directly on the kernel of the host, which is essentially a combination of a series of processes.

Containers are lightweight and virtual machines are heavyweight. First of all, the container does not need additional resources to manage (no Hypervisor, Guest OS), and the virtual machine consumes more performance; secondly, creating, starting or shutting down the container is as easy as creating, starting or shutting down processes, while creating, starting, and shutting down an operating system is not so convenient.

As a result, it means that more containers can be run on a given hardware, or even Docker can be run directly on a virtual machine.

Life cycle management of containers

Let's take a look at a schematic diagram of the life cycle of the container:

The following salted fish will briefly learn some of the container management commands:

Container creation (docker create)

Command format:

Docker create [parameters] Image name [Container execution Command] [parameters to be provided when executing the command]

Common parameters:

-t assign a virtual terminal

-I provide an analog input. If you do not provide it, you cannot enter the default command.

-- name provides a container name for the created container. If not, one is randomly assigned.

Container boot (docker start)

Start one or more containers.

Command format: docker start [parameter] container [container..]

Common parameters:

-a connect the current input / output to the container

-I connect the current input to the container

Container creation and launch (docker run)

Command format:

Docker run [parameters] Mirror [Container execution Command] [parameters provided by execution Command]

Common parameters:

-t assign a virtual terminal

-I keep input open

-d the container runs in the background and prints the container id

-- automatically delete a container when the rm container is finished

Note: since we study docker mainly for our later docker-based Python development services, the specific differences between some parameters of docker run and docker create/start will not be discussed in the article. Friends who have questions can refer to the official documents. Of course, you are welcome to discuss with salted fish in the message area. Just remember two formulas here:

Docker run = doker create + docker start-a

Docker run-d = docker create + docker start

Here, salted fish is recommended to use docker run-dti to start the required container.

Container pause (docker pause/unpause)

Docker pause pauses one or more containers

Docker unpause starts one or more containers in pause

Command format:

Docker pause/unpause container [container..]

Container closed (docker stop)

Close one or more containers.

Docker stop closes one or more containers

Command format:

Docker stop container [container..]

Common parameters:

-t wait time before closing. Default is 10 seconds.

Container termination (docker kill)

Force and immediately close one or more containers that are in a paused or running state.

Command format: docker kill [parameter] container [container..]

Common parameters:

-s specifies the close signal sent to the container. The default is "kill".

Container restart (docker restart)

Restart one or more containers in the running, paused, closed, or created state.

Command format:

Docker restart [parameter] container [container..]

Common parameters:

-t the waiting time before closing. The default is 10 seconds, but it is actually the waiting time before closing.

Container deletion (docker container rm / docker rm)

Command format: docker container rm [parameter] container [container..]

Common parameters:

-f forcibly delete

-v delete the container while deleting the container's data volume

The difference between docker kill and docker stop

Both stop and kill close containers in the docker container lifecycle management above, but how does kill forcibly kill running containers?

Here we need to explain the signals about terminating the process under linux: SIGTERM and SIGKILL.

SIGKILL signal: unconditional termination of the process signal. When the process receives the signal, it terminates immediately without cleanup and temporary storage. This signal cannot be ignored, processed, or blocked, and it provides the system administrator with a way to kill any process.

SIGTERM signal: program termination signal, which can be generated by the kill command. Unlike SIGKILL, SIGTERM signals can be blocked and terminated so that the program can save work or clean up temporary files before exiting.

Docker stop first sends a SIGTERM signal to the process, telling the process that it is about to be shut down. Immediately after the wait time specified by-t has passed, a SIGKILL signal will be sent to close the container directly.

Docker kill sends a SIGKILL signal directly to close the container. However, the signal can also be modified with the-s parameter.

The-t wait time can also be set in docker restart. When the wait time is over, the SIGKILL signal will be sent immediately and the container will be closed directly.

Therefore, you will find that during the waiting process of the docker stop, if the execution of the docker stop is terminated, the container is not closed in the end. And docker kill happens almost immediately and cannot be undone.

Summary

With regard to the life cycle management of docker containers, the parts that salted fish think need to be further understood after stepping on the pit are about docker run/start/create and docker kill/stop/restart.

With regard to the difference between docker kill and docker stop, I hope you can focus on the difference between the above docker kill/stop. As for docker run/start/create, you'd better be able to knock on the difference between the back containers created by different parameters.

The above is the summary of the salted fish's stampede pit on the container life cycle management.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report