An Ultimate Docker and Docker-compose Cheat Sheet

An Ultimate Docker and Docker-compose Cheat Sheet

ยท

3 min read

We've navigated through containers, images, volumes, and networks, and we've gained invaluable insights into Docker and Docker Compose. ๐Ÿ™Œ Now, itโ€™s time to take our Docker skills to the next level.

Itโ€™s time to consolidate our learning into a comprehensive cheat-sheet. This cheat-sheet will serve as both a personal reference and a valuable resource for the DevOps community. It will include essential Docker and Docker Compose commands, along with brief explanations of their usage. This resource is not only about showcasing our knowledge but also about contributing to the wider community by providing a practical guide for others.


Docker Commands Cheat-Sheet

Docker General Commands

  • Display Docker version:
    docker --version

  • Display Docker info:
    docker info

  • Show system-wide information:
    docker system info

Docker Container Commands

  • List all containers (running and stopped):
    docker ps -a

  • List only running containers:
    docker ps

  • Start a container:
    docker start <container_id_or_name>

  • Stop a container:
    docker stop <container_id_or_name>

  • Restart a container:
    docker restart <container_id_or_name>

  • Pause a container:
    docker pause <container_id_or_name>

  • Unpause a container:
    docker unpause <container_id_or_name>

  • Remove a container:
    docker rm <container_id_or_name>

  • Remove all stopped containers:
    docker container prune

  • Inspect a container:
    docker inspect <container_id_or_name>

  • View logs of a container:
    docker logs <container_id_or_name>

  • Execute a command in a running container:
    docker exec -it <container_id_or_name> <command>

Docker Image Commands

  • List all images:
    docker images

  • Pull an image from a registry:
    docker pull <image_name>:<tag>

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

  • Tag an image:
    docker tag <image_id> <new_image_name>:<tag>

  • Remove an image:
    docker rmi <image_id_or_name>

  • Remove all unused images:
    docker image prune

  • Inspect an image:
    docker inspect <image_id_or_name>

Docker Volume Commands

  • List all volumes:
    docker volume ls

  • Create a volume:
    docker volume create <volume_name>

  • Remove a volume:
    docker volume rm <volume_name>

  • Inspect a volume:
    docker volume inspect <volume_name>

  • Remove all unused volumes:
    docker volume prune

Docker Network Commands

  • List all networks:
    docker network ls

  • Create a network:
    docker network create <network_name>

  • Remove a network:
    docker network rm <network_name>

  • Inspect a network:
    docker network inspect <network_name>

  • Remove all unused networks:
    docker network prune

Docker System Commands

  • Show system-wide information:
    docker system info

  • Remove unused data (containers, networks, images, volumes):
    docker system prune

  • Remove unused data with more aggressive pruning:
    docker system prune -a

Docker Compose Commands

  • Check Docker Compose version:
    docker-compose --version

  • Start services defined in docker-compose.yml:
    docker-compose up

  • Start services in detached mode:
    docker-compose up -d

  • Stop and remove containers, networks, volumes, and images:
    docker-compose down

  • Rebuild services:
    docker-compose build

  • View logs of services:
    docker-compose logs

  • Execute a command in a running service:
    docker-compose exec <service_name> <command>

  • Run a one-off command:
    docker-compose run <service_name> <command>

  • Remove stopped service containers:
    docker-compose down --volumes

Docker Registry Commands

  • Login to Docker Hub:
    docker login

  • Logout from Docker Hub:
    docker logout

  • Push an image to a registry:
    docker push <image_name>:<tag>


Why This Cheat-Sheet? ๐Ÿค”

Creating a cheat-sheet not only reinforces your own learning but also provides a valuable resource for others in the DevOps community. This cheat-sheet will help streamline your workflow, making Docker and Docker Compose easier to use and understand.

Happy Dockering! ๐Ÿš€

ย