Docker Containers
Estimated reading: 5 minutes 10 views

🚪 How to Exit from a Docker Container – With or Without Killing It

When working inside a Docker container, it’s essential to know how to exit properly — especially if you don’t want to accidentally stop or kill the container.

In this guide, you’ll learn:

  • 🧱 How to create containers in attached and detached mode
  • ✅ How to exit without killing the container
  • ❌ How to exit by killing the container
  • 🙋‍♂️ Plus, answers to frequently asked questions

🏗️ Creating a Docker Container – Attached vs. Detached Mode

Before diving into exiting, let’s understand how container modes work — because how you start the container determines what happens when you exit.


🔗 Attached Mode (-it)

In attached mode, the container runs in the foreground, and you’re connected to its terminal. The container will usually stop when the session ends, unless you detach it properly.

🧪 Command:

docker run -it ubuntu /bin/bash

Breakdown of the Command:

  • docker run → Creates and starts a new container
  • -i → Interactive mode: Keeps STDIN open to send input to the container.
  • -t → TTY allocation: Assigns a pseudo-terminal for interactive shell use.
  • ubuntu → The Docker image to use (defaults to ubuntu:latest)
  • /bin/bash → The command to run inside the container (Bash shell)

In this case, if you type exit or Ctrl + D, you will not terminate the main shell process, and the container will not stop. The container will continue running until you manually stop it.

⚠️ Important:

If you simply type exit or press Ctrl + D, you will terminate the main shell process, and the container will stop.


✅ Example: Container Started with -it (Attached Mode)

docker run -it ubuntu /bin/bash
# You're now inside the container...
exit # or Ctrl + D

🔴 The container stops after you exit because it’s running in the foreground and the main process has ended.


🧠 Tip: If you want the container to keep running, use Ctrl + P followed by Ctrl + Q to detach instead of exiting.


🧳 Detached Mode (-dit)

In detached mode, the container runs in the background, and you’re not connected to its terminal.

🧪 Command:

docker run -dit ubuntu /bin/bash

Breakdown of the Command:

  • docker run → Creates and starts a new container
  • -d → Runs the container in detached mode (background)
  • -i → Keeps STDIN open (interactive mode)
  • -t → Allocates a pseudo-TTY (terminal)
  • ubuntu → The Docker image to use (defaults to ubuntu:latest)
  • /bin/bash → The command to run inside the container (Bash shell)

In this case, if you type exit or Ctrl + D, you will not terminate the main shell process, and the container will stop. The container will continue running until you manually stop it.

✅ Example:

docker exec -it my_container /bin/bash
# Do some work inside...
exit # or Ctrl + D

🟢 The container remains running in the background.


✅ How to Exit from a Container Without Killing It

If you’re connected to a container and want to leave it running, here’s how to safely exit:


🔑 Method 1: Detach from docker attach

If you’re attached to a container using docker attach, use:

Ctrl + P, then Ctrl + Q

➡️ This detaches from the container without stopping it.

✅ Example: Container Started with -it (Attached Mode)

docker run -it ubuntu /bin/bash
# You're now inside the container...
exit # or Ctrl + D

🔴 The container stops after you exit because it’s running in the foreground and the main process has ended.


🧠 Tip: If you want the container to keep running, use Ctrl + P followed by Ctrl + Q to detach instead of exiting.


🔄 Method 2: Exit a docker exec Session

If you entered the container with docker exec -it, just use:

exit

or

Ctrl + D

➡️ This ends your session, but the container keeps running.


✅ Example:

docker exec -it my_container /bin/bash
# Do some work inside...
exit # or Ctrl + D

🟢 The container remains running in the background.


❌ How to Exit from a Container By Killing It

If you want to stop the container completely when exiting:


🔪 Method 1: Ctrl + C in Attached Mode

When running a container in -it mode and you press:

Ctrl + C

➡️ It sends a SIGINT (interrupt) to the container’s main process, usually causing it to stop.


🔚 Method 2: Typing exit in an -it Session

If you started a container using -it and then type:

exit

➡️ This ends the main process, and the container will stop.

🛑 Reminder: In -it mode, unless detached using Ctrl + P + Q, exiting will stop the container.


📌 Summary Table

ActionCommand / Key ComboEffect on Container
Run container (attached)docker run -it ubuntu /bin/bashConnected; exits will stop it
Run container (detached)docker run -dit ubuntu /bin/bashBackground; keeps running
Exit (keep running)Ctrl + P, Ctrl + QDetaches, container runs
Exit from exec shellexit or Ctrl + DShell closes, container runs
Exit -it (kill container)exit or Ctrl + CStops the container

🏁 Final Thoughts:

Exiting a Docker container isn’t just about typing exit. It’s about knowing what mode you’re in and what your goal is:

  • 🎛️ Use -it for interactive containers — but remember, exit stops them!
  • 🧳 Use -dit for background tasks
  • 🧠 Use Ctrl + P + Ctrl + Q to keep a container running while leaving it

Now you know exactly how to exit — the smart way!


🙋‍♂️ FAQs

❓ What happens if I exit a container started with -it?

If you type exit or press Ctrl + D, you will end the main shell process, and the container will stop automatically.

To avoid this, use Ctrl + P + Ctrl + Q to detach without stopping it.


❓ How can I keep an -it container running after exit?

You must detach instead of exiting. Use:

Ctrl + P, then Ctrl + Q

➡️ This keeps the container running in the background.


❓ How do I confirm if my container is still running?

Use:

docker ps

✔️ If it’s listed — it’s still running.
❌ If not — it has stopped.


❓ Can I re-enter or reconnect to a container?

Yes!

  • To reconnect to its main terminal:
docker attach <container_name_or_id>
  • To open a new shell inside it:
docker exec -it <container_name_or_id> /bin/bash

Leave a Reply

Your email address will not be published. Required fields are marked *

Share this Doc

How to Exit from a Docker Container

Or copy link

CONTENTS
Scroll to Top