Check Out Our Exclusive Docker Cheat Sheet!

Yana Khare 08 Aug, 2023 • 5 min read

Introduction

Docker is an open-source platform that simplifies the process of building, shipping, and running applications using containers. Containers allow developers to package applications and their dependencies, making them portable and consistent across different environments. This Docker cheat sheet provides a quick reference guide to essential commands and concepts for effectively working with Docker.

What is Docker?

Docker cheat sheet

Docker is a containerization platform enabling developers to create, deploy, and manage applications within isolated containers. These containers package the application code, runtime, libraries, and dependencies, ensuring consistency and efficiency across various systems.

Learn More: A Complete Guide on Docker for Beginners

Docker Architecture

Docker follows a client-server architecture:

  • Docker Client: The command-line tool that allows users to interact with the Docker daemon.
  • Docker Daemon: The background service responsible for building, running, and managing Docker containers.
  • Docker Images: Read-only templates used to create containers.
  • Docker Containers: Running instances of Docker images.
  • Docker Registry: A repository for storing and distributing Docker images.

Installation of Docker

Installation of Docker | Docker cheat sheet

To install Docker on your system, follow the appropriate instructions for your operating system. Below are the steps for Windows and MacOS.

Windows

Check system requirements

Windows 10 64-bit: Pro, Enterprise, or Education editions

Hardware Virtualization enabled in BIOS/UEFI (Intel VT-x/AMD-V)

Download Docker Desktop

Go to Docker’s official website and download the Docker Desktop installer for Windows.

Install Docker Desktop

Run the installer you downloaded and follow the installation wizard. It will guide you through the process.

Start Docker

Once installed, Docker Desktop should start automatically. You’ll see the Docker icon in the system tray when it’s running.

MacOS

Check system requirements

macOS Sierra 10.12 or newer

macOS must be a 2010 or newer model, with Intel’s hardware support for Memory Management Unit (MMU) virtualization.

Download Docker Desktop for Mac

Visit Docker’s official website and download the Docker Desktop installer for macOS.

Install Docker Desktop

Open the downloaded .dmg file, and drag the Docker icon into the Applications folder.

Start Docker

Open Docker from the Applications folder. It will appear in the menu bar, indicating that it’s running.

Docker Registry and Repository

Docker Registry is a service that stores and manages Docker images. It acts as a central repository where users can store, share, and pull Docker images. Docker Hub is a public Docker Registry, while private registries can be set up for secure internal image storage. 

A Docker Repository is a collection of related images with the same name, differentiated by tags representing different versions or configurations. Users can push images to a repository in a registry, and others can pull those images to run containers. Registries and repositories are crucial in simplifying image distribution and facilitating collaborative development in the Docker ecosystem.

Below are the docker cheat sheet commands for various functions-

Running Containers

To run a container from an image, use the following command: docker run [options] IMAGE [command]

For example, to run an Nginx web server: docker run -d -p 80:80 nginx

Starting or Stopping the Container

  1. To start a stopped container, use: docker start CONTAINER_ID
  2. To stop a running container, use: docker stop CONTAINER_ID

Obtaining Container Information

  1. To list all running containers, use: docker ps
  2. To view all containers (including stopped ones), use: docker ps -a

Managing Images

  1. To pull an image from a registry, use: docker pull IMAGE_NAME[:TAG]
  2. To build an image from a Dockerfile, navigate to the Dockerfile directory and use: docker build -t IMAGE_NAME[:TAG].

Networking

  1. To create a user-defined bridge network, use: docker network create NETWORK_NAME
  2. To connect a container to a network, use: docker network connect NETWORK_NAME CONTAINER_NAME

Cleaning Docker

  1. To remove a stopped container, use: docker rm CONTAINER_ID
  2. To remove an image, use: docker rmi IMAGE_NAME
  3. To clean up unused resources (containers, networks, images, and volumes), use: docker system prune

Orchestrate

Docker Compose is a tool for defining and managing multi-container Docker applications. It configures the application’s services, networks, and volumes using a YAML file. With Docker Compose, you can easily spin up complex environments and applications. Here are the commands for it Docker Compose cheat sheet-

  1. To create and start all containers defined in the docker-compose.yml file use: docker-compose up
  2. To create and start containers in detached mode use: docker-compose up -d
  3. To stop and remove all containers, networks, and volumes created by docker-compose up use: docker-compose down
  4. To list all running containers defined in the docker-compose.yml file use: docker-compose ps
  5. To view the logs of a specific service use: docker-compose logs [service_name]
  6. To build or rebuild the images defined in the docker-compose.yml file use: docker-compose build
  7. To execute a command inside a specific service container use: docker-compose exec [service_name] [command]

Services

Services in Docker Swarm mode define the tasks on a cluster. They allow you to scale the application across multiple nodes and ensure high availability. Here are the commands for it: 

  1. To initialize a Docker Swarm on the current node, use: docker swarm init
  2. To join a Docker Swarm as a worker node use the following: docker swarm join
  3. To list nodes in the Docker Swarm, use: docker node ls
  4. To create a new service use the following: docker service create
  5. To list all services running in the Swarm, use: docker service ls
  6. To list tasks (containers) of a service use: docker service ps [service_name]
  7. To scale a service to N replicas, use: docker service scale [service_name]=N
  8. To update a service (e.g., change replicas, image version), use: docker service update
  9. To remove a service from the Swarm, use: docker service rm [service_name]
  10. To set a node to “drain” mode, meaning it will not receive new tasks use: docker node update –availability drain [node_name]

Interaction with a Container

To execute commands inside a running container, use: docker exec [options] CONTAINER_ID COMMAND [ARG…]

For example, to access the shell of a running container: docker exec -it CONTAINER_ID sh

Important Terms

  • Image: A lightweight, standalone, and executable software package that includes everything needed to run a piece of software, including the code, runtime, libraries, and dependencies.
  • Container: An instance of a Docker image that can be run, started, stopped, and deleted.
  • Dockerfile: A text file that contains instructions for building a Docker image.
  • Registry: A storage and content delivery system for named Docker images.
  • Repository: A collection of Docker images with the same name, distinguished by tags.
  • Docker Compose: A tool for defining and managing multi-container Docker applications.
  • Docker Swarm: A native clustering and orchestration solution for Docker.

Conclusion

In conclusion, Docker is an essential containerization platform that simplifies the process of building, shipping, and running applications by encapsulating them and their dependencies in isolated containers. With this comprehensive cheat sheet, users can efficiently harness the power of Docker, streamline application deployment, and maintain consistency across diverse environments.

Yana Khare 08 Aug 2023

Frequently Asked Questions

Lorem ipsum dolor sit amet, consectetur adipiscing elit,

Responses From Readers

Clear

  • [tta_listen_btn class="listen"]