Skip to content

Docker

Basics

Check Docker Version

docker --version
docker version

Show System-wide Docker Information

docker info

Uninstall Old Versions

sudo apt remove docker docker-engine docker.io containerd runc

Install Docker on Linux (Debian/Ubuntu)

sudo apt update
sudo apt install -y docker.io

Start Docker Service

sudo systemctl start docker
sudo systemctl enable docker

Docker Image Management

docker search <image_name>             # Search for images on Docker Hub
docker pull <image_name>:<tag>         # Pull an image from a registry
docker images                          # List all locally downloaded images
docker tag <src>:<tag> <dest>:<tag>    # Create a new tag (alias) for an image
docker inspect <image_id_or_name>      # Inspect metadata (JSON format)
docker history <image_id_or_name>      # View the history/layers of an image
docker rmi <image_id_1> <image_id_2>   # Remove one or more specific images
docker image prune                     # Remove all unused (dangling) images
docker image prune -a                  # Remove all images not used by any container

Docker Container Management

docker run <image_name>                      # Create and run a container from an image
docker run -it <image_name> /bin/bash        # Run interactively with a terminal
docker run -d <image_name>                   # Run in detached (background) mode
docker run --name <name> <image_name>        # Run and assign a custom name
docker run --rm <image_name>                 # Run and automatically remove after exit
docker ps                                    # List all currently running containers
docker ps -a                                 # List all containers (running + stopped)
docker start <id_or_name>                    # Start a stopped container
docker stop <id_or_name>                     # Gracefully stop a running container
docker restart <id_or_name>                  # Restart a container
docker kill <id_or_name>                     # Forcefully kill a running container
docker pause <id_or_name>                    # Pause all processes in a container
docker unpause <id_or_name>                  # Resume a paused container
docker rename <old> <new>                    # Rename an existing container
docker rm <id_or_name>                       # Remove a stopped container
docker rm -f <id_or_name>                    # Force remove a running container
docker logs <id_or_name>                     # View container output logs
docker logs -f <id_or_name>                  # Stream/follow container logs in real-time
docker top <id_or_name>                      # Display running processes of a container
docker inspect <id> | grep -i limits         # View resource limits for a container

Docker Networking Management

docker network ls                            # List all available Docker networks
docker network create <network_name>         # Create a new custom network
docker network connect <net> <container>     # Attach a running container to a network
docker network disconnect <net> <container>  # Detach a container from a network
docker network inspect <network_name>        # View detailed configuration and connected containers
docker network rm <network_name>             # Remove one or more networks
docker network prune                         # Remove all unused networks

Docker Volume Management

docker volume ls                             # List all available Docker volumes
docker volume create <volume_name>           # Create a new named volume
docker run -v <vol>:<path> <image>           # Map a volume to a container path
docker run -v my_data:/data nginx:latest     # Example: Map 'my_data' volume to '/data'
docker volume rm <volume_name>               # Remove a specific volume (must be unused)
docker volume inspect <volume_name>          # View volume metadata (mount point, driver)
docker volume ls -f dangling=true            # List volumes not attached to any container
docker volume prune                          # Remove all unused (dangling) volumes

Docker Build Management

docker build -t <name>:<tag> .               # Build an image from Dockerfile in current dir
docker build -t <name>:<tag> <path>          # Build an image from a specific directory path
docker build -f <filename> -t <name> .       # Build using a specific filename (not 'Dockerfile')
docker build --no-cache -t <name> .          # Rebuild image from scratch without using cache
docker build --pull -t <name> .              # Always attempt to pull a newer version of the base image
docker build --build-arg <key>=<val> .       # Pass variables to the Dockerfile at build-time
docker build --target <stage> -t <name> .    # Build a specific stage in a multi-stage Dockerfile
docker build --quiet -t <name> .             # Suppress build output and print image ID only
docker build --squash -t <name> .            # Squash newly created layers into a single new layer

Docker Compose Management

docker-compose up                            # Create and start containers defined in YAML
docker-compose up -d                         # Run containers in the background (detached)
docker-compose up --build                    # Rebuild images before starting containers
docker-compose down                          # Stop and remove containers, networks, and images
docker-compose down --volumes                # Stop and remove containers AND named volumes
docker-compose start                         # Start existing containers for a service
docker-compose stop                          # Stop running containers without removing them
docker-compose pause                         # Pause running containers of a service
docker-compose unpause                       # Unpause containers of a service
docker-compose ps                            # List containers related to the current YAML file
docker-compose logs                          # View output logs from all services
docker-compose logs -f                       # Stream/follow logs from all services in real-time
docker-compose build                         # Build or rebuild services defined in the file
docker-compose build --no-cache              # Rebuild services from scratch without using cache
docker-compose pull                          # Pull images for services defined in the file
docker-compose exec <service> <command>      # Execute a command in a running service container
docker-compose run <service> <command>       # Run a one-off command for a service
docker-compose config                        # Validate and view the Compose file configuration
docker-compose restart                       # Restart all service containers

Docker System Cleanup

docker container prune                       # Remove all stopped containers
docker image prune                           # Remove all unused (dangling) images
docker image prune -a                        # Remove all images not used by any container
docker volume prune                          # Remove all unused volumes (not used by any container)
docker network prune                         # Remove all unused networks
docker system prune                          # Remove all stopped containers, unused networks, and dangling images
docker system prune -a                       # Remove all unused images, not just dangling ones
docker system prune --volumes                # Remove everything above PLUS all unused volumes
docker system df                             # Show Docker disk usage and reclaimable space

Advanced & Debugging Operations

docker exec -it <id_or_name> /bin/bash      # Open an interactive shell inside a running container
docker save -o <path>.tar <image>:<tag>      # Export an image to a tar archive
docker load -i <path>.tar                    # Load an image from a tar archive
docker export <id_or_name> > <path>.tar      # Export container's filesystem as a tar archive
docker import <path>.tar <image_name>        # Create an image from a container tarball
docker inspect <id_or_name>                  # Return low-level information on Docker objects
docker stats                                 # Display a live stream of container resource usage statistics
docker info                                  # Display system-wide information/daemon status

Docker Registry & Distribution

docker login                                 # Log in to a Docker registry (defaults to Docker Hub)
docker logout                                # Log out from a Docker registry
docker tag <id> <repo>/<name>:<tag>          # Tag an image into a specific repository
docker push <repo>/<name>:<tag>              # Upload an image to a registry
docker pull <repo>/<name>:<tag>              # Download an image from a specific registry

Docker Swarm (Orchestration)

docker swarm init                            # Initialize a swarm (current node becomes manager)
docker swarm join --token <tkn> <ip>:<port>  # Join a node to the swarm as a worker
docker node ls                               # List nodes in the swarm (Manager only)
docker stack deploy -c <file>.yml <name>     # Deploy or update a stack from a Compose file
docker stack services <name>                 # List services running in the stack
docker stack rm <name>                       # Remove a stack and its services

Resource Constraints & Security

docker run --cpus="1.0" <image>              # Limit container to 1.0 CPU cores
docker run -m 512m <image>                   # Limit container memory to 512MB
docker run --user "$(id -u):$(id -g)" <img|  # Run container with specific UID/GID
dockerd-rootless-setuptool.sh install        # Set up Docker to run without root privileges

Kubernetes Integration (Tools)

docker info | grep -i kubernetes             # Check if K8s is enabled in Docker Desktop
kompose convert                              # Convert Docker Compose file to K8s manifests
kompose up                                   # Deploy Compose-defined services directly to K8s