Docker Basic Commands
This guide covers 80% of daily Docker workflows. For advanced use cases (e.g., Docker Compose, Swarm), refer to the official Docker documentation
Managing Images
List Downloaded Images
docker imagesDisplays all locally stored images with details like size and tags.
Remove an Image
docker image rm <image_name/id> -f-f: Force deletion (even if the image is in use).Remove all images:
docker rmi $(docker images -q)
Build an Image from a Dockerfile
docker build -t <image_name>:<image_version> <path_to_dockerfile>Example:
docker build -t myapp:v1.3.17 .
Managing Containers
List Containers
Running containers:
docker psAll containers (running/stopped):
docker ps -a
Run a Container
docker run [options] <image>Common flags:
-d # Run in detached mode -p 8080:80 # Map host port to container port -v /host/path:/container/path # Mount a volume -e ENV_VAR=value # Set environment variable --name <container_name> # Assign a custom name
Lifecycle Management
Start a stopped container:
docker start <container>Stop a running container:
docker stop <container>Restart a container:
docker restart <container>Delete a container:
docker rm <container>Remove all stopped containers:
docker container prune
Interact with Containers
View logs:
docker logs <container>Access a running container (interactive shell):
docker exec -it <container> /bin/bashMonitor resource usage:
docker stats
Advanced Operations
Volume Management
List volumes:
docker volume lsCreate a volume:
docker volume create <volume_name>
Network Management
List networks:
docker network lsConnect a container to a network:
docker network connect <network> <container>
Inspect Details
View detailed metadata:
docker inspect <container/image>
Pro Tips
Clean Unused Resources
docker system prune -aRemoves all stopped containers, unused images, and networks.
Real-Time Log Streaming
docker logs -f <container>-f: Follow logs in real-time (similar totail -f).
Copy Files Between Host and Container
docker cp <container>:<path> <host_path> # Copy from container docker cp <host_path> <container>:<path> # Copy to containerSave/Load Images as Files
Export an image:
docker save -o <file.tar> <image>Import an image:
docker load -i <file.tar>