# Docker Engine (Part 5)

When working with Docker containers, you often need to inspect files, check environment variables, or debug a failing service inside a container that is already running in the background. However, running the wrong command—or exiting a session the wrong way—can accidentally kill your container's main process.

In this guide, we will cover how to cleanly detach from an interactive container without stopping it, how to execute one-off or interactive commands inside running containers using `docker container exec`, and the crucial difference between `exec` and `attach`.

## 1\. Detaching Without Stopping a Container (`Ctrl + P`, `Ctrl + Q`)

Suppose you launch an interactive Ubuntu container:

```plaintext
docker container run -it --name my-ubuntu ubuntu
```

You are now inside the container's `bash` shell. If you type `exit` or press `Ctrl + D`, you terminate the primary `bash` process—which causes the entire container to stop and enter an `Exited` state.

### Using the Escape Sequence

To leave the container's terminal while keeping it running in the background, use Docker's **detach escape sequence**:

1. Hold `Ctrl + P`
2. Immediately press `Ctrl + Q`

Docker prints `read escape sequence` and returns you to your host machine's prompt. If you run `docker container ls`, you will see `my-ubuntu` is still `Up` and running in the background.

## 2\. Running Commands Inside a Running Container (`docker container exec`)

When a container is running in the background, you don't need to stop or rebuild it to inspect what's happening inside. The `docker container exec` command starts a **new, secondary process** inside an already running container.

### Running a Single One-Off Command

You can execute a quick command—such as checking the container's hostname or viewing a configuration file—without opening an interactive session:

```plaintext
docker container exec my-ubuntu hostname
```

*(By default, Docker sets a container's hostname to its* ***12-character short Container ID****.)*

You can run any binary available inside the container's filesystem:

```plaintext
# View OS release details
docker container exec my-ubuntu cat /etc/os-release

# List files in the app directory
docker container exec my-ubuntu ls -la /var/www/html
```

### Opening an Interactive Shell (`exec -it`)

For deeper troubleshooting—such as tailing application logs, testing network connectivity, or inspecting running processes—launch an interactive shell (`/bin/bash` or `/bin/sh`) inside the container:

```plaintext
docker container exec -it my-ubuntu /bin/bash
```

*(Note: Minimal base images like* `alpine` *do not include* `bash`*. For Alpine-based containers, use* `/bin/sh` *instead.)*

Because `exec` spawns a **secondary** shell process rather than attaching to PID 1, typing `exit` inside an `exec` session **only closes your debugging shell**—your main container continues running safely in the background.

## 3\. `docker container exec` vs. `docker container attach`

Both `exec` and `attach` let you interact with a running container, so developers often confuse them. Under the hood, they behave very differently:

<table style="min-width: 75px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><td colspan="1" rowspan="1"><p><strong>Feature</strong></p></td><td colspan="1" rowspan="1"><p><strong>docker container exec -it</strong></p></td><td colspan="1" rowspan="1"><p><strong>docker container attach</strong></p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>What it connects to</strong></p></td><td colspan="1" rowspan="1"><p>Spawns a <strong>brand-new process</strong> (e.g., <code>/bin/bash</code>) inside the container.</p></td><td colspan="1" rowspan="1"><p>Connects directly to the container's <strong>main process (PID 1)</strong> (<code>STDIN</code>/<code>STDOUT</code>/<code>STDERR</code>).</p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Typing </strong><code>exit</code><strong> or </strong><code>Ctrl + C</code></p></td><td colspan="1" rowspan="1"><p>Closes <strong>only</strong> the <code>exec</code> shell; the container stays alive.</p></td><td colspan="1" rowspan="1"><p>Kills PID 1 and <strong>stops the entire container</strong> (unless detached with <code>Ctrl + P</code>, <code>Ctrl + Q</code>).</p></td></tr><tr><td colspan="1" rowspan="1"><p><strong>Best use case</strong></p></td><td colspan="1" rowspan="1"><p>Troubleshooting, inspecting logs/configs, or running admin tasks in any running container (like <code>nginx</code>, <code>postgres</code>, or <code>ubuntu</code>).</p></td><td colspan="1" rowspan="1"><p>Reconnecting to an interactive terminal session you previously detached from using <code>Ctrl + P</code>, <code>Ctrl + Q</code>.</p></td></tr></tbody></table>

### Practical Rule of Thumb

- Use `docker container exec -it <name> /bin/bash` 95% of the time when debugging production or background containers—it is safe and won't accidentally terminate your application when you exit.
- Use `docker container attach <name>` only when you specifically want to resume control of the container's primary foreground process.
