The Docker CLI is self-documenting. To see every command available in your installation, run:
shell
1docker --helpTo see the options of a single command, add --help after it. For example, for docker run:
shell
1docker run --helpAll commands and outputs below were run on Docker Engine 29.9 on Linux, and the core flow was repeated in Docker Desktop on a Mac. Each section builds on the previous one, so you can follow the whole guide in a terminal from top to bottom. Where an output is long, we cut it and mark the cut with three dots (…).
Docker commands cheat sheet
Here are the commands this guide covers, grouped by what you do with them. The full list lives in the Docker CLI reference.
Systemdocker version- client and server versions.docker info- system-wide information: containers, images, storage driver, resources.docker system df- how much disk space images, containers, volumes and build cache use.docker system prune- remove everything unused.
docker search- search Docker Hub for images.docker pull- download an image from a registry.docker images- list local images (same asdocker image ls).docker history- show the layers an image was built from.docker build- build an image from a Dockerfile.docker tag- give an image another name, for example for a registry.docker push- upload an image to a registry.docker rmi- remove images.
docker run- create and start a container from an image.docker ps- list running containers;docker ps -alists all of them.docker start,docker stop,docker restart- control the container lifecycle.docker kill- stop a container immediately.docker logs- show what the container wrote to stdout and stderr.docker exec- run a command, or a shell, inside a running container.docker attach- connect your terminal to the container's main process.docker inspect- low-level details about a container, image, network or volume.docker port,docker stats- published ports and live resource usage.docker cp- copy files between your machine and a container.docker rm- remove containers.
docker login,docker logout- authenticate to Docker Hub or another registry.
docker network create,docker network ls,docker network rmdocker volume create,docker volume ls,docker volume rm
docker compose up,docker compose ps,docker compose logs,docker compose exec,docker compose down
Most short commands have a longer "management" form that groups commands by object: docker ps is docker container ls, docker images is docker image ls, docker rmi is docker image rm. Both forms work, so use whichever you remember.
Check the Docker version and system information
Whether you work on a laptop or a cloud server, the first thing to check is which Docker you're talking to:
shell
1$ docker version2Client:3 Version: 29.9.24 API version: 1.565 Go version: go1.26.106 Git commit: a82e5287 Built: Tue Oct 20 18:43:37 20268 OS/Arch: linux/arm649 Context: default10
11Server: Docker Engine - Community12 Engine:13 Version: 29.9.214 API version: 1.56 (minimum version 1.40)15 Go version: go1.26.1016 Git commit: 0367cbe17 Built: Tue Oct 20 18:44:59 202618 OS/Arch: linux/arm6419 Experimental: false20 containerd:21 Version: v2.4.222 GitCommit:23 runc:24 Version: 1.5.225 GitCommit: v1.5.2-0-g29dd3dc26 docker-init:27 Version: 0.19.028 GitCommit: de40ad0The output has two parts. The client is the docker command you type, and the server is the Docker Engine that actually runs containers. They can be on different machines and different versions.
In Docker Desktop, the server line reads Server: Docker Desktop followed by the Desktop version.
For the bigger picture, use docker info:
shell
1$ docker info2Client:3 Version: 29.9.24 Context: default5 Debug Mode: false6 Plugins:7 buildx: Docker Buildx (Docker Inc.)8 Version: v0.38.19 Path: /usr/local/libexec/docker/cli-plugins/docker-buildx10 compose: Docker Compose (Docker Inc.)11 Version: v5.6.112 Path: /usr/local/libexec/docker/cli-plugins/docker-compose13
14Server:15 Containers: 016 Running: 017 Paused: 018 Stopped: 019 Images: 020 Server Version: 29.9.221 Storage Driver: overlayfs22 driver-type: io.containerd.snapshotter.v123 Logging Driver: json-file24 Cgroup Driver: cgroupfs25 Cgroup Version: 226…27 Kernel Version: 6.12.76-linuxkit28 Operating System: Alpine Linux v3.24 (containerized)29 OSType: linux30 Architecture: aarch6431 CPUs: 1032 Total Memory: 7.75GiB33…It shows how many containers are running, paused and stopped, how many images you have, the storage driver, the kernel, and how much CPU and memory the engine can use. That's the first place to look when containers run out of memory or disk, or when you need to know whether the Compose and Buildx plugins are installed.
Search and pull images
You can search Docker Hub right from the terminal:
shell
1$ docker search --limit 5 ubuntu2NAME DESCRIPTION STARS OFFICIAL3ubuntu Ubuntu is a Debian-based Linux operating sys… 17873 [OK]4ubuntu/squid Squid is a caching proxy for the Web. Long-t… 1315ubuntu/nginx Nginx, a high-performance reverse proxy & we… 1436ubuntu/kafka Apache Kafka, a distributed event streaming … 627ubuntu/bind9 BIND 9 is a very flexible, full-featured DNS… 121Stars show popularity, and [OK] in the OFFICIAL column marks a Docker Official Image, a curated image that Docker reviews and publishes. To see only official images, add a filter:
shell
1$ docker search --filter is-official=true --limit 3 nginx2NAME DESCRIPTION STARS OFFICIAL3nginx Official build of Nginx. 21414 [OK]Download the official Ubuntu image with docker pull:
shell
1$ docker pull ubuntu2Using default tag: latest3latest: Pulling from library/ubuntu4…5Digest: sha256:6a06111c3b0a8600feadb270aec771dc239bf407b25625f93f44ab00e11f4d3c6Status: Downloaded newer image for ubuntu:latest7docker.io/library/ubuntu:latestWithout a tag, Docker pulls latest. For Ubuntu, latest points to the newest LTS release, which you can check by running a throwaway container:
shell
1$ docker run --rm ubuntu grep PRETTY_NAME /etc/os-release2PRETTY_NAME="Ubuntu 26.04.1 LTS"latest moves when a new release comes out, so for anything you deploy, pin a tag. To get Ubuntu 24.04 LTS, pull it by tag:
shell
1$ docker pull ubuntu:24.04224.04: Pulling from library/ubuntu3…4Status: Downloaded newer image for ubuntu:24.045docker.io/library/ubuntu:24.04Pull NGINX too, we'll use it in the next sections:
shell
1docker pull nginxNow list your local images:
shell
1$ docker images2IMAGE ID DISK USAGE CONTENT SIZE EXTRA3nginx:latest 5ab5b0b01097 261MB 65.1MB4ubuntu:24.04 fe81560990fa 141MB 30.9MB5ubuntu:latest 6a06111c3b0a 181MB 44.9MBThe available tags for each image are listed on its Docker Hub page, for example Ubuntu and NGINX.
You don't need a Docker Hub account to pull public images. Anonymous pulls are limited to 100 per 6 hours per IP address, and a free account doubles that, according to the Docker Hub usage docs. On a shared CI runner, log in to avoid hitting the limit.
Run a container
Start an NGINX container in the background, publish its port 80 on port 8080 of your machine, and give it a name:
shell
1$ docker run -d -p 8080:80 --name web nginx2e1643f43bdd35c03824d7b9c9d81ad7b6ee1b909d4259deea1033685fd1d53ed-d(detached) runs the container in the background and prints its ID. Without it, the container is attached to your terminal and stops when you press Ctrl+C.-p 8080:80sends traffic from port 8080 on your machine to port 80 in the container. More on that in our guide on how to expose a port in a Docker container.--name webgives the container a name you can use in every other command.
Check that NGINX answers on http://localhost:8080:
shell
1$ curl -sI http://localhost:80802HTTP/1.1 200 OK3Server: nginx/1.31.84Date: Mon, 02 Nov 2026 13:18:22 GMT5Content-Type: text/html6Content-Length: 8967Last-Modified: Wed, 21 Oct 2026 14:24:51 GMT8Connection: keep-alive9ETag: "6ad8cb33-380"10Accept-Ranges: bytesStart a second NGINX container, this time without a name:
shell
1$ docker run -d -p 8081:80 nginx27f61bf93cbae366a489bfc9cf5af02f82f6379e52c37d7e2c7265e4792eecffbList containers with docker ps
docker ps shows the running containers:
shell
1$ docker ps2CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES37f61bf93cbae nginx "/docker-entrypoint.…" 2 seconds ago Up 1 second 0.0.0.0:8081->80/tcp, [::]:8081->80/tcp condescending_banzai4e1643f43bdd3 nginx "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:8080->80/tcp, [::]:8080->80/tcp webYou see the image, the command, the status and the port mappings. Every command accepts either the CONTAINER ID (the first few characters are enough) or the NAME.
The second container got a random name, condescending_banzai. Yours will be different, so use your own name in the commands below. When you work with many containers, name them yourself with --name and a naming convention.
docker ps only lists running containers. Add -a to see stopped ones too, as you'll see in the next section.
Start, stop, restart and kill containers
Stop the second container:
shell
1$ docker stop condescending_banzai2condescending_banzaiNow compare docker ps -a with the previous output:
shell
1$ docker ps -a2CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES37f61bf93cbae nginx "/docker-entrypoint.…" 2 seconds ago Exited (0) Less than a second ago condescending_banzai4e1643f43bdd3 nginx "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:8080->80/tcp, [::]:8080->80/tcp webThe stopped container is still there, with status Exited (0). docker stop sends the container's stop signal, waits up to 10 seconds for a clean shutdown, and only then kills it.
Start it again, and restart the web container in one go:
shell
1$ docker start condescending_banzai2condescending_banzai3$ docker restart web4webdocker kill doesn't wait. It sends SIGKILL right away, like kill -9 for a process:
shell
1$ docker kill condescending_banzai2condescending_banzaishell
1$ docker ps -a2CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES37f61bf93cbae nginx "/docker-entrypoint.…" 2 seconds ago Exited (137) Less than a second ago condescending_banzai4e1643f43bdd3 nginx "/docker-entrypoint.…" 4 seconds ago Up Less than a second 0.0.0.0:8080->80/tcp, [::]:8080->80/tcp webExit code 137 means the process was killed (128 + signal 9). Use docker kill for a container that ignores docker stop, and docker start brings it back.
View container logs
Whatever the main process writes to stdout and stderr ends up in docker logs:
shell
1$ docker logs --tail 3 web22026/11/02 13:18:24 [notice] 1#1: start worker process 2932026/11/02 13:18:24 [notice] 1#1: start worker process 3042026/11/02 13:18:24 [notice] 1#1: start worker process 31The flags you'll use most:
-ffollows the log live, liketail -f;--tail 100shows only the last 100 lines;--since 10mshows the last 10 minutes;-tadds timestamps.
That's why official images send their logs to stdout: you read them with docker logs instead of looking for files inside the container.
Open a shell in a container with docker exec
You'll often need a shell inside a container to check a config or debug a problem. There's no need for SSH: Docker gives you a shell directly.
First, start an Ubuntu container with an interactive bash shell:
shell
1$ docker run -it --name my_ubuntu ubuntu:24.04 bash2root@25828d357116:/# grep PRETTY_NAME /etc/os-release3PRETTY_NAME="Ubuntu 24.04.5 LTS"4root@25828d357116:/# pwd5/6root@25828d357116:/# exit7exit-i keeps input open and -t gives you a terminal. The prompt root@25828d357116 shows the container ID as the hostname.
Here bash is the container's main process, so exit stops the container:
shell
1$ docker ps -a --filter name=my_ubuntu2CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES325828d357116 ubuntu:24.04 "bash" 3 seconds ago Exited (0) Less than a second ago my_ubuntuStart it again. It keeps running in the background with bash waiting for input:
shell
1$ docker start my_ubuntu2my_ubuntuNow open a second shell inside the running container with docker exec:
shell
1$ docker exec -it my_ubuntu bash2root@25828d357116:/# ps aux3USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND4root 1 0.0 0.0 4300 3632 pts/0 Ss+ 13:18 0:00 bash5root 9 0.0 0.0 4300 3640 pts/1 Ss 13:18 0:00 bash6root 17 0.0 0.0 7632 3656 pts/1 R+ 13:18 0:00 ps aux7root@25828d357116:/# exit8exitps aux shows the difference. PID 1 is the container's main process, and your docker exec shell is a separate process (PID 9). When you exit it, the container keeps running:
shell
1$ docker ps --filter name=my_ubuntu2CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES325828d357116 ubuntu:24.04 "bash" 5 seconds ago Up 2 seconds my_ubuntuYou don't need a shell to run a single command. Drop -it and pass the command:
shell
1$ docker exec my_ubuntu ls /2bin3boot4dev5etc6…7usr8varWhat -i, -t and -d do. docker exec always attaches the command's stdout and stderr to your terminal. That's why the ls output above appears without any flags.
-i also connects your stdin, so you can pipe data into the command: echo hello | docker exec my_ubuntu cat prints nothing, while echo hello | docker exec -i my_ubuntu cat prints hello.
-t allocates a terminal, which an interactive shell needs. -d runs the command in the background and returns right away.
The errors you'll meet most often with docker exec:
Error response from daemon: container … is not running.docker execonly works on running containers. Start the container first. If it crashes on start, readdocker logs, and pull files out withdocker cp, which works on stopped containers too.exec: "bash": executable file not found in $PATH. The image has no bash, which is normal for Alpine and other minimal images. Useshinstead. Distroless images have no shell at all, so debug them from outside withdocker logs,docker inspectanddocker cp.cannot attach stdin to a TTY-enabled container because stdin is not a terminal. You used-tin a script, a pipe or a CI job. Drop-tand keep-iif you need stdin.
Run containers in detached mode and use docker attach
You can start an interactive container in the background right away by adding -d to -it:
shell
1$ docker run -dit --name my_ubuntu_2 ubuntu:24.04 bash28300f8c500cc29da872d523c42ec47fe3a2304e1ad37583b1948918fad88c594docker attach connects your terminal to the container's main process, the same bash you started:
shell
1$ docker attach my_ubuntu_22root@8300f8c500cc:/# read escape sequenceThe important part is how you leave. Type exit and you end the main process, so the container stops.
To detach and leave it running, press Ctrl+P and then Ctrl+Q. Docker prints read escape sequence, and the container is still up:
shell
1$ docker ps --filter name=my_ubuntu_22CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES38300f8c500cc ubuntu:24.04 "bash" 2 seconds ago Up 2 seconds my_ubuntu_2For day-to-day work, docker exec is the safer choice. Use docker attach when you need the main process itself, for example to answer a prompt in an interactive program. The docker attach reference explains how to change the detach keys.
Inspect containers and images
docker inspect returns everything Docker knows about an object as JSON:
shell
1$ docker inspect web | head -122[3 {4 "Id": "e1643f43bdd35c03824d7b9c9d81ad7b6ee1b909d4259deea1033685fd1d53ed",5 "Created": "2026-11-02T13:18:20.493622343Z",6 "Path": "/docker-entrypoint.sh",7 "Args": [8 "nginx",9 "-g",10 "daemon off;"11 ],12 "State": {13 "Status": "running",14…The full output runs to hundreds of lines. Instead of scrolling through it, ask for the one field you need with --format. Here's the container's IP address:
shell
1$ docker inspect --format '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' web2172.18.0.2Two related commands answer the most common questions faster. docker port shows the published ports, and docker stats shows CPU, memory and network usage:
shell
1$ docker port web280/tcp -> 0.0.0.0:8080380/tcp -> [::]:8080shell
1$ docker stats --no-stream web2CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS3e1643f43bdd3 web 0.00% 8.777MiB / 7.75GiB 0.11% 922B / 126B 0B / 4.1kB 11Without --no-stream, docker stats keeps updating until you press Ctrl+C.
Check the history of an image
Images are built in layers, one per Dockerfile instruction. docker history shows those layers:
shell
1$ docker history nginx2IMAGE CREATED CREATED BY SIZE COMMENT35ab5b0b01097 5 days ago CMD ["nginx" "-g" "daemon off;"] 0B buildkit.dockerfile.v04<missing> 5 days ago STOPSIGNAL SIGQUIT 0B buildkit.dockerfile.v05<missing> 5 days ago EXPOSE map[80/tcp:{}] 0B buildkit.dockerfile.v06<missing> 5 days ago ENTRYPOINT ["/docker-entrypoint.sh"] 0B buildkit.dockerfile.v07<missing> 5 days ago COPY 30-tune-worker-processes.sh /docker-ent… 16.4kB buildkit.dockerfile.v08<missing> 5 days ago COPY 20-envsubst-on-templates.sh /docker-ent… 12.3kB buildkit.dockerfile.v09<missing> 5 days ago COPY 15-local-resolvers.envsh /docker-entryp… 12.3kB buildkit.dockerfile.v010<missing> 5 days ago COPY 10-listen-on-ipv6-by-default.sh /docker… 12.3kB buildkit.dockerfile.v011<missing> 5 days ago COPY docker-entrypoint.sh / # buildkit 8.19kB buildkit.dockerfile.v012<missing> 5 days ago RUN /bin/sh -c set -x && groupadd --syst… 86.6MB buildkit.dockerfile.v013<missing> 5 days ago ENV DYNPKG_RELEASE=1~trixie 0B buildkit.dockerfile.v014<missing> 5 days ago ENV PKG_RELEASE=1~trixie 0B buildkit.dockerfile.v015<missing> 5 days ago ENV ACME_VERSION=0.4.1 0B buildkit.dockerfile.v016<missing> 5 days ago ENV NJS_RELEASE=1~trixie 0B buildkit.dockerfile.v017<missing> 5 days ago ENV NJS_VERSION=1.0.2 0B buildkit.dockerfile.v018<missing> 5 days ago ENV NGINX_VERSION=1.31.8 0B buildkit.dockerfile.v019<missing> 5 days ago LABEL maintainer=NGINX Docker Maintainers <d… 0B buildkit.dockerfile.v020<missing> 6 days ago # debian.sh --arch 'arm64' out/ 'trixie' '@1… 110MB debuerreotype 0.17<missing> isn't an error. Layers that were built elsewhere and pulled from a registry don't have local image IDs.
The history tells you what changed between versions, which helps when a container breaks after an image update. Add --no-trunc to see the full commands. docker image history nginx is the same command in the long form.
Copy files with docker cp
Let's replace the NGINX welcome page in the web container. Create index.html in your current directory:
html
1<!DOCTYPE html>2<html>3<head><title>My Website</title></head>4<body>5Hello world6</body>7</html>Check what's in the NGINX web root:
shell
1$ docker exec web ls /usr/share/nginx/html250x.html3index.htmlCopy your file over the default one. docker cp prints nothing when it succeeds:
shell
1docker cp index.html web:/usr/share/nginx/html/index.htmlNow http://localhost:8080 serves your page:
shell
1$ curl -s http://localhost:80802<!DOCTYPE html>3<html>4<head><title>My Website</title></head>5<body>6Hello world7</body>8</html>docker cp works in both directions: docker cp web:/etc/nginx/conf.d/default.conf . copies a file out of the container.
It's handy for debugging, but the change lives only in this container. Remove the container and the file is gone. For a change you want to keep, build an image.
Build your own image with a Dockerfile
To get more "Hello world" containers, put the change in a Dockerfile. Create a file named Dockerfile next to index.html:
dockerfile
1FROM nginx:1.31-alpine2COPY index.html /usr/share/nginx/html/index.htmlFROM sets the base image, pinned to a version so the build gives the same result next month. COPY adds your page on top. Build the image and tag it hello-web:1.0:
shell
1$ docker build -t hello-web:1.0 .2#0 building with "default" instance using docker driver3…4#5 [1/2] FROM docker.io/library/nginx:1.31-alpine@sha256:785835ea65db4bba6f021acc0f3be91b0fef76a53fe41f8588c6698d2aed06065#5 DONE 1.4s6
7#6 [2/2] COPY index.html /usr/share/nginx/html/index.html8#6 DONE 0.1s9
10#7 exporting to image11…12#7 naming to docker.io/library/hello-web:1.0 done13#7 unpacking to docker.io/library/hello-web:1.0 0.3s done14#7 DONE 0.3sThe dot at the end is the build context, the folder whose files the Dockerfile can copy. The command fails without it.
shell
1$ docker images hello-web2IMAGE ID DISK USAGE CONTENT SIZE EXTRA3hello-web:1.0 74314a40ea3e 93.2MB 26.3MBEvery container you start from this image serves your page:
shell
1$ docker run -d -p 8082:80 --name hello hello-web:1.02722cd3d8ffadb5ee75deb70e08a112e2ff7b10d3c2d0db9ac3ea905bfac280823$ curl -s http://localhost:80824<!DOCTYPE html>5<html>6<head><title>My Website</title></head>7<body>8Hello world9</body>10</html>What about docker commit? It still exists and saves a container's current state as an image, for example docker commit web web-snapshot. But nobody can see or repeat how that image was made, and it carries whatever else changed in the container: caches, logs, temp files. Use it to save a broken container for later debugging.
For anything you ship, use a Dockerfile: it's versioned in git, reviewed, and rebuilt the same way every time. The Dockerfile best practices cover the rest, and our guide on how to run a Docker container on your local machine builds and runs a real app image step by step.
Tag, log in and push to a registry
To share an image, push it to a registry. For Docker Hub, log in with:
shell
1docker loginDocker opens a web-based sign-in and shows a one-time device code to confirm in the browser.
On a server or in CI, sign in with a username and a personal access token (PAT) instead of your password. Create the token in your Docker Hub account settings, as described in the access tokens docs, and pass it on stdin so it doesn't end up in your shell history:
shell
1echo "$DOCKERHUB_TOKEN" | docker login --username yourhubusername --password-stdinOld guides show docker login with --email. That flag was removed in Docker 17.06, and today it fails:
shell
1$ docker login --username demo --email demo@example.com2unknown flag: --email3
4Usage: docker login [OPTIONS] [SERVER]5
6Run 'docker login --help' for more informationTo try the whole push flow without a Docker Hub account, run a private registry locally. The official registry:3 image does that. First, create a user demo with the password demo-password in an htpasswd file:
shell
1mkdir -p auth && docker run --rm --entrypoint htpasswd httpd:2.4 -Bbn demo demo-password > auth/htpasswdStart the registry on port 5000 with password authentication:
shell
1docker run -d -p 5000:5000 --name registry -v "$PWD/auth:/auth" -e REGISTRY_AUTH=htpasswd -e REGISTRY_AUTH_HTPASSWD_REALM=Registry -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd registry:3An image goes to the registry that its name points to. Tag hello-web:1.0 with the registry address:
shell
1docker tag hello-web:1.0 localhost:5000/hello-web:1.0Pushing before you log in fails:
shell
1$ docker push localhost:5000/hello-web:1.02The push refers to repository [localhost:5000/hello-web]3push access denied, repository does not exist or may require authorization: authorization failed: no basic auth credentialsLog in with the password on stdin:
shell
1$ export REGISTRY_PASSWORD=demo-password2$ echo "$REGISTRY_PASSWORD" | docker login localhost:5000 --username demo --password-stdin3
4WARNING! Your credentials are stored unencrypted in '/root/.docker/config.json'.5Configure a credential helper to remove this warning. See6https://docs.docker.com/go/credential-store/7
8Login SucceededThe warning matters on servers: without a credential helper, Docker saves the credentials base64-encoded, not encrypted. Docker Desktop uses the system keychain by default. On Linux, set up a credential store or log out when you're done.
Now the push works:
shell
1$ docker push localhost:5000/hello-web:1.02The push refers to repository [localhost:5000/hello-web]3567a969e0221: Pushed4…5cbe54b031f3e: Pushed61.0: digest: sha256:74314a40ea3ed55d62b7a652250da8c21e7230e1ca8357af4298ed495cb414f0 size: 856To check, remove the local tag and pull the image back from the registry:
shell
1$ docker rmi localhost:5000/hello-web:1.02Untagged: localhost:5000/hello-web:1.03$ docker pull localhost:5000/hello-web:1.041.0: Pulling from hello-web5Digest: sha256:74314a40ea3ed55d62b7a652250da8c21e7230e1ca8357af4298ed495cb414f06Status: Downloaded newer image for localhost:5000/hello-web:1.07localhost:5000/hello-web:1.0Log out to remove the stored credentials:
shell
1$ docker logout localhost:50002Removing login credentials for localhost:5000For Docker Hub, the flow is the same, only the image name changes: docker tag hello-web:1.0 yourhubusername/hello-web:1.0, then docker push yourhubusername/hello-web:1.0.
Connect containers with networks and volumes
Containers on the same user-defined network find each other by name, and a volume keeps data after the container is gone. Create both:
shell
1$ docker network create app-net2f0947cc37c65a53aeda2c7c26ee56e854dbd480a362f62de41ee0bede91419b13$ docker volume create cache-data4cache-dataStart Redis on that network, with its data in the volume:
shell
1docker run -d --name cache --network app-net -v cache-data:/data redis:8-alpineAny other container on app-net reaches it by the name cache:
shell
1$ docker run --rm --network app-net redis:8-alpine redis-cli -h cache ping2PONG--rm removes this one-off container as soon as the command finishes. List networks and volumes with ls:
shell
1$ docker network ls2NETWORK ID NAME DRIVER SCOPE3f0947cc37c65 app-net bridge local438993e6149f8 bridge bridge local56cb39cba9523 host host local6d5b80360d6e8 none null localshell
1$ docker volume ls2DRIVER VOLUME NAME3local 40cf6939646ee5f530f6a8551955c3ab99dac5f6f81606468c4225c1e99a84c74local cache-dataThe volume with a long random name is an anonymous volume. The registry image created it for its data. Name resolution doesn't work on the default bridge network, which is one more reason to create your own.
Run multi-container apps with docker compose
Typing long docker run commands for every container gets old fast. Docker Compose describes the whole set in one file. Create compose.yaml next to your Dockerfile:
yaml
1services:2 web:3 build: .4 ports:5 - "8083:80"6 depends_on:7 - cache8 cache:9 image: redis:8-alpine10 volumes:11 - cache-data:/data12
13volumes:14 cache-data:Build and start everything in the background:
shell
1$ docker compose up -d --build2 Image project-web Building3…4 Image project-web Built5…6 Container project-cache-1 Started7 Container project-web-1 Starting8 Container project-web-1 StartedCompose created a network for the project, a volume, built the web image from your Dockerfile and started both containers. It names everything after the project folder, project in our case. The everyday commands mirror the ones you already know:
shell
1$ docker compose ps2NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS3project-cache-1 redis:8-alpine "docker-entrypoint.s…" cache 2 seconds ago Up 2 seconds 6379/tcp4project-web-1 project-web "/docker-entrypoint.…" web 2 seconds ago Up 2 seconds 0.0.0.0:8083->80/tcp, [::]:8083->80/tcpshell
1$ curl -s http://localhost:80832<!DOCTYPE html>3<html>4<head><title>My Website</title></head>5<body>6Hello world7</body>8</html>shell
1$ docker compose exec cache redis-cli ping2PONGshell
1$ docker compose logs --tail 2 cache2cache-1 | 1:M 02 Nov 2026 13:18:51.675 * Ready to accept connections tcp3cache-1 | 1:M 02 Nov 2026 13:18:51.675 # WARNING: Redis does not require authentication and is not protected by network restrictions. Redis will accept connections from any IP address on any network interface.Stop and remove the containers and the network. --volumes removes the volume too, so leave it out when you want to keep the data:
shell
1$ docker compose down --volumes2 Container project-web-1 Stopping3 Container project-web-1 Stopped4 Container project-web-1 Removing5 Container project-web-1 Removed6 Container project-cache-1 Stopping7 Container project-cache-1 Stopped8 Container project-cache-1 Removing9 Container project-cache-1 Removed10 Volume project_cache-data Removing11 Network project_default Removing12 Volume project_cache-data Removed13 Network project_default RemovedNote the space: docker compose, not docker-compose. The hyphenated command is the old standalone Compose v1.
Current Docker ships Compose as a CLI plugin, and the compose line in docker info shows its version. More in the Docker Compose docs.
Remove containers, images and everything unused
Containers, images, volumes and build cache take disk space. See how much with docker system df:
shell
1$ docker system df2TYPE TOTAL ACTIVE SIZE RECLAIMABLE3Images 8 5 974.6MB 248.8MB (25%)4Containers 7 6 323.6kB 81.92kB (25%)5Local Volumes 2 2 26.28MB 0B (0%)6Build Cache 12 0 93.23MB 20.48kBRemove containers
Here's everything this guide has started:
shell
1$ docker ps -a2CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3218eaebc80b0 redis:8-alpine "docker-entrypoint.s…" 6 seconds ago Up 5 seconds 6379/tcp cache48bbd9557c437 registry:3 "/entrypoint.sh /etc…" 12 seconds ago Up 12 seconds 0.0.0.0:5000->5000/tcp, [::]:5000->5000/tcp registry5722cd3d8ffad hello-web:1.0 "/docker-entrypoint.…" 19 seconds ago Up 18 seconds 0.0.0.0:8082->80/tcp, [::]:8082->80/tcp hello68300f8c500cc ubuntu:24.04 "bash" 25 seconds ago Up 25 seconds my_ubuntu_2725828d357116 ubuntu:24.04 "bash" 30 seconds ago Up 27 seconds my_ubuntu87f61bf93cbae nginx "/docker-entrypoint.…" 32 seconds ago Exited (137) 29 seconds ago condescending_banzai9e1643f43bdd3 nginx "/docker-entrypoint.…" 34 seconds ago Up 30 seconds 0.0.0.0:8080->80/tcp, [::]:8080->80/tcp webdocker rm refuses to remove a running container:
shell
1$ docker rm web2Error response from daemon: cannot remove container "web": container is running: stop the container before removing or force removeStop the running ones first, then remove them all. Both commands take several names or IDs at once:
shell
1$ docker stop web hello my_ubuntu my_ubuntu_2 cache registry2web3hello4my_ubuntu5my_ubuntu_26cache7registryshell
1$ docker rm web hello my_ubuntu my_ubuntu_2 condescending_banzai cache registry2web3hello4my_ubuntu5my_ubuntu_26condescending_banzai7cache8registrydocker rm -f stops and removes in one step. It's a kill, not a clean shutdown, so keep it for containers you don't care about:
shell
1$ docker run -d --name tmp nginx234dd1754dabef3e6b3cd883b9f366d46317f4d541de470de444bfa96c8bc80ea3$ docker rm -f tmp4tmpRemove images, volumes and networks
shell
1$ docker rmi localhost:5000/hello-web:1.0 hello-web:1.02Untagged: localhost:5000/hello-web:1.03Untagged: hello-web:1.04Deleted: sha256:74314a40ea3ed55d62b7a652250da8c21e7230e1ca8357af4298ed495cb414f0Both tags pointed to the same image, so the image itself is deleted only when its last tag goes. Volumes and networks are removed by name:
shell
1$ docker volume rm cache-data2cache-data3$ docker network rm app-net4app-netPrune everything unused
docker system prune removes stopped containers, unused networks, dangling images and unused build cache:
shell
1$ docker system prune -f2Deleted build cache objects:3u60dcdfn0hkq80p5l2xybo4no4exlqpdkmsknpg7bhor0pgdl4k5fbqe3rg4le4ni1frbenmay0ov6
7Total reclaimed space: 20.48kBAdd -a to also remove every image that no container uses, and --volumes for anonymous volumes:
shell
1$ docker system prune -a --volumes -f2Deleted Volumes:340cf6939646ee5f530f6a8551955c3ab99dac5f6f81606468c4225c1e99a84c74
5Deleted Images:6untagged: registry:37…8untagged: redis:8-alpine9…10
11Total reclaimed space: 1.068GBWithout -f, Docker lists what it's about to delete and asks for confirmation. Read that list.
The prune commands work on the whole machine, not on one project. On a laptop or server shared with other projects, prune -a also deletes their images, and the next start pulls them again. When in doubt, remove your own containers and images by name, as above.
Now the engine is empty again:
shell
1$ docker ps -a2CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES3$ docker images4IMAGE ID DISK USAGE CONTENT SIZE EXTRAWrapping up
These commands cover most of a Docker workday:
run,psandlogsto start and watch containers;execto get inside;inspectto dig deeper;buildandpushto ship images;composefor multi-container apps;rm,rmiandpruneto clean up.
When a flag doesn't work the way an old guide says, docker <command> --help shows what your version supports.
Want to go deeper on a single topic? See how to expose a port in a Docker container, how to run a Docker container on your local machine, and connecting to the Docker host from a Docker container.
Once your containers move from a laptop to production, the questions change: orchestration, scaling, security, cost. If you're planning that step, our Kubernetes consulting team can help you get there without the usual detours.
