Details the Docker method for exiting a container without closing a container
- 2020-12-26 06:06:19
- OfStack
If you exit the docker container after entering it, the container becomes Exited. How do you exit the container so that the container does not close?
To exit normally without closing the container, press Ctrl+P+Q to exit the container. This is an important point to keep in mind!
The following example is to exit the container without closing the container
[root@localhost ~]# docker attach c600c4519fc8
[root@c600c4519fc8 /]# exit
exit
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c600c4519fc8 centos "/bin/bash" 3 hours ago Exited (0) 1 second ago pensive_jackson
5a7a0d694651 busybox "sh" 20 hours ago Exited (0) 20 hours ago hungry_vaughan
4b0296d18849 hello-world "/hello" 46 hours ago Exited (0) 46 hours ago hopeful_yonath
[root@localhost ~]# docker start pensive_jackson
pensive_jackson
[root@localhost ~]# docker attach c600c4519fc8
Ctrl + P + Q
[root@c600c4519fc8 /]# read escape sequence
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c600c4519fc8 centos "/bin/bash" 3 hours ago Up 22 seconds pensive_jackson
5a7a0d694651 busybox "sh" 20 hours ago Exited (0) 20 hours ago hungry_vaughan
4b0296d18849 hello-world "/hello" 46 hours ago Exited (0) 46 hours ago hopeful_yonath
In fact, we can configure this at the start of the container by adding the -ES16en parameter to start the container. Of course, this command is limited to starting a brand new container. Starting a closed container is not an option.
Tips 1
docker ES24en-ES25en: Runs the container in the background and returns the container ID
The following example is to start the container and exit using ES29en-ES30en
[root@localhost ~]# docker run -i -t -d centos /bin/bash
8521b11d5d99535d4cb0080adc5a58a4dd018ecd0751d9945f7da7ab01bec330
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8521b11d5d99 centos "/bin/bash" 4 seconds ago Up 4 seconds eager_goldwasser
c600c4519fc8 centos "/bin/bash" 3 hours ago Exited (0) 28 seconds ago pensive_jackson
5a7a0d694651 busybox "sh" 20 hours ago Exited (0) 20 hours ago hungry_vaughan
4b0296d18849 hello-world "/hello" 46 hours ago Exited (0) 46 hours ago hopeful_yonath
[root@localhost ~]# docker attach 8
[root@8521b11d5d99 /]# uname -r
3.10.0-514.el7.x86_64
[root@8521b11d5d99 /]# exit
exit
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8521b11d5d99 centos "/bin/bash" 2 minutes ago Exited (0) 2 seconds ago eager_goldwasser
c600c4519fc8 centos "/bin/bash" 3 hours ago Exited (0) 2 minutes ago pensive_jackson
5a7a0d694651 busybox "sh" 20 hours ago Exited (0) 20 hours ago hungry_vaughan
4b0296d18849 hello-world "/hello" 46 hours ago Exited (0) 46 hours ago hopeful_yonath
Here you may find that the container is still dead after exiting using -ES35en, and your do-it-yourself friends may find that just using docker run-ES38en to start the container is still dead
In fact, what we need to understand here is the running mechanism of the container. The Docker container runs in the background, and it must have a foreground process. Here, we let the container run with the foreground program, so that the -d container can survive after startup
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c600c4519fc8 centos "/bin/bash" 3 hours ago Exited (0) 4 minutes ago pensive_jackson
5a7a0d694651 busybox "sh" 21 hours ago Exited (0) 21 hours ago hungry_vaughan
4b0296d18849 hello-world "/hello" 47 hours ago Exited (0) 47 hours ago hopeful_yonath
[root@localhost ~]# docker run -d centos /bin/bash -c "nohup ping -i 1000 www.baidu.com"
8aa19c9604382bc019797ccda831ae1bcebd81d86380b6040d636e03000b440a
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8aa19c960438 centos "/bin/bash -c 'nohup ... " 2 seconds ago Up 2 seconds adoring_wing
c600c4519fc8 centos "/bin/bash" 3 hours ago Exited (0) 5 minutes ago pensive_jackson
5a7a0d694651 busybox "sh" 21 hours ago Exited (0) 21 hours ago hungry_vaughan
4b0296d18849 hello-world "/hello" 47 hours ago Exited (0) 47 hours ago hopeful_yonath
Here I use nohup to run a baidu process with ping1 per 1000 seconds in the background. In addition, you can also use "while true "; do echo hello world; sleep 1; done", unlimited output hello world.
Also, even if a process is running in the background and you enter the container and type exit to exit, it will still terminate the container, keep in mind.
Ctrl+P+Q is still my best use.