How to Exit a Docker Container

February 2, 2023

Introduction

Docker management tasks frequently demand running commands inside containers. Once the user performs the required operation, they must exit the container to resume work in their system's shell session.

This article shows you how to exit a Docker container.

How to exit a Docker container.

How to Exit Docker Container from an Interactive Shell Session

To access a container shell prompt, use Docker commands such as docker run, docker exec, and docker attach. For example, the following docker run command runs a container based on the Alpine Linux official image and starts an interactive session inside the container using an sh shell prompt:

docker run -it alpine sh

Docker starts the container, and the prompt appears.

An sh prompt inside an Alpine Linux Docker container.

There are two ways to exit a Docker container interactive shell session. One method exits and stops the container, while the other keeps the container and its processes running in the background.

The sections below provide details for both methods.

Method 1: Exit and Stop Docker Container

Perform the following actions to close the interactive terminal shell and stop the container.

1. If a process is running in the container, press Ctrl+C to send the SIGINT signal and stop the process. The screenshot below shows Ctrl+C interrupting the ping command.

2. Next, press Ctrl+D to exit and stop the container.

Using keyboard shortcuts to stop processes in a container and exit the container.

Alternatively, type the exit command to achieve the same effect as when pressing Ctrl+D:

exit
Exiting the container with the exit command.

Method 2: Exit Docker Container without Stopping It

If you want to exit the container's interactive shell session, but do not want to interrupt the processes running in it, press Ctrl+P followed by Ctrl+Q. This operation detaches the container and allows you to return to your system's shell.

The example below runs the ping command in the container, then interrupts the session with Ctrl+P and Ctrl+Q.

Exiting a Docker container without stopping it, using keyboard shortcuts.

The shell session ends, but the container continues to run in the background. Confirm this by listing the running containers with the command below:

docker ps

The container appears on the list.

Listing the running containers.

To return to the container's interactive shell, use docker attach followed by the name or ID of the container.

docker attach [container-name-or-id]

Once the container attaches, the ping command output resumes printing in the terminal.

Attaching a detached container.

Note: You can also run the container in the detached mode from the start. To do this, add the -d option to the docker run command:

docker run -it -d alpine sh

Conclusion

This article presented two methods to exit a Docker container. The first method included stopping the exited container, while the second preserved the running container processes.

Learn more about the basic container operations by reading How to List/Start/Stop Docker Containers.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How To Install Docker on Ubuntu 20.04
April 6, 2023

Docker is a platform enabling users to package and run applications in containers. Containers are isolated, similar to...
Read more
How To Remove Docker Images, Containers, Networks & Volumes
February 7, 2019

In this guide, you will learn how to organize a Docker environment by removing Docker images...
Read more
How to Manage Docker Containers?
January 29, 2019

Containers remove many tedious processes out of software development. Maximize Docker’s potential by implementing best practices to improve security, speed...
Read more
Docker Container Logs
May 9, 2022

Docker offers built-in logging solutions and additional features that ensure effective log management. Read on to learn about Docker container logs, the commands...
Read more