> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Demo Docker Commands Part 2

> Docker container basics covering foreground and detached runs, listing running and all containers, stopping, removing containers, and inspecting logs and executing into running containers

In this lesson you'll learn how to run containers in the foreground and background (detached mode), how to list running and exited containers, and how to remove containers you no longer need. These are essential Docker basics for managing container lifecycles on development and production hosts.

## Running a container in the foreground (no -d)

By default, `docker run` attaches your terminal to the container's STDOUT/STDERR. This is useful for debugging or when you want to see process output in real time. However, interrupting your terminal (for example with Ctrl+C) sends a termination signal to the container and causes it to exit.

Example: running the official `nginx` image in the foreground

```bash theme={null}
ubuntu@docker-demo:~$ docker run nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2026/05/18 17:52:31 [notice] 1#1: using the "epoll" event method
2026/05/18 17:52:31 [notice] 1#1: nginx/1.31.0
2026/05/18 17:52:31 [notice] 1#1: start worker processes
2026/05/18 17:52:31 [notice] 1#1: start worker process 29
2026/05/18 17:52:31 [notice] 1#1: start worker process 30
^C
2026/05/18 17:52:41 [notice] 1#1: signal 2 (SIGINT) received, exiting
2026/05/18 17:52:41 [notice] 29#29: exiting
2026/05/18 17:52:41 [notice] 29#29: exit
2026/05/18 17:52:41 [notice] 30#30: exiting
2026/05/18 17:52:41 [notice] 30#30: exit
2026/05/18 17:52:41 [notice] 1#1: worker process 29 exited with code 0
2026/05/18 17:52:41 [notice] 1#1: worker process 30 exited with code 0
2026/05/18 17:52:41 [notice] 1#1: exit
ubuntu@docker-demo:~$
```

When you need a running container to continue after you close the terminal, run it in detached mode.

## Running a container in the background (detached mode)

Add the `-d` flag to `docker run` to start the container detached. Docker prints the container ID and returns you immediately to the shell; the container continues running in the background.

Example:

```bash theme={null}
ubuntu@docker-demo:~$ docker run -d nginx
dbb4cce619eb89dc5e550538606d6f4491e284349f5e4d0b03ce668c84915e18
ubuntu@docker-demo:~$
```

The long hex string printed is the full container ID. You can use a short, unique prefix of that ID with most Docker commands.

## List running containers

To see containers that are currently running, use:

```bash theme={null}
docker ps
```

Example output:

```bash theme={null}
ubuntu@docker-demo:~$ docker ps
CONTAINER ID    IMAGE     COMMAND               CREATED          STATUS          PORTS     NAMES
dbb4cce619eb    nginx     "/docker-entrypoint…" 21 seconds ago   Up 20 seconds   80/tcp    adoring_zhukovsky
ubuntu@docker-demo:~$
```

Notes about IDs and names

* The full container ID is 64 hex characters; most commands accept the short form (first 4–12 characters) as long as it is unique.
* Docker assigns a human-readable container name (e.g., `adoring_zhukovsky`) if you don't provide one. You can set a custom name with `--name <name>` when running the container.

## Stopping a running container

Use `docker stop` followed by the container ID, its short unique prefix, or its name.

Example:

```bash theme={null}
ubuntu@docker-demo:~$ docker stop dbb4
dbb4
ubuntu@docker-demo:~$
```

After stopping, `docker ps` will not list that container:

```bash theme={null}
ubuntu@docker-demo:~$ docker ps
CONTAINER ID   IMAGE   COMMAND   CREATED   STATUS    PORTS   NAMES
ubuntu@docker-demo:~$
```

## List all containers (including exited)

To view every container on the host — running and exited — use `docker ps -a`.

Example:

```bash theme={null}
ubuntu@docker-demo:~$ docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED             STATUS                        PORTS     NAMES
dbb4cce619eb   nginx     "/docker-entrypoint…"    About a minute ago  Exited (0) 14 seconds ago               adoring_zhukovsky
af6a9032a3b4   nginx     "/docker-entrypoint…"    About a minute ago  Exited (0) About a minute ago               frosty_carson
c16f0bfc8d8e   nginx     "/docker-entrypoint…"    3 minutes ago       Exited (0) 2 minutes ago                   frosty_rosalind
2e0ed4a373e8   nginx     "/docker-entrypoint…"    3 minutes ago       Exited (0) 3 minutes ago                   vigorous_elgamal
8ddbb9c64dc8   nginx     "/docker-entrypoint…"    43 minutes ago      Exited (0) 43 minutes ago                  elastic_keldysh
0a1bd048936d   alpine    "sleep 10"               44 minutes ago      Exited (0) 44 minutes ago                  agitated_carson
855fa1ec8c58   alpine    "sleep 5"                45 minutes ago      Exited (0) 45 minutes ago                  tender_elbakyan
827e523371f0   alpine    "cat /etc/lsb-release"   45 minutes ago      Exited (1) 45 minutes ago                  beautiful_jennings
82af5bdd0bfd   alpine    "/bin/sh"                46 minutes ago      Exited (0) 46 minutes ago                  epic_lehmann

ubuntu@docker-demo:~$
```

## Removing containers

Exited containers remain on disk until you explicitly remove them. Use `docker rm` to delete container metadata and its writable layer. You can pass multiple IDs or names in one command.

Remove a single container (short ID):

```bash theme={null}
ubuntu@docker-demo:~$ docker rm dbb4
dbb4
```

Remove multiple containers at once:

```bash theme={null}
ubuntu@docker-demo:~$ docker rm af6a c16f 2e0e
af6a
c16f
2e0e
```

After removal, `docker ps -a` will list only the containers that remain:

```bash theme={null}
ubuntu@docker-demo:~$ docker ps -a
CONTAINER ID        IMAGE     COMMAND                  CREATED         STATUS
8ddbb9c64dc8        nginx     "/docker-entrypoint…"    45 minutes ago  Exited (0) 44 minutes ago
0a1bd048936d        alpine    "sleep 10"               46 minutes ago  Exited (0) 46 minutes ago
855fa1ec8c58        alpine    "sleep 5"                46 minutes ago  Exited (0) 46 minutes ago
827e523371f0        alpine    "cat /etc/lsb-release"   47 minutes ago  Exited (1) 47 minutes ago
82af5bdd0bfd        alpine    "/bin/sh"                47 minutes ago  Exited (0) 47 minutes ago
ubuntu@docker-demo:~$
```

## Quick reference — common container lifecycle commands

| Action | Command | Notes |
| - | -: | - |
| Run in foreground | `docker run nginx` | Attaches terminal to container; Ctrl+C will stop it. |
| Run in background (detached) | `docker run -d nginx` | Prints container ID and returns to shell. |
| List running containers | `docker ps` | Shows only running containers by default. |
| List all containers | `docker ps -a` | Includes exited containers. |
| Stop a container | `docker stop <id\|name>` | Sends SIGTERM then SIGKILL after timeout. |
| Remove container(s) | `docker rm <id\|name> [<id2> ...]` | Deletes container metadata and writable layer. |
| View logs | `docker logs <id\|name>` | Works on running or stopped containers. |
| Exec into running container | `docker exec -it <id\|name> /bin/sh` | Use `/bin/bash` if available for an interactive shell. |

<Callout icon="lightbulb" color="#1CB2FE">
  Detached containers still run in the background and produce logs. Use `docker logs <container>` to inspect output, and `docker exec -it <container> /bin/sh` (or `/bin/bash`) to open an interactive shell inside a running container.
</Callout>

<Callout icon="warning" color="#FF6B6B">
  Be careful when removing containers: `docker rm` deletes container metadata and filesystem changes made in that container. If you need to preserve data, consider using volumes or committing the container to an image before removal.
</Callout>

## Links and references

* Docker documentation — containers: [https://docs.docker.com/engine/reference/commandline/container\_ls/](https://docs.docker.com/engine/reference/commandline/container_ls/)
* docker run reference: [https://docs.docker.com/engine/reference/commandline/run/](https://docs.docker.com/engine/reference/commandline/run/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/docker-training-course-for-the-absolute-beginner/module/672dc9a2-52ce-467f-b00c-c2f16185479b/lesson/2be70825-d91b-4dfc-85e7-6eb910cb2133" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.