- English
- English
Appearance
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.
| No. | Command format | Example command | Description |
|---|---|---|---|
| 1 | docker pull <image>:<tag> | docker pull nginx:1.27 | Pulls an image to the local machine. This command does not start a container. |
| 2 | docker images | docker images | Lists images available on the local machine. |
| 3 | docker 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. |
| 4 | docker tag <local-image>:<local-tag> <new-image>:<new-tag> | docker tag hello-web:1.0 bagus/hello-web:1.0 | Adds another name or tag for the same image. This command does not create a new image copy. |
| 5 | docker rmi <image>:<tag> | docker rmi hello-web:1.0 | Removes an image from the local machine. It fails if a container still uses the image. |
| No. | Command format | Example command | Description |
|---|---|---|---|
| 6 | docker run -d --name <container-name> -p <local-port>:<container-port> <image>:<tag> | docker run -d --name web -p 8080:80 nginx:1.27 | Creates and runs a container in the background. Laptop port 8080 is mapped to port 80 inside the container. |
| 7 | docker ps | docker ps | Lists running containers. |
| 8 | docker ps -a | docker ps -a | Lists all containers, including stopped containers. |
| 9 | docker logs <container-name> | docker logs web | Shows process output from inside a container. |
| 10 | docker logs -f <container-name> | docker logs -f web | Follows new log entries live. Press Ctrl+C to stop following the logs. |
| 11 | docker exec -it <container-name> <command> | docker exec -it web sh | Runs a command inside a running container. Type exit to leave the container shell. |
| 12 | docker stop <container-name> | docker stop web | Stops a container and keeps it so you can start it again. |
| 13 | docker start <container-name> | docker start web | Starts a stopped container with its previous configuration. |
| 14 | docker rm <container-name> | docker rm web | Removes a stopped container. |
| 15 | docker rm -f <container-name> | docker rm -f web | Stops and removes a running container. It does not automatically remove the image or named volumes. |
| No. | Command format | Example command | Description |
|---|---|---|---|
| 16 | docker login -u <dockerhub-username> | docker login -u bagus | Logs 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. |
| 17 | docker push <registry>/<repository>:<tag> | docker push bagus/hello-web:1.0 | Pushes an image from the local machine to a registry. The image must have the correct repository tag and you must already be logged in. |
| No. | Command format | Example command | Description |
|---|---|---|---|
| 18 | docker volume ls | docker volume ls | Lists named volumes managed by Docker. |
| 19 | docker 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:16 | Mounts a named volume at a data path inside a container. The named volume remains after the container is removed. |
| 20 | docker 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.27 | Mounts a folder from the laptop into a container. The :ro suffix makes the folder read-only inside the container. |
| No. | Command format | Example command | Description |
|---|---|---|---|
| 21 | docker network create <network-name> | docker network create app-network | Creates a user-defined network so containers can communicate using container names. |
| 22 | docker run -d --name <container-name> --network <network-name> <image>:<tag> | docker run -d --name db --network app-network postgres:16 | Places a container on a specific network. Other containers on the same network can use db as the hostname. |
| 23 | docker network ls | docker network ls | Lists networks available on the local machine. |
| No. | Command format | Example command | Description |
|---|---|---|---|
| 24 | docker 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:16 | Provides configuration to a container without changing the image. Avoid writing production secrets directly in a command or shell history. |
| No. | Command format | Example command | Description |
|---|---|---|---|
| 25 | docker compose up -d | docker compose up -d | Starts all services from compose.yaml in the background. |
| 26 | docker compose logs -f | docker compose logs -f | Follows new log entries from all services live. |
| 27 | docker compose logs -f <service-name> | docker compose logs -f db | Follows new log entries from a specific service live. |
| 28 | docker compose down | docker compose down | Stops and removes Compose containers and network. Named volumes remain. |
| 29 | docker compose down -v | docker compose down -v | Stops and removes Compose containers, network, and named volumes. Use this only when you want to reset local data. |
| 30 | docker compose up -d --build | docker compose up -d --build | Rebuilds images for services that use build, then starts the services in the background. |