Container is another core concept of Docker. Simply put, a container is one or a group of applications that run independently, and their operating environment. Correspondingly, a virtual machine can be understood as a complete set of simulated operating systems (providing a running environment and other system environments) and the applications running on it. The main operations on containers are:
Start the container There are two ways. One is to create a new container based on the image and start it. The other is to restart the container in the stopped state. Create and start The command required is mainly docker run This is almost indistinguishable from executing /bin/echo 'hello world' locally. The following command starts a bash terminal, allowing user interaction, such as: docker run -t -i ubuntu:18.04 /bin/bash The -t option lets Docker allocate a pseudo-tty and bind it to the container's standard input, while -i keeps the container's standard input open. So what happens when we use the docker run command to create and start a container?
So when we enter the terminal through bash, we are actually entering another system. Start a terminated container You can use the docker container start command to directly start a terminated container. The core of the container is the executed application, and the required resources are necessary for the application to run. Apart from this, there are no other resources. You can use ps or top in the pseudo terminal to view process information. It can be seen that only the specified bash application is running in the container. This feature makes Docker's resource utilization extremely high, and it is truly lightweight virtualization. Background operation More often, we will run the container in the background, and we can add the -d parameter to achieve this. The following prints hello world every 1 second. docker run ubuntu:18.04 /bin/sh -c "while true; do echo hello world;sleep 1;done" If you use the -d parameter to run the container, it will run in the background: Note: Whether the container will run for a long time is related to the command specified by docker run and has nothing to do with the -d parameter. After starting with the -d parameter, a unique id will be returned. You can also view the container information through the docker container ls command. To get the output information of the container, you can use the docker container logs command. docker container logs [container ID or NAMES] Terminating the container You can use docker container stop or docker stop container-id to terminate a running container. In addition, when the application specified in the Docker container terminates, the container is also automatically terminated. Containers in terminated state can be seen using the docker container ls -a command. For example: A terminated container can be restarted using the docker container start command. In addition, the docker container restart command terminates a running container and then restarts it. Entering the container When the -d parameter is used, the container will enter the background after startup. Sometimes you need to enter the container to perform operations, including using the docker attach command or the docker exec command. It is recommended that you use the docker exec command for the reasons explained below. attach command Note: If you exit from this stdin, the container will stop. exec command docker exec can be followed by multiple parameters, here we mainly explain the -i -t parameters. When only the -i parameter is used, since no pseudo terminal is allocated, the interface does not have the familiar Linux command prompt, but the command execution results can still be returned. When the -i -t parameters are used together, we can see the familiar Linux command prompt. If you exit from this stdin, the container will not be stopped. This is why it is recommended that you use docker exec. Exporting and importing containers Export Container If you want to export a local container, you can use the docker export command. This will export the container snapshot to a local file. Importing a container You can use docker import to import a container snapshot file as an image, for example: cat ubuntu.tar | docker import - test/ubuntu:v1.0 Deleting a container You can use docker container rm or docker rm container-id to delete a container in a terminated state. For example: Note: Deleting a container requires stopping the container Clean up all terminated containers Use the docker container ls -a command to view all containers that have been created, including those in the terminated state. If there are too many containers, it may be troublesome to delete them one by one. Use the following command to clean up all containers in the terminated state. $ docker container prune This operation is quite dangerous, use with caution. The above are common operations about Docker containers. The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Tutorial on resetting the root password of Mac MySQL
>>: WeChat applet implements user login module server construction
Table of contents defineComponent overload functi...
1. Block-level element: refers to the ability to e...
Table of contents 1. Understanding the Equality R...
Table of contents Why update the auto-increment i...
Table of contents Jenkins installation Install Ch...
Yes, CSS has regular expressions too (Amen) Two p...
The filter attribute defines the visual effect of...
This article shares the specific code for the WeC...
What is HTML? To put it simply: HTML is used to m...
Table of contents Using routing plugins in a modu...
This article shares the specific code of native j...
How to use the MySQL authorization command grant:...
Table of contents 1. Component 2. keep-alive 2.1 ...
This article shares the specific code of js to ac...
This article mainly introduces the binary type op...