v27.x Official Docs ↗
// quick reference guide

Docker Commands
at a Glance

Every essential Docker command with plain-English explanations and real examples. Variables shown as <angle-brackets>. Each line has its own copy button.

Pull an Image
fetch
Download 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
docs.docker.com → docker pull
List Images
list
See all images stored locally — name, tag, size, and creation date.
docker images
# alternative (same result)
docker image ls
docs.docker.com → docker images
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> .
docs.docker.com → docker build
Remove an Image
delete
Delete 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>
docs.docker.com → docker rmi
Search Docker Hub
search
Find available public images on Docker Hub without opening a browser.
docker search <term>
# limit to top 5 results
docker search <term> --limit 5
docs.docker.com → docker search
Tag & Push to Registry
publish
Label 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>
docs.docker.com → docker push
Run a Container
run
The 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>
docs.docker.com → docker run
List Containers
list
See what's running. Add -a to also show stopped containers.
# only running containers
docker ps
# all containers (including stopped)
docker ps -a
docs.docker.com → docker ps
Start / Stop / Restart
control
Control the lifecycle of an existing container using its name or ID.
docker start <container>
docker stop <container>
docker restart <container>
docs.docker.com → docker start
Run Command Inside Container
exec
Jump 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>
docs.docker.com → docker exec
View Logs
debug
See 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>
docs.docker.com → docker logs
Inspect a Container
debug
Get 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}}'
docs.docker.com → docker inspect
Remove a Container
delete
Delete 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>
docs.docker.com → docker rm
Monitor Resource Usage
monitor
Live dashboard showing CPU, memory, and network usage — like Task Manager but for containers.
# all running containers
docker stats
# single container only
docker stats <container>
docs.docker.com → docker stats
Port Mapping
network
Connect 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>
docs.docker.com → port mapping
Copy Files
files
Transfer 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>
docs.docker.com → docker cp

Dockerfile — 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

InstructionWhat it does
FROMSets the base image to start from
WORKDIRSets working directory inside container
COPYCopies files from host into image
RUNExecutes a command at build time
ENVSets a persistent environment variable
EXPOSEDocuments the port the app listens on
CMDDefault startup command (overridable)
ENTRYPOINTAlways runs; docker run args are appended
ARGBuild-time variable only (not at runtime)
VOLUMEDeclares a mount point for persistent data
Create a Network
create
Make 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>
docs.docker.com → docker network create
List & Inspect Networks
list
See all networks. Inspect one to view connected containers and their IP addresses.
docker network ls
# see connected containers + IPs
docker network inspect <network-name>
docs.docker.com → docker network ls
Connect Container to Network
connect
Assign 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>
docs.docker.com → docker network connect
Container-to-Container DNS
DNS
On 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
docs.docker.com → networking tutorial
Remove a Network
delete
Delete 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
docs.docker.com → docker network rm
Network Driver Types
reference
Docker 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)
docs.docker.com → network drivers
Bind Mount (Host Folder)
mount
Sync 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>
docs.docker.com → bind mounts
Named Volume (Docker Managed)
volume
Docker 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>
docs.docker.com → volumes
List & Inspect Volumes
list
See all volumes and find out exactly where Docker is storing data on your machine.
docker volume ls
docker volume inspect <volume-name>
docs.docker.com → docker volume ls
Remove Volumes
delete
Delete 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
docs.docker.com → docker volume rm
.dockerignore File
config
Like .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
docs.docker.com → .dockerignore

docker-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

CommandWhat it does
docker compose upStart all services (foreground)
docker compose up -dStart all services in background
docker compose downStop & remove all containers
docker compose down -vAlso delete associated volumes
docker compose buildRebuild custom images
docker compose logs -fStream live logs from all services
docker compose psList running compose services
docker compose run <service>Start a single service only
docker compose exec <service> bashOpen shell in a service container
docker compose restart <service>Restart a specific service
Full System Cleanup
cleanup
Removes 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
docs.docker.com → docker system prune
Check Disk Usage
info
Shows how much disk space Docker is consuming — broken down by images, containers, and volumes.
docker system df
docs.docker.com → 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)
docs.docker.com → docker stop
Remove All Images
cleanup
Removes every image on your machine — clean slate. You'll need to re-pull everything afterward.
docker rmi $(docker images -q)
docs.docker.com → docker rmi
Version & System Info
info
Check your Docker version and get a full system overview including daemon status and resource limits.
docker version
docker info
docs.docker.com → docker version
No commands found. Try a different keyword.