// This guide covers 80% of daily Docker workflows. For advanced use cases (e.g., Docker Compose, Swarm), refer to the official Docker documentation
List Downloaded Images
docker images
Displays 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 .
List Containers
Running containers:
docker ps
All 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/bash
Monitor resource usage:
docker stats
Volume Management
List volumes:
docker volume ls
Create a volume:
docker volume create <volume_name>
Network Management
List networks:
docker network ls
Connect a container to a network:
docker network connect <network> <container>
Inspect Details
View detailed metadata:
docker inspect <container/image>
Clean Unused Resources
docker system prune -a
Removes all stopped containers, unused images, and networks.
Real-Time Log Streaming
docker logs -f <container>
-f
: Follow logs in real-time (similar to tail -f
).
Copy Files Between Host and Container
docker cp <container>:<path> <host_path> # Copy from container
docker cp <host_path> <container>:<path> # Copy to container
Save/Load Images as Files
Export an image:
docker save -o <file.tar> <image>
Import an image:
docker load -i <file.tar>