20 Docker commands you need to know
Most day-to-day Docker work comes down to a small set of commands.
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 (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, 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.htmlThe 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.0The most common options are:
-druns the container in the background (detached) and prints its ID.--namegives the container a name, so you don't have to use its ID.-p 127.0.0.1:8080:80publishes port 80 of the container on port 8080 of your machine. Including127.0.0.1keeps the port reachable only from your machine.-e KEY=valuesets an environment variable, and-vmounts a volume (covered below).-itopens an interactive terminal, and--rmremoves 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 psThe 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-webUseful options include:
-alists all containers, including stopped ones.-sshows how much disk space each container uses.--filternarrows the list, for example,docker ps --filter name=demoordocker 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.htmlTo open an interactive shell inside the container, add -it and run a shell:
docker exec -it demo-web shUse 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-webTwo options make it much more useful:
-ffollows the logs in real time, liketail -f. Press Ctrl+C to stop following.--tail 50shows 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-webdocker 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-webIt 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 removeStop 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.htmlThe 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-webThe output is long, so it's often easier to pick out specific fields with --format:
docker inspect --format '{{.State.Status}} {{.NetworkSettings.IPAddress}}' demo-webThis prints the container's status and internal IP address:
running 172.17.0.3Commands 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 imagesYou 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 unless you name another one:
docker pull redis:alpineThe 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.0The 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:latestBoth 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.0You 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:
createcreates a volume.lslists volumes.inspectshows a volume's details, such as where its data is stored.rmremoves a volume, andpruneremoves 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.txtThe 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 pingThe 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:alpineThen you manage the whole app with a few commands:
docker compose up -dcreates and starts all the services in the background.docker compose pslists the services and their status.docker compose logs -ffollows the logs of every service.docker compose exec web shopens a shell in a service's container.docker compose downstops and removes the containers and the network Compose created. Add-vto 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:
dfshows how much disk space images, containers, volumes, and the build cache use, and how much you can reclaim.pruneremoves resources you're not using.infoshows details about your Docker installation.eventsstreams 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:
lslists your contexts and marks the current one with an asterisk.createcreates a context, for example, for a remote server over SSH.useswitches to a context.inspectshows 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://[email protected]"
docker --context my-server ps20. 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-webUseful options include:
--memorysets a memory limit. When you set it on a running container, also set--memory-swap, as in the example.--cpuslimits how many CPUs the container can use.--restartsets the restart policy, such asunless-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 pauseanddocker unpausefreeze and resume all processes in a container.docker renamerenames a container, as indocker rename demo-web web.docker historyshows the layers an image was built from and their sizes.docker searchsearches Docker Hub for images, as indocker search --filter is-official=true redis.docker commitsaves 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.
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. Sevalla can build your app from its Dockerfile straight from your Git repository, or deploy an image you've already pushed to a registry.