🐳A Comprehensive Cheatsheet of Essential Docker Commands 🐳

-->Introduction:

Docker has become an indispensable tool for developers and system administrators alike, offering a streamlined approach to containerization and deployment. With its vast array of commands, Docker empowers users to efficiently manage containers, images, networks, and volumes. However, mastering these commands can be overwhelming, especially for beginners. Fear not! In this cheatsheet, we'll compile a comprehensive list of essential Docker commands to simplify your workflow and boost productivity.

  1. Working with Containers:

    • docker run <image>: Create and start a container based on the specified image.

    • docker ps: List all running containers.

    • docker ps -a: List all containers, including those that are stopped.

    • docker start <container>: Start a stopped container.

    • docker stop <container>: Stop a running container.

    • docker restart <container>: Restart a container.

    • docker rm <container>: Remove a stopped container.

    • docker exec -it <container> <command>: Execute a command within a running container.

  2. Managing Images:

    • docker images: List all locally available images.

    • docker pull <image>: Download an image from a registry.

    • docker rmi <image>: Remove a local image.

    • docker build -t <tag> <path_to_Dockerfile>: Build an image from a Dockerfile.

    • docker tag <image> <new_tag>: Tag an image for easier reference.

  3. Working with Volumes:

    • docker volume ls: List all Docker volumes.

    • docker volume create <volume_name>: Create a new volume.

    • docker volume rm <volume>: Remove a volume.

    • docker volume inspect <volume>: Display detailed information about a volume.

    • docker run -v <host_path>:<container_path>: Mount a host directory as a volume in the container.

  4. Networking:

    • docker network ls: List all Docker networks.

    • docker network create <network_name>: Create a new network.

    • docker network connect <network> <container>: Connect a container to a network.

    • docker network disconnect <network> <container>: Disconnect a container from a network.

  5. Docker Compose:

    • docker-compose up: Create and start containers defined in the docker-compose.yml.

    • docker-compose down: Stop and remove containers defined in the docker-compose.yml.

    • docker-compose build: Build or rebuild services defined in the docker-compose.yml.

    • docker-compose exec <service> <command>: Execute a command within a running service container.