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

> Introductory guide to running Docker containers, using Docker Hub images like Alpine and NGINX, and common docker run flags and examples.

In this lesson you'll run your first Docker containers and explore Docker Hub — Docker's central registry of prebuilt images you can use to create containers locally.

Docker Hub hosts images for many purposes: base operating systems (Ubuntu, Alpine, Debian), web servers (NGINX), databases (Postgres, MySQL, MongoDB), machine learning frameworks (TensorFlow, PyTorch), and more. Choose an image and run it with `docker run` to create a container.

Useful links:

* [Docker Hub](https://hub.docker.com/)
* [Docker documentation — run containers](https://docs.docker.com/engine/reference/commandline/run/)

Example terminal prompt used below:

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

## What you'll see in this lesson

* Pulling small base images (Alpine) and running short commands
* Running a service image (NGINX) and mapping ports to access it from the host
* Quick reference for common `docker run` flags

| Image category | Use case | Example |
| - | -: | - |
| Minimal OS | Fast pulls, small attack surface | `docker run alpine` |
| Web server | Host static sites or reverse proxy | `docker run --rm -p 8080:80 nginx` |
| Database | Run stateful services (use volumes for persistence) | `docker run --rm -e POSTGRES_PASSWORD=mysecret postgres` |
| ML frameworks | Experiment with models in isolated environments | `docker run --gpus all tensorflow/tensorflow:latest-gpu` |

## Try a minimal OS image: Alpine

Alpine Linux is a tiny distribution commonly used for base images. Its small size (a few MB) makes pulls fast and is ideal for lightweight containers.

Run Alpine (no command specified):

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

If the image is not present locally, Docker pulls it first. Example summarized output when pulling:

```bash theme={null}
Unable to find image 'alpine:latest' locally
latest: Pulling from library/alpine
Digest: sha256:5b10f432ef3da1b8d4c7eb6c487f2f5a8f096bc91145e68878dd4a5019afde1
Status: Downloaded newer image for alpine:latest
```

Note: Running `docker run alpine` with no foreground process typically exits immediately because the container has nothing to keep it alive. To run a short-lived command inside the container:

```bash theme={null}
ubuntu@docker-demo:~$ docker run alpine sleep 10
```

This creates a container, runs `sleep 10` inside it, and then the container exits after 10 seconds.

<Callout icon="lightbulb" color="#1CB2FE">
  Containers remain running only while their main process runs. To explore a container interactively, start a shell with `docker run -it --rm alpine sh`. The `--rm` flag removes the container when it exits.
</Callout>

To inspect a file or output non-interactively and remove the container afterwards:

```bash theme={null}
ubuntu@docker-demo:~$ docker run --rm alpine cat /etc/os-release
```

This prints the OS release info, then exits and removes the container.

## Run a real application: NGINX

NGINX images are available in several variants (Alpine-based, slim, full) and tagged per version. Run the default NGINX image:

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

If the image isn't local, Docker pulls required layers first. Example summarized startup logs:

```bash theme={null}
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
Status: Downloaded newer image for nginx:latest
/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
/docker-entrypoint.sh: Configuration complete; ready for start up
2026/05/18 17:10:32 [notice] 1#1: using the "epoll" event method
2026/05/18 17:10:32 [notice] 1#1: nginx/1.31.0
2026/05/18 17:10:32 [notice] 1#1: start worker processes
```

By default the container listens on port 80 internally. To access the server from your host, publish the container port to a host port:

```bash theme={null}
ubuntu@docker-demo:~$ docker run --rm -p 8080:80 nginx
```

Then open `http://localhost:8080` (or the host IP and port) to see the NGINX welcome page.

<Callout icon="warning" color="#FF6B6B">
  When mapping container ports to host ports, be careful exposing services directly to public networks. For production use, secure your service, use firewalls, and control access. Avoid publishing unnecessary ports.
</Callout>

## Quick reference: common docker run flags

| Flag | Purpose | Example |
| - | - | - |
| `-it` | Interactive terminal (allocate TTY and keep STDIN open) | `docker run -it alpine sh` |
| `--rm` | Remove container after exit (useful for one-off commands) | `docker run --rm alpine cat /etc/os-release` |
| `-p host:container` | Publish container port to host | `docker run -p 8080:80 nginx` |
| `-e KEY=VALUE` | Set environment variable inside container | `docker run -e POSTGRES_PASSWORD=secret postgres` |
| `--name` | Assign a name to the container | `docker run --name my-nginx -p 8080:80 nginx` |
| `-v host:container` | Mount host directory or volume | `docker run -v /host/data:/data my-image` |

## Next steps

After this introduction, useful topics to learn next include:

* Image tags and version pinning (use specific tags, not `latest`)
* Persistent storage with volumes
* Container networking and port management
* Container lifecycle and process management

Further reading:

* [Docker Documentation](https://docs.docker.com/)
* [Docker Hub — Official NGINX Image](https://hub.docker.com/_/nginx)
* [NGINX Documentation](https://nginx.org/en/docs/)

<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/ea659025-2770-46b3-863e-c86f3ab6d004" />
</CardGroup>


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