Skip to content

Docker Essentials Cheatsheet ​

This cheatsheet contains the Docker commands most often used in local development and daily work. Read the Docker Essentials course for more complete explanations and practice.

Image commands ​

No.Command formatExample commandDescription
1docker pull <image>:<tag>docker pull nginx:1.27Pulls an image to the local machine. This command does not start a container.
2docker imagesdocker imagesLists images available on the local machine.
3docker build -t <image-name>:<tag> <build-context>docker build -t hello-web:1.0 .Builds an image from a Dockerfile. The . means the current folder is used as the build context.
4docker tag <local-image>:<local-tag> <new-image>:<new-tag>docker tag hello-web:1.0 bagus/hello-web:1.0Adds another name or tag for the same image. This command does not create a new image copy.
5docker rmi <image>:<tag>docker rmi hello-web:1.0Removes an image from the local machine. It fails if a container still uses the image.

Container commands ​

No.Command formatExample commandDescription
6docker run -d --name <container-name> -p <local-port>:<container-port> <image>:<tag>docker run -d --name web -p 8080:80 nginx:1.27Creates and runs a container in the background. Laptop port 8080 is mapped to port 80 inside the container.
7docker psdocker psLists running containers.
8docker ps -adocker ps -aLists all containers, including stopped containers.
9docker logs <container-name>docker logs webShows process output from inside a container.
10docker logs -f <container-name>docker logs -f webFollows new log entries live. Press Ctrl+C to stop following the logs.
11docker exec -it <container-name> <command>docker exec -it web shRuns a command inside a running container. Type exit to leave the container shell.
12docker stop <container-name>docker stop webStops a container and keeps it so you can start it again.
13docker start <container-name>docker start webStarts a stopped container with its previous configuration.
14docker rm <container-name>docker rm webRemoves a stopped container.
15docker rm -f <container-name>docker rm -f webStops and removes a running container. It does not automatically remove the image or named volumes.

Registry commands ​

No.Command formatExample commandDescription
16docker login -u <dockerhub-username>docker login -u bagusLogs in to Docker Hub so Docker Engine can push images. Use a personal access token as the password when the account uses two-factor authentication.
17docker push <registry>/<repository>:<tag>docker push bagus/hello-web:1.0Pushes an image from the local machine to a registry. The image must have the correct repository tag and you must already be logged in.

Volume commands ​

No.Command formatExample commandDescription
18docker volume lsdocker volume lsLists named volumes managed by Docker.
19docker run -d --name <container-name> -v <volume-name>:<path-in-container> <image>:<tag>docker run -d --name db -v pgdata:/var/lib/postgresql/data postgres:16Mounts a named volume at a data path inside a container. The named volume remains after the container is removed.
20docker run -d --name <container-name> -v <local-folder>:<path-in-container> <image>:<tag>docker run -d --name web -v "$(pwd)/html:/usr/share/nginx/html:ro" nginx:1.27Mounts a folder from the laptop into a container. The :ro suffix makes the folder read-only inside the container.

Network commands ​

No.Command formatExample commandDescription
21docker network create <network-name>docker network create app-networkCreates a user-defined network so containers can communicate using container names.
22docker run -d --name <container-name> --network <network-name> <image>:<tag>docker run -d --name db --network app-network postgres:16Places a container on a specific network. Other containers on the same network can use db as the hostname.
23docker network lsdocker network lsLists networks available on the local machine.

Environment variable commands ​

No.Command formatExample commandDescription
24docker run -d --name <container-name> -e <VARIABLE_NAME>=<value> <image>:<tag>docker run -d --name db -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=app postgres:16Provides configuration to a container without changing the image. Avoid writing production secrets directly in a command or shell history.

Docker Compose commands ​

No.Command formatExample commandDescription
25docker compose up -ddocker compose up -dStarts all services from compose.yaml in the background.
26docker compose logs -fdocker compose logs -fFollows new log entries from all services live.
27docker compose logs -f <service-name>docker compose logs -f dbFollows new log entries from a specific service live.
28docker compose downdocker compose downStops and removes Compose containers and network. Named volumes remain.
29docker compose down -vdocker compose down -vStops and removes Compose containers, network, and named volumes. Use this only when you want to reset local data.
30docker compose up -d --builddocker compose up -d --buildRebuilds images for services that use build, then starts the services in the background.