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

Blog    

Recommend

SQL query for users who have placed orders for at least seven consecutive days

Create a table create table order(id varchar(10),...

How to export and import .sql files under Linux command

This article describes how to export and import ....

js to achieve drag and drop sorting details

Table of contents 1. Introduction 2. Implementati...

Detailed explanation of how to mount remote file systems via SSH on Linux

Features of SSHFS: Based on FUSE (the best usersp...

You really need to understand the use of CSS variables var()

When a web project gets bigger and bigger, its CS...

Detailed explanation of how to use grep to obtain MySQL error log information

To facilitate the maintenance of MySQL, a script ...

Graphical explanation of the solutions for front-end processing of small icons

Preface Before starting this article, let’s do a ...

Detailed explanation of writing multiple conditions of CSS: not

The :not pseudo-class selector can filter element...

Detailed explanation of for loop and double for loop in JavaScript

for loop The for loop loops through the elements ...

Detailed explanation of the transition attribute of simple CSS animation

1. Understanding of transition attributes 1. The ...

MySQL partition table is classified by month

Table of contents Create a table View the databas...

80 lines of code to write a Webpack plugin and publish it to npm

1. Introduction I have been studying the principl...

Directory permissions when creating a container with Docker

When I was writing a project yesterday, I needed ...