---
title: "20 Docker commands you need to know"
description: "Most day-to-day Docker work comes down to a small set of commands."
url: https://sevalla.com/blog/docker-commands/
---

[Blog](https://sevalla.com/blog/)

# 20 Docker commands you need to know

Most day-to-day Docker work comes down to a small set of commands.

Updated: Nov 24, 2026·by Steve Bonisteel

[Docker](https://kinsta.com/blog/what-is-docker/) is a platform that helps developers build, ship, and run applications in containers. A container packages an application with everything it needs to run, so it behaves the same on your laptop, a CI server, and in production.

Docker's [command line interface](https://docs.docker.com/reference/cli/docker/) (CLI) is the main way to work with containers. It has dozens of subcommands, but most day-to-day Docker work comes down to a small set of commands. This article covers the 20 you'll use most, grouped by what they work with: containers, images, data and networks, multi-container apps, and the Docker system itself.

## Getting your hands on the Docker CLI

You get the `docker` CLI with either Docker Engine, the open-source container runtime that's common on Linux servers, or [Docker Desktop](https://docs.docker.com/desktop/), the desktop app for macOS, Windows, and Linux. Even if you mostly use Docker Desktop's interface, the commands below are worth learning, because they're how you script, automate, and troubleshoot Docker.

To follow along, you can build a small website image. Create a folder with an `index.html` file:

```
<h1>Hello from Docker</h1>
```

And a `Dockerfile` that copies it into the official NGINX image:

```
FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html
```

The examples below use this image, tagged `demo-site:1.0`, and a container named `demo-web`.

## Commands for working with containers

### 1\. docker run

`docker run` creates a container from an image and starts it. It's the command you'll use most often:

```
docker run -d --name demo-web -p 127.0.0.1:8080:80 demo-site:1.0
```

The most common options are:

- `-d` runs the container in the background (detached) and prints its ID.
- `--name` gives the container a name, so you don't have to use its ID.
- `-p 127.0.0.1:8080:80` publishes port 80 of the container on port 8080 of your machine. Including `127.0.0.1` keeps the port reachable only from your machine.
- `-e KEY=value` sets an environment variable, and `-v` mounts a volume (covered below).
- `-it` opens an interactive terminal, and `--rm` removes the container when it stops, which is handy for one-off commands.

After running the command above, open `http://localhost:8080` in your browser, and you'll see "Hello from Docker".

### 2\. docker ps

`docker ps` lists your running containers:

```
docker ps
```

The output shows each container's ID, image, command, status, published ports, and name:

```
CONTAINER ID   IMAGE           COMMAND                  CREATED        STATUS        PORTS                    NAMES
4396d5efe44b   demo-site:1.0   "/docker-entrypoint.…"   1 second ago   Up 1 second   127.0.0.1:8080->80/tcp   demo-web
```

Useful options include:

- `-a` lists all containers, including stopped ones.
- `-s` shows how much disk space each container uses.
- `--filter` narrows the list, for example, `docker ps --filter name=demo` or `docker ps --filter status=exited`.

`docker ps` is short for `docker container ls`, which does the same thing.

### 3\. docker exec

`docker exec` runs a command inside a running container:

```
docker exec demo-web cat /usr/share/nginx/html/index.html
```

To open an interactive shell inside the container, add `-it` and run a shell:

```
docker exec -it demo-web sh
```

Use `sh` for images based on Alpine Linux, like this one, and `bash` for images that include it, such as those based on Debian or Ubuntu. Type `exit` to leave the shell without stopping the container.

### 4\. docker logs

`docker logs` shows what a container has written to its standard output and error streams, which is usually where applications in containers send their logs:

```
docker logs demo-web
```

Two options make it much more useful:

- `-f` follows the logs in real time, like `tail -f`. Press **Ctrl+C** to stop following.
- `--tail 50` shows only the last 50 lines.

For example, `docker logs --tail 2 demo-web` shows the last two lines, here NGINX's startup message and the request from your browser:

```
2026/11/24 14:49:05 [notice] 1#1: start worker process 37
172.17.0.1 - - [24/Nov/2026:14:49:06 +0000] "GET / HTTP/1.1" 200 27 "-" "curl/8.7.1" "-"
```

### 5\. docker stop, start, and restart

These commands control a container's lifecycle without removing it:

```
docker stop demo-web
docker start demo-web
docker restart demo-web
```

`docker stop` asks the container's main process to shut down gracefully and forcibly stops it after 10 seconds if it hasn't exited. A stopped container keeps its files and configuration, so `docker start` brings it back exactly as it was.

If a container doesn't respond to `docker stop`, `docker kill` stops it immediately.

### 6\. docker rm

`docker rm` removes one or more stopped containers:

```
docker rm demo-web
```

It only removes containers, not images, volumes, or networks. If the container is still running, Docker refuses:

```
Error response from daemon: cannot remove container "/demo-web": container is running: stop the container before removing or force remove
```

Stop it first, or add `-f` to stop and remove it in one step. Adding `-v` also removes the anonymous volumes attached to the container, but not named volumes.

### 7\. docker cp

`docker cp` copies files between a container and your machine. It works whether the container is running or stopped:

```
docker cp demo-web:/etc/nginx/nginx.conf ./nginx.conf
docker cp ./new.html demo-web:/usr/share/nginx/html/index.html
```

The first command copies NGINX's configuration file out of the container, and the second copies a new HTML file into it. This is handy for inspecting configuration or quickly testing a change. Changes you copy into a container are lost when the container is removed, so make permanent changes in your Dockerfile.

### 8\. docker inspect

`docker inspect` returns detailed information about a container, image, volume, or network as JSON, including its configuration, state, IP address, mounts, and port mappings:

```
docker inspect demo-web
```

The output is long, so it's often easier to pick out specific fields with `--format`:

```
docker inspect --format '{{.State.Status}} {{.NetworkSettings.IPAddress}}' demo-web
```

This prints the container's status and internal IP address:

```
running 172.17.0.3
```

## Commands for working with images

### 9\. docker build

`docker build` builds an image from a Dockerfile. Run it in the folder that contains the Dockerfile:

```
docker build -t demo-site:1.0 .
```

The `-t` option names and tags the image (`demo-site` with the tag `1.0`), and the `.` at the end sets the build context, the folder whose files the Dockerfile can copy into the image. Modern Docker versions build with BuildKit, which caches each step, so rebuilding after a small change is fast.

### 10\. docker images

`docker images` lists the images stored on your machine, with their repository, tag, ID, and size:

```
docker images
```

You can pass a name to list only matching images, for example, `docker images demo-site`. It's short for `docker image ls`.

### 11\. docker pull

`docker pull` downloads an image from a registry, which is [Docker Hub](https://hub.docker.com/) unless you name another one:

```
docker pull redis:alpine
```

The part after the colon is the tag. If you leave it out, Docker pulls the `latest` tag. For production, pin a specific version tag, such as `redis:8-alpine`, so a new release doesn't change your image unexpectedly. `docker run` also pulls an image automatically if it isn't on your machine yet.

### 12\. docker push

`docker push` uploads an image to a registry, so you can share it or deploy it elsewhere:

```
docker push your-username/demo-site:1.0
```

The image name must include the registry and repository you're pushing to. For Docker Hub, that's your username followed by the repository name. For other registries, it starts with the registry's host name, such as `ghcr.io/your-org/demo-site:1.0` for GitHub Container Registry. Log in to the registry with `docker login` before you push.

### 13\. docker tag

`docker tag` gives an existing image another name or tag, without copying it:

```
docker tag demo-site:1.0 demo-site:latest
```

Both names now point to the same image ID. Tagging is also how you prepare an image for a registry: tag it with the full registry name, like `your-username/demo-site:1.0`, then push that tag.

### 14\. docker rmi

`docker rmi` removes images from your machine to free up disk space:

```
docker rmi demo-site:1.0
```

You can refer to an image by name and tag or by ID. If several tags point to the same image, removing one tag only removes that tag, and the image stays until its last tag is removed. Docker won't remove an image that a container is still using, even a stopped one, so remove the container first. `docker rmi` is short for `docker image rm`.

## Commands for data and networking

### 15\. docker volume

Files written inside a container disappear when the container is removed. Volumes store data outside the container's life cycle, which is what you want for databases and uploads. The `docker volume` command manages them:

- `create` creates a volume.
- `ls` lists volumes.
- `inspect` shows a volume's details, such as where its data is stored.
- `rm` removes a volume, and `prune` removes all volumes that no container uses.

Here's a volume that keeps a file after the container that wrote it is gone:

```
docker volume create demo-data
docker run --rm -v demo-data:/data alpine sh -c 'echo saved > /data/note.txt'
docker run --rm -v demo-data:/data alpine cat /data/note.txt
```

The second container reads the file the first one wrote and prints `saved`. Be careful with `docker volume rm` and `docker volume prune`, because they permanently delete the data in those volumes.

### 16\. docker network

The `docker network` command manages networks, which let containers talk to each other. Its subcommands include `create`, `ls`, `inspect`, `connect`, `disconnect`, and `rm`.

Containers on the same user-defined network can reach each other by name:

```
docker network create demo-net
docker run -d --name demo-redis --network demo-net redis:alpine
docker run --rm --network demo-net redis:alpine redis-cli -h demo-redis ping
```

The last command connects to the Redis container by its name, `demo-redis`, and prints `PONG`.

## Commands for multi-container apps

### 17\. docker compose

Most applications need more than one container, such as a web server, a database, and a cache. Docker Compose defines them all in a `compose.yaml` file:

```
services:
  web:
    image: nginx:alpine
    ports:
      - "127.0.0.1:8081:80"
  cache:
    image: redis:alpine
```

Then you manage the whole app with a few commands:

- `docker compose up -d` creates and starts all the services in the background.
- `docker compose ps` lists the services and their status.
- `docker compose logs -f` follows the logs of every service.
- `docker compose exec web sh` opens a shell in a service's container.
- `docker compose down` stops and removes the containers and the network Compose created. Add `-v` to remove its volumes, too.

Compose automatically puts the services on a shared network, so `web` can reach `cache` by its service name. Use `docker compose` (Compose V2), which is built into current Docker versions, rather than the old standalone `docker-compose` command.

## Commands for managing Docker

### 18\. docker system

The `docker system` command shows and cleans up Docker's own resources:

- `df` shows how much disk space images, containers, volumes, and the build cache use, and how much you can reclaim.
- `prune` removes resources you're not using.
- `info` shows details about your Docker installation.
- `events` streams events, such as containers starting and stopping, in real time.

`docker system prune` is the quickest way to free up disk space, but check what it removes before you confirm:

```
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]
```

By default, it doesn't touch volumes or images that have a tag. Adding `-a` also removes every image that no container uses, and adding `--volumes` removes unused volumes too, which deletes their data permanently. To clean up one kind of resource at a time, use the more specific commands: `docker container prune`, `docker image prune`, `docker volume prune`, and `docker network prune`.

### 19\. docker context

A Docker context tells the CLI which Docker Engine to talk to. With contexts, you can switch between your local Docker and a remote server:

- `ls` lists your contexts and marks the current one with an asterisk.
- `create` creates a context, for example, for a remote server over SSH.
- `use` switches to a context.
- `inspect` shows a context's details.

For example, this creates a context for a remote server and runs `docker ps` on it:

```
docker context create my-server --docker "host=ssh://deploy@server.example.com"
docker --context my-server ps
```

### 20\. docker update

`docker update` changes the resource limits and restart policy of a container without recreating it:

```
docker update --memory 256m --memory-swap 256m --restart unless-stopped demo-web
```

Useful options include:

- `--memory` sets a memory limit. When you set it on a running container, also set `--memory-swap`, as in the example.
- `--cpus` limits how many CPUs the container can use.
- `--restart` sets the restart policy, such as `unless-stopped`, which restarts the container automatically unless you stopped it yourself.

## Other useful Docker commands

Here are a few more commands that come in handy less often:

- `docker pause` and `docker unpause` freeze and resume all processes in a container.
- `docker rename` renames a container, as in `docker rename demo-web web`.
- `docker history` shows the layers an image was built from and their sizes.
- `docker search` searches Docker Hub for images, as in `docker search --filter is-official=true redis`.
- `docker commit` saves a container's current state as a new image. It's useful for capturing a container while debugging, but build images from a Dockerfile for anything you keep, because a committed image can't be reproduced.

To learn every option of a command, add `--help`, as in `docker run --help`, or see the [Docker CLI reference](https://docs.docker.com/reference/cli/docker/).

## Summary

Docker is an effective tool for building and running containerized applications, and you only need a handful of its commands for most of your work. You use `docker build`, `run`, `ps`, `exec`, and `logs` every day, `docker compose` for apps with several containers, and `docker system prune` to keep your disk from filling up.

Mastering the 20 commands above will make working with containers much faster, from your first `docker run` to troubleshooting a production issue.

When your containerized app is ready to go live, you can deploy it with Sevalla's [Application Hosting](https://sevalla.com/application-hosting/). Sevalla can [build your app from its Dockerfile](https://docs.sevalla.com/applications/builds/dockerfile) straight from your Git repository, or deploy an image you've already pushed to a registry.

Share on:[Tweet](https://x.com/intent/post?text=I've just read this article "20 Docker commands you need to know" on @sevalla%5Fhosting 's blog: https://sevalla.com/blog/docker-commands/ "x.com")[Reddit](https://www.reddit.com/submit?url=https://sevalla.com/blog/docker-commands&title=20 Docker commands you need to know "Reddit")[HackerNews](https://news.ycombinator.com/submitlink?u=https://sevalla.com/blog/docker-commands&t=20 Docker commands you need to know "HackerNews")

## Deep dive into the cloud!

Deploy your application, database, or static site in minutes.

[Get started](https://sevalla.com/signup/)
