Docker One Shot

1. Docker Basics
These commands help you set up Docker and manage basic container operations.
1.1 Docker Version & System Info
docker --version # Check Docker version
docker info # Display system-wide information about Docker
1.2 Working with Docker Images
docker search <image_name> # Search for an image on Docker Hub
docker pull <image_name> # Download an image from Docker Hub
docker images # List all downloaded images
docker rmi <image_id> # Remove an image
docker rmi $(docker images -q) # Remove all images
1.3 Working with Docker Containers
docker run <image_name> # Run a container from an image
docker run -d <image_name> # Run in detached mode (background)
docker run -it <image_name> /bin/bash # Start container with interactive shell
docker ps # List running containers
docker ps -a # List all containers (running + stopped)
docker stop <container_id> # Stop a running container
docker start <container_id> # Start a stopped container
docker restart <container_id> # Restart a container
docker kill <container_id> # Force stop a running container
docker rm <container_id> # Remove a stopped container
docker rm $(docker ps -aq) # Remove all containers
1.4 Container Logs & Inspection
docker logs <container_id> # View logs of a container
docker inspect <container_id> # Inspect details of a container
docker stats # View real-time stats of running containers
docker top <container_id> # Show running processes inside a container
docker events # Get real-time events from the Docker server
2. Docker Images & Containers Management
2.1 Committing & Tagging Images
docker commit <container_id> <new_image_name> # Save container changes as an image
docker tag <image_id> <repository>:<tag> # Tag an image
2.2 Building Images
docker build -t <image_name> . # Build an image from a Dockerfile
docker build -f <Dockerfile> -t <image_name> . # Specify a Dockerfile name
2.3 Working with Volumes
docker volume create <volume_name> # Create a volume
docker volume ls # List all volumes
docker volume rm <volume_name> # Remove a volume
docker run -v <volume_name>:/data <image> # Mount a volume inside a container
docker inspect <container_id> # Check volume mounts
3. Networking in Docker
docker network ls # List available networks
docker network create <network_name> # Create a new network
docker network inspect <network_name> # Inspect network details
docker network connect <network_name> <container_id> # Connect a container to a network
docker network disconnect <network_name> <container_id> # Disconnect a container from a network
docker network rm <network_name> # Remove a network
Running Containers with Custom Networks
docker run --network=<network_name> <image> # Run container in a specific network
4. Docker Compose (Multi-Container Applications)
docker-compose up # Start services defined in docker-compose.yml
docker-compose up -d # Start services in detached mode
docker-compose down # Stop and remove all containers from the compose file
docker-compose ps # List running services
docker-compose logs # View logs of all services
5. Docker Security
docker scan <image> # Scan image for vulnerabilities
docker history <image> # View image layers and history
docker diff <container_id> # Show changes made to container files
6. Docker Registry (Pushing & Pulling)
docker login # Login to Docker Hub
docker tag <image_id> username/repo # Tag an image before pushing
docker push username/repo # Push image to Docker Hub
docker pull username/repo # Pull an image from Docker Hub
7. Docker Swarm (Orchestration)
docker swarm init # Initialize swarm mode
docker swarm join --token <token> # Join a node to swarm
docker service create --name my_service <image> # Deploy a service
docker service ls # List services in swarm
docker service rm <service_name> # Remove a service
docker node ls # List nodes in swarm
docker node rm <node_id> # Remove a node from swarm
8. Dockerfile Commands (Building Custom Images)
Basic Dockerfile Structure
FROM ubuntu:latest # Base image
MAINTAINER John Doe # Maintainer info
RUN apt update && apt install -y nginx # Install packages
COPY . /app # Copy files into the container
WORKDIR /app # Set working directory
CMD ["nginx", "-g", "daemon off;"] # Default command
Building & Running a Dockerfile
docker build -t my_custom_image .
docker run -d my_custom_image
9. Docker System Cleanup & Debugging
docker system df # Show disk usage by Docker
docker system prune # Remove unused images, containers, networks, and volumes
docker system prune -a # Remove all unused data
docker logs <container_id> # Check container logs for debugging
Important: One-Liners for Quick Cleanup
docker stop $(docker ps -aq) # Stop all running containers
docker rm $(docker ps -aq) # Remove all containers
docker rmi $(docker images -q) # Remove all images
docker volume rm $(docker volume ls -q) # Remove all volumes
docker system prune -a # Remove everything


