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:
We can run the container as shown in the following command:
Get the list of running Docker containers as follows:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
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:
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.
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.
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.