Bytes
Web Development

Docker Cheat Sheet (Basics to Advanced Docker Cheat Sheet)

Last Updated: 1st November, 2024
icon

Jay Abhani

Senior Web Development Instructor at almaBetter

Master essential Docker commands with this Docker cheat sheet covering Docker CLI, Compose, Swarm, Kubernetes, useful flags for streamlined container management

Docker has revolutionized the way developers build, ship, and run applications by packaging them into lightweight, portable containers. This Docker Cheat Sheet serves as a comprehensive guide to help you understand Docker’s core commands and functionalities, covering essential Docker CLI commands, Docker Compose, Docker Swarm, Kubernetes integration, and useful flags for more efficient container management.

Introduction to Docker

Docker enables developers to create applications that are isolated from the underlying infrastructure, promoting easier deployment and scaling. Docker containers bundle an application’s code, dependencies, and configurations into a single package, ensuring consistent behavior across different environments. Docker uses a client-server model where the Docker CLI (client) communicates with the Docker Daemon (server), which does the actual work of creating, running, and managing containers.

Docker Commands Cheat Sheet

The Docker CLI provides commands to work with containers, images, networks, and volumes. Here’s a breakdown of some key commands and what they do:

Basic Docker Commands

  • docker version - Displays the current Docker version and client/server version info.
  • docker info - Displays detailed information about the Docker setup, including number of containers, images, and network details.
  • docker help - Displays a list of Docker commands or detailed information about a specific command.

Working with Containers

Containers are the heart of Docker. Here’s how to manage them effectively:

  • docker run [options] IMAGE [COMMAND] - Creates a new container and starts it. Some useful options include:
    • -d - Runs the container in detached mode (background).
    • -p - Maps host and container ports (-p [HOST_PORT]:[CONTAINER_PORT]).
    • --name [NAME] - Assigns a custom name to the container.

Example:

docker run -d -p 8080:80 --name my-nginx nginx
  • docker ps - Lists all running containers.
    • -a - Shows all containers, even stopped ones.
  • docker stop [CONTAINER_ID or NAME] - Stops a running container by ID or name.
  • docker rm [CONTAINER_ID or NAME] - Removes a container. Use docker rm -f [CONTAINER_ID] to force-remove a running container.
  • docker exec -it [CONTAINER_ID] [COMMAND] - Executes a command in a running container (useful for shell access).
    • Example: docker exec -it my-nginx bash opens a shell in the container.

Working with Images

Images are the foundation of containers; they are read-only templates that define how a container should behave.

  • docker images - Lists all images currently on the system.
  • docker pull [IMAGE_NAME] - Downloads an image from Docker Hub.
  • docker build -t [IMAGE_NAME] [PATH_TO_DOCKERFILE] - Builds an image from a Dockerfile.
  • docker rmi [IMAGE_ID] - Removes an image by ID or name.
  • docker tag [IMAGE_NAME] [NEW_TAG] - Tags an image, which helps in versioning and pushing to registries.

Networking

Docker networks allow containers to communicate with each other.

  • docker network create [NETWORK_NAME] - Creates a custom bridge network.
  • docker network ls - Lists all Docker networks.

Volumes

Volumes persist data across container restarts and reboots.

  • docker volume create [VOLUME_NAME] - Creates a new volume.
  • docker volume ls - Lists all volumes.
  • docker volume rm [VOLUME_NAME] - Removes a specific volume.

Docker Flags Cheat Sheet

Flags are options that you can pass to Docker commands to customize their behavior. Here are some commonly used Docker flags:

  • -d - Run in detached mode (background).
  • -it - Interactive terminal for running containers with a TTY.
  • -p [HOST_PORT]:[CONTAINER_PORT] - Maps container ports to host ports.
  • -v [HOST_PATH]:[CONTAINER_PATH] - Mounts volumes for persistent storage.
  • --rm - Automatically removes the container after it stops.
  • -e [ENV_VAR]=[VALUE] - Sets environment variables in a container.

Docker Compose Cheat Sheet

Docker Compose simplifies multi-container applications by defining services in a single YAML file (docker-compose.yml).

Basic Docker Compose Commands

  • docker-compose up - Creates and starts all services defined in docker-compose.yml.
    • -d - Runs services in the background.
    • Example: docker-compose up -d
  • docker-compose down - Stops and removes services, networks, and volumes created by Compose.
  • docker-compose ps - Lists all services defined in the Compose file.
  • docker-compose build - Builds or rebuilds images defined in the Compose file.
  • docker-compose logs - Shows logs for all running services.

Example Docker Compose File

A sample file to start a web server and database with linked services:

version: '3'
services:
  web:
    image: nginx
    ports:
      - "80:80"
  db:
    image: mysql
    environment:
      MYSQL_ROOT_PASSWORD: password

Docker Swarm Cheat Sheet

Docker Swarm provides a native clustering and orchestration solution, allowing you to deploy containers across multiple Docker hosts.

Setting up Swarm

  • docker swarm init - Initializes the current Docker host as the Swarm manager.
  • docker swarm join --token [TOKEN] [IP]:[PORT] - Joins a worker node to a Swarm.
  • docker swarm leave - Makes a node leave the Swarm.

Swarm Service Management

  • docker service create --name [SERVICE_NAME] [IMAGE] - Creates a new service in Swarm.
    • Example: docker service create --name nginx-web -p 80:80 nginx
  • docker service ls - Lists all services in the Swarm.
  • docker service scale [SERVICE_NAME]=[NUMBER_OF_REPLICAS] - Scales a service up or down.

Docker CLI Cheat Sheet

The Docker CLI offers various commands to interact with Docker efficiently. Here are some of the core CLI commands:

  • docker run - Creates and runs a container.
  • docker exec - Executes a command in a running container.
  • docker logs - Fetches logs of a container.
  • docker ps - Lists all running containers.
  • docker pull - Downloads an image from Docker Hub.
  • docker build - Builds an image from a Dockerfile.

Docker and Kubernetes Cheat Sheet

Kubernetes is an orchestration platform designed for managing containerized applications across clusters of machines. Docker can be used alongside Kubernetes for container management. Here’s how you can interact with Kubernetes using Docker containers:

Kubernetes Commands for Docker Users

  • kubectl create -f [FILENAME] - Deploys resources defined in a YAML file.
  • kubectl get pods - Lists all running pods in the cluster.
  • kubectl logs [POD_NAME] - Fetches logs from a pod.
  • kubectl delete -f [FILENAME] - Deletes resources defined in a YAML file.

Docker Compose vs. Kubernetes

  • Docker Compose is suited for simple applications with a few containers, ideal for development and testing.
  • Kubernetes is built for large-scale production applications, providing features like load balancing, auto-scaling, and service discovery.

Example Workflow: Deploying with Docker Compose

Let’s say you’re deploying a web application with a database. Here’s how to define services in docker-compose.yml and deploy them with Docker Compose:

version: '3'
services:
  app:
    image: node:14
    environment:
      - MONGO_URL=mongodb://db:27017/mydatabase
    ports:
      - "3000:3000"
    depends_on:
      - db
  db:
    image: mongo
    ports:
      - "27017:27017"

To deploy, simply run:

docker-compose up -d

This command launches both the app and db services as defined in the file.

Conclusion

This Docker Cheat Sheet covers everything you need to work with Docker containers, networks, and storage. From simple Docker commands to complex orchestration using Docker Swarm and Kubernetes, this guide serves as a quick reference for Docker users at any level. Happy Dockering!

More Cheat Sheets and Top Picks

  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2024 AlmaBetter