🖼
Images
6 commandsPull an Image
fetchDownload an image from Docker Hub. Think of it as installing a ready-made app template. Use a tag to get a specific version.
# pull latest version
docker pull <image>
# pull a specific version tag
docker pull <image>:<tag>
# example
docker pull nginx:1.25
List Images
listSee all images stored locally — name, tag, size, and creation date.
docker images
# alternative (same result)
docker image ls
Build an Image
build-t names the image. The path at the end tells Docker where to find the Dockerfile — . means current directory.# build from current directory
docker build -t <image-name> .
# build with a version tag
docker build -t <image-name>:<tag> .
# skip cache — forces a fresh build
docker build --no-cache -t <image-name> .
Remove an Image
deleteDelete a local image to free disk space. Can't remove while a container is still using it — use
-f to force.docker rmi <image>
# remove by name and tag
docker rmi <image>:<tag>
# force remove even if in use
docker rmi -f <image-id>
Search Docker Hub
searchFind available public images on Docker Hub without opening a browser.
docker search <term>
# limit to top 5 results
docker search <term> --limit 5
Tag & Push to Registry
publishLabel your image with your Docker Hub username, then upload it so servers or teammates can pull it.
# add a registry tag to a local image
docker tag <local-image> <username>/<repo>:<tag>
# push to Docker Hub
docker push <username>/<repo>:<tag>
📦
Containers
10 commandsRun a Container
runThe most important command — creates AND starts a container from an image. Combine flags to control how it runs.
# interactive terminal (stay in shell)
docker run -it <image>
# background (detached) mode
docker run -d <image>
# named + port mapping + detached
docker run -d --name <name> -p <host-port>:<container-port> <image>
# pass an environment variable
docker run -e <KEY>=<value> <image>
# auto-delete container when it exits
docker run --rm <image>
List Containers
listSee what's running. Add
-a to also show stopped containers.# only running containers
docker ps
# all containers (including stopped)
docker ps -a
Start / Stop / Restart
controlControl the lifecycle of an existing container using its name or ID.
docker start <container>
docker stop <container>
docker restart <container>
Run Command Inside Container
execJump into a running container to debug or run commands. Use
bash for Ubuntu-based images, sh for Alpine.# open an interactive shell
docker exec -it <container> bash
# alpine-based images use sh
docker exec -it <container> sh
# run a single command and exit
docker exec <container> <command>
View Logs
debugSee what a container is printing.
-f streams logs live. --tail limits to the last N lines.docker logs <container>
# stream live (follow mode)
docker logs -f <container>
# show last N lines only
docker logs --tail <n> <container>
Inspect a Container
debugGet detailed JSON metadata — IP address, mounts, env vars, network config. Use
--format to extract a single field.docker inspect <container>
# extract just the IP address
docker inspect <container> --format '{{.NetworkSettings.IPAddress}}'
Remove a Container
deleteDelete a stopped container. Use
-f to force-remove a running one.docker rm <container>
# force remove even if running
docker rm -f <container>
# auto-delete on exit (set at run time)
docker run --rm <image>
Monitor Resource Usage
monitorLive dashboard showing CPU, memory, and network usage — like Task Manager but for containers.
# all running containers
docker stats
# single container only
docker stats <container>
Port Mapping
networkConnect a port on your machine to a port inside the container. Format:
<host-port>:<container-port>.# host :<host-port> → container :<container-port>
docker run -p <host-port>:<container-port> <image>
# port + env var together
docker run -p <host-port>:<container-port> -e <KEY>=<value> <image>
Copy Files
filesTransfer files between your host and a running container — no volume needed.
# host → container
docker cp <local-path> <container>:<container-path>
# container → host
docker cp <container>:<container-path> <local-path>
📄
Dockerfile Reference
instructionsDockerfile — annotated example
# 1. Base image to build on top of FROM node:20-alpine # 2. Working directory inside the container WORKDIR /app # 3. Copy project files from host into image COPY . . # 4. Run commands at build time RUN npm install # 5. Set a persistent environment variable ENV PORT=3000 # 6. Document which port the app listens on EXPOSE 3000 # 7. Default command when container starts CMD ["npm", "start"]
instruction reference
| Instruction | What it does |
|---|---|
| FROM | Sets the base image to start from |
| WORKDIR | Sets working directory inside container |
| COPY | Copies files from host into image |
| RUN | Executes a command at build time |
| ENV | Sets a persistent environment variable |
| EXPOSE | Documents the port the app listens on |
| CMD | Default startup command (overridable) |
| ENTRYPOINT | Always runs; docker run args are appended |
| ARG | Build-time variable only (not at runtime) |
| VOLUME | Declares a mount point for persistent data |
🌐
Networking
6 commandsCreate a Network
createMake a custom isolated network so containers can reach each other by name instead of hardcoded IP addresses.
docker network create <network-name>
# explicitly set driver (bridge is default)
docker network create -d bridge <network-name>
List & Inspect Networks
listSee all networks. Inspect one to view connected containers and their IP addresses.
docker network ls
# see connected containers + IPs
docker network inspect <network-name>
Connect Container to Network
connectAssign a container to a network at run time, or attach/detach an already-running container.
# assign at run time
docker run --network <network-name> <image>
# connect an already-running container
docker network connect <network-name> <container>
# disconnect a container
docker network disconnect <network-name> <container>
Container-to-Container DNS
DNSOn the same custom network, use the container name as the hostname — Docker handles DNS automatically, no IPs needed.
# reach another container by its name
Host: <container-name>
# reach the host machine from inside a container
Host: host.docker.internal
Remove a Network
deleteDelete a network. All containers must be disconnected first, or use prune to clean up all unused ones.
docker network rm <network-name>
# remove all unused networks at once
docker network prune
Network Driver Types
referenceDocker supports different network modes.
bridge is the default for single-host setups.bridge → isolated, containers on same host (default)
host → shares the host's network stack directly
none → no networking at all
overlay → multi-host networking (Docker Swarm)
💾
Volumes & Mount Points
5 commandsBind Mount (Host Folder)
mountSync a folder from your machine into the container. Changes on either side reflect immediately — ideal for development.
# map host path into container
docker run -v <host-path>:<container-path> <image>
# use current directory as source
docker run -v $(pwd):<container-path> <image>
Named Volume (Docker Managed)
volumeDocker manages storage for you — better for databases. Data survives container deletion. No need to pick a folder location.
# create a named volume
docker volume create <volume-name>
# use it when running a container
docker run -v <volume-name>:<container-path> <image>
List & Inspect Volumes
listSee all volumes and find out exactly where Docker is storing data on your machine.
docker volume ls
docker volume inspect <volume-name>
Remove Volumes
deleteDelete a volume to free up space. This permanently removes all data inside it — no undo.
docker volume rm <volume-name>
# remove all unused volumes
docker volume prune
.dockerignore File
configLike
.gitignore — excludes files from the build context. Prevents large or sensitive files from ending up in your image.# place .dockerignore in project root
node_modules
.git
.env
*.log
Dockerfile
🧩
Docker Compose
commands + yamldocker-compose.yml — generalized with variables
# docker-compose.yml
services:
<db-service>: # e.g. mysqldb
image: <db-image>:<tag> # e.g. mysql:latest
container_name: <db-container>
environment:
- <ENV_VAR_1>=<value>
- <ENV_VAR_2>=<value>
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-h", "localhost"]
interval: <interval> # e.g. 10s
timeout: <timeout> # e.g. 20s
retries: <retries> # e.g. 10
networks:
- <network-name>
<app-service>: # e.g. app
build: . # path to Dockerfile
container_name: <app-container>
ports:
- "<host-port>:<container-port>"
environment:
- <ENV_VAR>=<value>
depends_on:
<db-service>:
condition: service_healthy # wait for DB healthcheck
volumes:
- <host-path>:<container-path>
networks:
- <network-name>
networks:
<network-name>: # defines the shared network
volumes:
<volume-name>: # optional named volume
compose commands
| Command | What it does |
|---|---|
| docker compose up | Start all services (foreground) |
| docker compose up -d | Start all services in background |
| docker compose down | Stop & remove all containers |
| docker compose down -v | Also delete associated volumes |
| docker compose build | Rebuild custom images |
| docker compose logs -f | Stream live logs from all services |
| docker compose ps | List running compose services |
| docker compose run <service> | Start a single service only |
| docker compose exec <service> bash | Open shell in a service container |
| docker compose restart <service> | Restart a specific service |
🧹
System & Cleanup
5 commandsFull System Cleanup
cleanupRemoves all stopped containers, unused networks, dangling images, and build cache in one shot.
# safe cleanup (keeps images still in use)
docker system prune
# aggressive: also removes all unused images
docker system prune -a
Check Disk Usage
infoShows how much disk space Docker is consuming — broken down by images, containers, and volumes.
docker system df
Bulk Stop & Remove Containers
one-liner$(docker ps -q) outputs all running container IDs, which are passed directly to stop or rm.# stop all running containers
docker stop $(docker ps -q)
# remove all stopped containers
docker rm $(docker ps -aq)
Remove All Images
cleanupRemoves every image on your machine — clean slate. You'll need to re-pull everything afterward.
docker rmi $(docker images -q)
Version & System Info
infoCheck your Docker version and get a full system overview including daemon status and resource limits.
docker version
docker info
No commands found. Try a different keyword.