Detailed explanation of how to exit Docker container without closing it

Detailed explanation of how to exit Docker container without closing it

After entering the Docker container, if you exit the container, the container will change to the Exited state. So how do you exit the container without shutting down the container?

If you want to exit normally without closing the container, press Ctrl+P+Q to exit the container. This is very important, please remember!

The following example exits the container without closing it:

[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 it when starting the container and add the -d parameter to start the container. Of course, this command is limited to starting a new container, and it is not possible to start a closed container.

Tips 1

docker run -d: runs the container in the background and returns the container ID

The following example uses docker -d to start a container and exit

[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 with the -d command. Hands-on friends may find that just starting the container with docker run -d is also dead.

What we need to understand here is the operating mechanism of the container. The Docker container runs in the background and must have a foreground process. Here we let the container have a foreground program running, so that the container can survive after -d is started.

[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

I use nohup to run a process in the background that pings Baidu every 1000 seconds. You can also use "while true; do echo hello world; sleep 1; done" to output hello world infinitely.

In addition, even if there is a process running in the background, if you enter the container and enter exit, the container will still be terminated. Please keep this in mind.

Ctrl+P+Q is still the best method in my opinion.

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:
  • Introduction to Jenkins and how to deploy Jenkins with Docker
  • Detailed explanation of how to solve the problem that the docker container cannot access the host machine through IP
  • How to create an Nginx server with Docker
  • How to configure https for nginx in docker
  • Docker container introduction
  • Detailed explanation of Docker+Jenkins+Gitlab+Django application deployment practice
  • Docker Getting Started Installation Tutorial (Beginner Edition)
  • Install docker offline by downloading rpm and related dependencies using yum
  • Docker uses busybox to create a base image
  • Docker Detailed Illustrations

<<:  MySQL gets the current date and time function

>>:  Two methods to implement Mysql remote connection configuration

Recommend

Detailed explanation of the solution to forget the password in MySQL 5.7

ENV: [root@centos7 ~]# uname -r 3.10.0-514.el7.x8...

Detailed explanation of pid and socket in MySQL

Table of contents 1. Introduction to pid-file 2.S...

How to detect file system integrity based on AIDE in Linux

1. AIDE AIDE (Advanced Intrusion Detection Enviro...

Understanding and using React useEffect

Table of contents Avoid repetitive rendering loop...

Front-end JavaScript thoroughly understands function currying

Table of contents 1. What is currying 2. Uses of ...

react-beautiful-dnd implements component drag and drop function

Table of contents 1. Installation 2.APi 3. react-...

Introduction to the usage of common XHTML tags

There are many tags in XHTML, but only a few are ...

How to automatically execute SQL statements when MySQL in Docker starts

When creating a MySQL container with Docker, some...

How to implement two-way binding function in vue.js with pure JS

Table of contents First, let's talk about the...

How to ensure that every page of WeChat Mini Program is logged in

Table of contents status quo Solution Further sol...

VUE render function usage and detailed explanation

Table of contents Preface The role of render Rend...

RHEL7.5 mysql 8.0.11 installation tutorial

This article records the installation tutorial of...

Detailed implementation plan of Vue front-end exporting Excel files

Table of contents 1. Technology Selection 2. Tech...