The Docker commands I use the most, grouped by task
This article was reworked in November 2026. The first version from 2019 had a few commands that no longer match how Docker behaves, and it used the old docker-compose binary.
I have been working with Docker containers since 2019, and a handful of commands cover almost everything I do day to day. This is my list, grouped by what I am trying to get done, with the details that tripped me up at least once.
Images
docker pull nginx:1.27- Downloads an image from a registry. Always give a tag; without one Docker pulls
latest, which changes under your feet.
- Downloads an image from a registry. Always give a tag; without one Docker pulls
docker build -t myapp:1.0 .- Builds an image from the
Dockerfilein the current folder. The.at the end is the build context, the folder whose files Docker can copy into the image. -f path/to/Dockerfileuses a Dockerfile with another name or location.--no-cacheignores the cached layers and builds every step again.
- Builds an image from the
docker image ls- Lists the images on your machine. Two tags with the same IMAGE ID are the same image.
docker tag myapp:1.0 myregistry.azurecr.io/myapp:1.0- Gives a local image the name it needs in a remote registry. No copy is made, it is a second name for the same image.
docker login myregistry.azurecr.ioanddocker push myregistry.azurecr.io/myapp:1.0- Logs in to a registry and uploads the image. Without a registry name,
docker logintargets Docker Hub.
- Logs in to a registry and uploads the image. Without a registry name,
docker image rm myapp:1.0(short:docker rmi)- Removes an image. Docker refuses while a container still uses it;
-fforces it.
- Removes an image. Docker refuses while a container still uses it;
Running containers
docker run -d --name web -p 8080:80 nginx:1.27- Creates and starts a new container from an image.
-druns it in the background.--namegives it a name, so you don’t have to copy IDs around.-p 8080:80maps port 8080 on your machine to port 80 inside the container. All options go before the image name; anything after it is passed to the container as its command.-e KEY=valuesets an environment variable,-vmounts a volume or a folder.--rmdeletes the container as soon as it exits. I use it for one-off containers I don’t want lying around.
docker run -it --rm ubuntu:24.04 bash- Starts a throwaway container with an interactive shell. Type
exitand it is gone.
- Starts a throwaway container with an interactive shell. Type
docker ps- Lists the running containers. Add
-ato see the stopped ones as well.
- Lists the running containers. Add
docker stop webanddocker start web- Stops a running container and starts the same container again.
docker startdoesn’t create anything new; that is the difference todocker run.
- Stops a running container and starts the same container again.
docker restart web- Stop and start in one command.
docker rm web- Deletes a stopped container. Add
-fto stop and delete a running one in one go.
- Deletes a stopped container. Add
Looking inside a container
docker exec -it web bash- Opens a shell in a running container. Many small images have no
bash; useshthere. Leaving withexitdoesn’t stop the container.
- Opens a shell in a running container. Many small images have no
docker exec web mkdir -p /data/import- Runs a single command inside the container without opening a shell.
docker logs -f --tail 100 web- Shows what the container wrote to its standard output and error.
--tail 100starts with the last 100 lines,-fkeeps streaming new lines until you press Ctrl+C.
- Shows what the container wrote to its standard output and error.
docker inspect web- Prints the full configuration of the container as JSON: environment variables, mounts, network, IP address.
docker stats- Live CPU, memory and network usage per container.
Copying files
docker cp works with running and stopped containers.
docker cp settings.json web:/app/settings.jsoncopies a file into the container.docker cp web:/app/logs ./logscopies a folder out of it.docker cp ./seed/. web:/datacopies the content of a folder into a folder in the container.
Volumes
docker volume create pgdata- Creates a named volume, storage that lives outside the container and survives when the container is deleted.
docker run -d -v pgdata:/var/lib/postgresql/data postgres:17- Mounts the volume into a container. Delete the container, start a new one with the same
-v, and the database is still there.
- Mounts the volume into a container. Delete the container, start a new one with the same
docker volume lsanddocker volume rm pgdata- Lists and deletes volumes. Deleting a volume deletes its data for good.
Cleaning up
This is where I see most mistakes, because each prune command removes something different.
docker container pruneremoves all stopped containers. Running ones are not touched.docker image pruneremoves dangling images, the untagged leftovers of earlier builds. Add-ato remove every image no container uses.docker builder pruneremoves the build cache, so the next build runs every step again.docker system prunedoes all of the above in one go: stopped containers, unused networks, dangling images and the build cache. With-ait also removes all unused images. Volumes are kept unless you add--volumes.
Docker Compose
Compose is now part of the Docker CLI and is called with docker compose, with a space. The old standalone docker-compose binary is no longer updated, so if a tutorial still uses it, drop the hyphen.
docker compose up -d- Builds what needs building, creates the network and starts every service in
compose.yamlin the background.
- Builds what needs building, creates the network and starts every service in
docker compose logs -f api- Follows the logs of one service.
docker compose down- Stops and removes the containers and networks that
upcreated. Named volumes are kept, so your database survives adown. Add-vonly when you really want to delete them too.
- Stops and removes the containers and networks that
For every option of these commands, the Docker CLI reference is the place to look.