> ## 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 3

> Explains how Docker pulls and manages images, lists and removes images and containers, and uses pruning commands to safely reclaim disk space

In this lesson we cover Docker images and how to clean them up safely. You'll learn how images are pulled, where to see what is stored locally, how to remove images and containers, and how to prune unused data to reclaim disk space.

## How `docker run` pulls and starts images

When you run a container with a command like `docker run nginx`, Docker will first pull the `nginx` image (if it's not already present) and then start a container from that image. The container's startup logs are printed to the terminal. Example output from running `docker run nginx`:

```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: Looking for shell scripts in /docker-entrypoint.d/
/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: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/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 18:38:59 [notice] 1#1: using the "epoll" event method
2026/05/18 18:38:59 [notice] 1#1: nginx/1.31.0
2026/05/18 18:38:59 [notice] 1#1: built by gcc 14.2.0 (Debian 14.2.0-19)
2026/05/18 18:38:59 [notice] 1#1: OS: Linux 6.8.0-106-generic
2026/05/18 18:38:59 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1024:524288
2026/05/18 18:38:59 [notice] 1#1: start worker processes
2026/05/18 18:38:59 [notice] 1#1: start worker process 29
2026/05/18 18:38:59 [notice] 1#1: start worker process 30
```

## List images stored locally

To inspect images on the local host, use:

* `docker images` — list all images stored locally.

Example output:

```bash theme={null}
ubuntu@docker-demo:~$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
alpine       latest    5b10f432ef3d   3 weeks ago     4.29MB
nginx        latest    06aa3d7be10b   2 hours ago     64.2MB
```

## Remove images and containers

* Remove an image: `docker rmi <image>` (for example `docker rmi alpine:latest`).
* Remove a container: `docker rm <container>`.

Example: removing `alpine:latest`:

```bash theme={null}
ubuntu@docker-demo:~$ docker rmi alpine:latest
Untagged: alpine:latest
Deleted: sha256:5b10f432ef3da1b8d4c7eb6c487f2f5a8f096bc91145e68878dd4a5019afde11
```

After removing `alpine`, the image list shows only `nginx`:

```bash theme={null}
ubuntu@docker-demo:~$ docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        latest    06aa3d7be10b   2 hours ago     64.2MB
```

### Image in use: what happens if a container references the image

If a container (running or stopped) still references an image, Docker will refuse to delete that image and report a conflict:

```bash theme={null}
ubuntu@docker-demo:~$ docker rmi nginx:latest
Error response from daemon: conflict: unable to delete nginx:latest (must be forced) - container 292efb is using its referenced image 06aa3d7be10b
```

To determine why, check your containers:

* `docker ps` — shows only running containers.
* `docker ps -a` — shows all containers, including exited ones that still reference images.

Example:

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

ubuntu@docker-demo:~$ docker ps -a
CONTAINER ID   IMAGE   COMMAND               CREATED         STATUS                      PORTS   NAMES
292efbf123b0   nginx   "/docker-entrypoint…" 2 minutes ago   Exited (0) About a minute ago         affectionate_boyd
```

Because the exited container `292efbf123b0` still references the `nginx` image, remove the container first, then remove the image:

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

Now you can remove the `nginx` image:

```bash theme={null}
ubuntu@docker-demo:~$ docker rmi nginx:latest
Untagged: nginx:latest
Deleted: sha256:06aa3d7be10bc6307990c81bdca075793132e9163391abc370c015e344e23128
```

After cleaning up, `docker ps`, `docker ps -a`, and `docker images` report nothing relevant:

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

ubuntu@docker-demo:~$ docker ps -a
CONTAINER ID   IMAGE   COMMAND   CREATED   STATUS    PORTS   NAMES

ubuntu@docker-demo:~$ docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
```

## Bulk cleanup with `docker system prune`

If you have many stopped containers and dangling or unused images, `docker system prune` is a convenient interactive command to reclaim space. It removes:

* all stopped containers
* all networks not used by at least one container
* all dangling images
* the unused build cache

Run it interactively:

```bash theme={null}
ubuntu@docker-demo:~$ docker system prune
WARNING! This will remove:
 - all stopped containers
 - all networks not used by at least one container
 - all dangling images
 - unused build cache

Are you sure you want to continue? [y/N]
```

If there is nothing to remove, the command reports reclaimed space as `0B`:

```bash theme={null}
Total reclaimed space: 0B
```

Table: quick reference for cleanup options

| Command | What it removes | Notes |
| -: | - | - |
| `docker system prune` | stopped containers, unused networks, dangling images, unused build cache | Interactive confirmation required |
| `docker system prune -a` | everything `prune` does, plus all images not referenced by any container | Use with caution — may remove large images |
| `docker system prune --volumes` | adds unused volumes to removal | Volumes can contain persistent data — be careful |

<Callout icon="warning" color="#FF6B6B">
  Using `docker system prune -a` or adding `--volumes` can delete images and data that you expect to keep. Always double-check which containers and volumes you still need before running these commands.
</Callout>

<Callout icon="lightbulb" color="#1CB2FE">
  If you want to remove all unused images (not just dangling ones), run `docker system prune -a`. That will remove all images not referenced by any container. Use it with caution.
</Callout>

## Links and references

* [Docker documentation: images](https://docs.docker.com/engine/reference/commandline/images/)
* [Docker documentation: system prune](https://docs.docker.com/engine/reference/commandline/system_prune/)

<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/19d2a931-b2f1-478b-8070-53332bbd02fe" />

  <Card title="Practice Lab" icon="flask-conical" cta="Learn more" href="https://learn.kodekloud.com/user/courses/docker-training-course-for-the-absolute-beginner/module/672dc9a2-52ce-467f-b00c-c2f16185479b/lesson/1f0b2e44-a2bc-4cd7-ad78-61ed326c59ec" />
</CardGroup>


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