Docker Exit Container

Docker containers have become the de facto method for app development in the modern age. The need to switch to a different environment, configure all the tools in the new system, and deal with dependency issues are nearly a forgotten problem in the modern age.

Using Docker containers, you can define all the requirements for your deployment and have it running in seconds. Need an SQL Server instance without a Windows platform? Docker has you covered the need to compile the Linux kernel.

However, despite Docker’s capabilities and features, it is not an automated system and requires occasional interaction. In this tutorial, we will learn how to exit a Docker container after performing some needed operations in the system.

Running a Test Container

The first step is to run a basic container that we will use for demonstration purposes. You can skip this step if you already have a container that you wish to work on.

Run the following command to run the Busybox container using the official image:

$ Docker pull busybox

We can run the container as shown in the following command:

# docker run -itd --rm busybox

Get the list of running Docker containers as follows:

root@gcp-docker:~# docker ps
e040c8dda480   busybox   "sh"      6 seconds ago   Up 5 seconds             zealous_shtern

Once we have the container running, we can use the “-it” parameter to log into the container shell as follows:

$ Docker run -it busybox sh

This should connect to the container shell and allow you to execute commands inside the container.

Method 1: Using the “Exit” Command

Once you completed the tasks that you wish to perform in the container, you can exit the container by pressing the “CTRL + D” shortcut.

You can also type the “exit” command in the container shell to exit the interactive session and return to your host shell.

# exit

Keep in mind that this stops all the processes that run in the container.

Method 2: Using the “Detach” Command

If you do not wish to stop the running processes in the container, you can use the “Ctrl+P” shortcuts followed by “Ctrl+Q” to detach from the container and return to the host session.

Using the “detach” method allows the container to continue running in the background without terminating any process that may be going on.

You can also use the “detach” command instead of the key shortcuts.

# detach


In this tutorial, we learned how to use the “exit” and “detach” commands in Docker to exit from a container with and without terminating the processes.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list