Complete Guide to Docker Commands
Docker commands are the backbone of container management. This guide covers essential commands, their use cases, and best practices. Bookmark this as your Docker CLI cheat sheet!
1. Installation & Setup
- Install Docker Engine:
- Verify Installation:
docker --version # Check Docker version docker info # View system-wide Docker details
2. Core Docker Commands
Image Management
| Command | Description | Example |
|---|---|---|
docker pull |
Download an image from a registry | docker pull nginx:latest |
docker build |
Build an image from a Dockerfile | docker build -t my-app:v1 . |
docker images |
List all local images | docker images |
docker rmi |
Remove an image | docker rmi my-app:v1 |
docker tag |
Tag an image | docker tag my-app:v1 my-repo/my-app:prod |
docker push |
Push an image to a registry | docker push my-repo/my-app:prod |
Container Management
| Command | Description | Example |
|---|---|---|
docker run |
Start a new container | docker run -d -p 8080:80 --name web nginx |
docker ps |
List running containers | docker ps -a (show all containers) |
docker start/stop |
Start/stop a container | docker stop web |
docker restart |
Restart a container | docker restart web |
docker rm |
Remove a stopped container | docker rm web |
docker exec |
Run a command in a running container | docker exec -it web bash |
docker logs |
View container logs | docker logs -f web (follow logs) |
docker inspect |
View container details | docker inspect web |
docker stats |
Monitor resource usage | docker stats |
Flags Explained
-d: Run in detached mode (background).-p: Map ports (host:container).-v: Mount volumes (host_path:container_path).-it: Interactive terminal (e.g., forbashaccess).--name: Assign a name to the container.--env/-e: Set environment variables.
3. Dockerfile Basics
A Dockerfile defines how to build an image. Example:
# Use a base image
FROM python:3.9-slim
# Set working directory
WORKDIR /app
# Copy files
COPY requirements.txt .
COPY app.py .
# Install dependencies
RUN pip install -r requirements.txt
# Expose port
EXPOSE 5000
# Define startup command
CMD ["python", "app.py"]
Build the image:
docker build -t my-python-app:latest .
4. Docker Compose
Define multi-container apps in docker-compose.yml:
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "80:80"
volumes:
- ./html:/usr/share/nginx/html
db:
image: postgres:14
environment:
POSTGRES_PASSWORD: mysecret
Key commands:
docker-compose up -d # Start containers
docker-compose down # Stop and remove containers
docker-compose logs # View logs
5. Docker Networking
| Command | Description | Example |
|---|---|---|
docker network ls |
List networks | docker network ls |
docker network create |
Create a custom network | docker network create my-net |
docker network inspect |
Inspect a network | docker network inspect my-net |
docker run --network |
Attach a container to a network | docker run --network=my-net my-app |
6. Volumes & Persistence Data
| Command | Description | Example |
|---|---|---|
docker volume create |
Create a named volume | docker volume create my-vol |
docker run -v |
Mount a volume | docker run -v my-vol:/data my-app |
docker volume ls |
List volumes | docker volume ls |
docker volume inspect |
Inspect a volume | docker volume inspect my-vol |
7. Advanced Commands
Cleanup
docker system prune # Remove unused images, containers, networks
docker volume prune # Remove unused volumes
Swarm Mode (Orchestration)
docker swarm init # Initialize a Swarm cluster
docker service create # Deploy a service
docker stack deploy # Deploy a stack from a Compose file
Image Inspection
docker history my-image # Show image layer history
docker scan my-image # Scan for vulnerabilities
8. Common Use Cases
-
Run a Temporary Container
docker run --rm -it ubuntu:22.04 bashThe
--rmflag deletes the container after it stops. -
Persist Database Data
docker run -d --name mysql-db -v mysql_data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=secret mysql:8 -
Copy Files to/from a Container
docker cp app.log web:/app/logs # Host → Container docker cp web:/app/logs ./logs # Container → Host -
Debug a Container
docker exec -it web sh # Open a shell in the container docker logs web --tail 100 -f # Tail the last 100 logs
9. Best Practices
- Use
.dockerignore
Exclude unnecessary files (likenode_modules) during builds. - Minimize Image Layers
Combine multipleRUNcommands in the Dockerfile. - Avoid
latestTags in Production
Use specific versions (e.g.,nginx:1.23.3). - Run as Non-Root User
AddUSER appuserin your Dockerfile. - Leverage Multi-Stage Builds
Reduce image size by separating build and runtime stages.
10. Troubleshooting
- Container Not Starting?
docker logs <container_id> # Check logs for errors docker inspect <container_id> # Inspect configuration - Port Conflicts?
Ensure the host port isn’t already in use (netstat -tuln | grep 8080). - Out of Disk Space?
Clean up unused resources withdocker system prune -a.
Cheat Sheet Table
| Category | Command |
|---|---|
| Images | docker pull, docker build, docker images, docker rmi |
| Containers | docker run, docker ps, docker start/stop, docker exec, docker logs |
| Networking | docker network create, docker network ls |
| Volumes | docker volume create, docker run -v |
| Compose | docker-compose up, docker-compose down |