The Complete Docker Cheat Sheet: A Handy Reference for Docker Images, Compose, Volumes, and More

Welcome to our comprehensive Docker cheat sheet! In this article, we have compiled a comprehensive list of essential Docker commands that every developer and DevOps enthusiast should know. Whether you are new to Docker or an experienced user, this cheat sheet will serve as your go-to reference for managing Docker images, working with Docker Compose, utilizing volumes, harnessing the power of Docker Swarm, managing Docker registries, building images, executing commands inside containers, performing clean-up operations, and configuring networking.

Docker Basics

docker pull <image>download a Docker image from a repository
docker imageslist all Docker images on your system
docker pslist all running Docker containers
docker run <image>start a new Docker container from an image
docker stop <container>stop a running Docker container
docker rm <container>remove a stopped Docker container
docker rmi <image>remove a Docker image from your system

Dockerfile

  • FROM: specify the base image to use in a Dockerfile

  • RUN: run a command during the build process of a Docker image

  • CMD: specify the default command to run when a container is started

  • EXPOSE: specify the port on which a container listens

  • COPY: copy files or directories from the build context to the container

  • WORKDIR: set the working directory for the container

  • ENV: set an environment variable for the container

Docker Images

  • docker images: list all Docker images on your system

  • docker image inspect <image>: inspect the details of a Docker image

  • docker image history <image>: show the history of a Docker image

  • docker image rm <image>: remove a Docker image from your system

  • docker image tag <source-image> <new-image-name>: tag a Docker image with a new name and/or tag

Docker Compose

  • docker-compose up: start the services defined in a docker-compose.yml file

  • docker-compose down: stop the services defined in a docker-compose.yml file

  • docker-compose build: build the Docker images defined in a docker-compose.yml file

  • docker-compose ps: list the running containers for services defined in a docker-compose.yml file

Docker Volumes

  • docker volume create <name>: create a new Docker volume

  • docker volume ls: list all Docker volumes on your system

  • docker volume inspect <name>: inspect the details of a Docker volume

  • docker run -v <name>:<path>: attach a Docker volume to a container

  • docker run -v <host-path>:<container-path>: mount a host directory as a Docker volume

Docker Swarm

  • docker swarm init: initialize a new Docker Swarm cluster

  • docker swarm join: join a Docker Swarm cluster

  • docker node ls: list all nodes in a Docker Swarm cluster

  • docker service create: create a new Docker service

  • docker service ls: list all Docker services in a Swarm cluster

  • docker service ps <service>: list the tasks for a Docker service

  • docker service scale <service>=<num-replicas>: scale a Docker service to a specified number of replicas

Docker Registry

  • docker login: login to a Docker registry

  • docker logout: log out from a Docker registry

  • docker push <image>: push a Docker image to a registry

  • docker pull <image>: pull a Docker image from a registry

  • docker tag <source-image> <registry>/<repo>:<tag>: tag a Docker image for a registry

Docker Logs

  • docker logs <container>: display the logs of a running container

  • docker logs -f <container>: follow the logs of a running container in real-time

  • docker logs --tail <number> <container>: display the last n lines of a container's logs

Docker Exec

  • docker exec -it <container> <command>: run a command inside a running container

  • docker exec -it <container> sh: open a shell session inside a running container

Docker Build

  • docker build -t <image-name> .: build a Docker image from a Dockerfile in the current directory and tag it with the given name

Docker Clean-up

  • docker system prune: remove all stopped containers, unused networks, dangling images and caches

  • docker container prune: remove all stopped containers

  • docker image prune: remove all unused images

  • docker volume prune: remove all unused volumes

  • docker network prune: remove all unused networks

Networking

  • docker network create <name>: create a new Docker network

  • docker network ls: list all Docker networks on your system

  • docker network inspect <name>: inspect the details of a Docker network

  • docker run --network <name>: attach a container to a Docker network

  • docker run -p <host-port>:<container-port>: publish a container's port to the host

  • docker run --expose <port>: expose a port from the container to other linked containers

With step-by-step explanations and practical examples, this cheat sheet will help you navigate the vast Docker ecosystem with ease. Whether you're a developer, system administrator, or anyone working with Docker, this cheat sheet is a valuable resource that will boost your productivity and help you master the essential Docker commands.

So, dive in and explore the world of Docker with our comprehensive cheat sheet. From beginner to pro, this guide will equip you with the knowledge and commands you need to effectively work with Docker and streamline your containerization workflow.

Thank you for reading this blog. If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future.

— Happy Learning !!!

Let’s connect !!!

Linkedin

Github

Mail