Docker Cheat Sheet — Essential Commands for Daily Use


💡 Introduction

Docker simplifies containerized application development, deployment, and management. This cheat sheet covers essential commands that developers use daily for efficient workflow management.


💡 Docker Basics

# Check installed Docker version
docker --version

# Display system-wide information
docker info

# List all available commands
docker --help

💡 Working with Images

# Pull an image from Docker Hub
docker pull ubuntu:latest

# List all downloaded images
docker images

# Remove an image
docker rmi image_name_or_id

# Build an image from a Dockerfile
docker build -t my-app .

# Tag an image for a repository
docker tag my-app myrepo/my-app:v1.0

# Push an image to Docker Hub
docker push myrepo/my-app:v1.0

💡 Managing Containers

# Run a container in interactive mode
docker run -it ubuntu /bin/bash

# Run a container in detached mode (-d)
docker run -d --name my-container nginx

# List running containers
docker ps

# List all containers (including stopped ones)
docker ps -a

# Stop a running container
docker stop container_id_or_name

# Restart a container
docker restart container_id_or_name

# Remove a stopped container
docker rm container_id_or_name

💡 Accessing Containers

# Attach to a running container
docker attach container_id_or_name

# Execute a command inside a running container
docker exec -it container_id_or_name /bin/bash

# Copy files from container to local machine
docker cp container_id_or_name:/path/to/file ./

💡 Networking

# List networks
docker network ls

# Create a new network
docker network create my-network

# Connect a container to a network
docker network connect my-network container_id_or_name

# Disconnect a container from a network
docker network disconnect my-network container_id_or_name

💡 Docker Compose

# Start all services defined in docker-compose.yml
docker-compose up -d

# Stop all services
docker-compose down

# Restart services
docker-compose restart

# View service logs
docker-compose logs -f

💡 Cleanup Commands

# Remove all stopped containers
docker container prune

# Remove all unused images
docker image prune

# Remove all unused networks
docker network prune

# Remove all unused data (volumes, images, containers, networks)
docker system prune -a

💡 Performance Tips

  • Use docker ps --format "{{.ID}} {{.Names}}" for cleaner output.
  • Run containers with --rm flag to remove them after stopping.
  • Use docker logs -f container_id_or_name to track live logs.

💡 Conclusion

This cheat sheet provides a quick reference for daily Docker commands. Bookmark it and share with your team!

Stay tuned for more at ZeroToJavaPro.com!