Check Out Our Exclusive Docker Cheat Sheet!

Yana Khare Last Updated : 03 Apr, 2024
6 min read

Introduction

Docker is an open-source platform that makes using containers to build, ship, and operate applications easier. With the use of containers, developers may bundle dependencies and apps to make them uniform and portable across many contexts. A short reference guide to the key Docker Commands and ideas for using Docker efficiently is provided by this cheat sheet.

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

How Docker Works

Docker is a platform that lets you use containers to develop, launch, and operate applications. However, what are containers precisely, and how does Docker CLI operate? Let’s dissect it.

Containers: Lightweight and Portable

The code, runtime environment, system tools, and libraries required to run an application are all included in a container, which is a small, stand-alone package. An independent operating system is not needed for containers, in contrast to virtual machines (VMs). Instead, they share the host machine’s operating system kernel, making them more lightweight and efficient.

The Docker Engine

Docker Compose uses a client-server architecture, with the Docker CLI Engine at the core. The Docker Engine consists of three main components:

  • Docker Daemon: This is the server component that manages containers, images, networks, and volumes.
  • Docker Client: The client communicates with the Docker Daemon to execute Docker Commands and manage objects.
  • Docker Objects: These include images, containers, networks, and volumes, which are the building blocks of Docker Compose applications.

The Docker Workflow

Here’s a typical workflow when working with Docker CLI:

  • Make a Dockerfile: A text file called a Dockerfile has instructions on how to construct a Docker image.
  • Create an Image: You can create a read-only Docker image that has all the components required to run your application by using the Dockerfile.
  • Run a Container: Following the creation of an image, one or more containers based on that image can be launched.
  • Distribute and Deploy: Docker containers are lightweight and easily distributable to various settings, including production, testing, and development.

Benefits of Docker

Docker Compose offers several benefits, including:

  • Consistency: When an application operates consistently in many circumstances, the “it works on my machine” issue is resolved.
  • Isolation: To improve security and resource management, containers are isolated from the host system and from one another.
  • Portability: Containers are easily transportable between various environments, like a production server and a developer’s workstation.
  • Scalability: Due to their lightweight design, Docker containers are simple to scale up or down in accordance with resource requirements.

By understanding how Docker works and leveraging its capabilities, developers can streamline their application development, deployment, and maintenance processes.

Also Read: Top 28 Cheat Sheets for Machine Learning, Data Science, Probability, SQL & Big Data

Docker Architecture

Docker Compose 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.

Also Read: Cheatsheet – Python & R codes for common Machine Learning Algorithms

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, Docker 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

  • To start a stopped container, use: docker start CONTAINER_ID
  • To stop a running container, use: docker stop CONTAINER_ID

Obtaining Container Information

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

Managing Images

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

Networking

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

Cleaning Docker

  • To remove a stopped container, use: docker rm CONTAINER_ID
  • To remove an image, use: docker rmi IMAGE_NAME
  • 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-

  • To create and start all containers defined in the docker-compose.yml file use: docker-compose up
  • To create and start containers in detached mode use: docker-compose up -d
  • To stop and remove all containers, networks, and volumes created by docker-compose up use: docker-compose down
  • To list all running containers defined in the docker-compose.yml file use: docker-compose ps
  • To view the logs of a specific service use: docker-compose logs [service_name]
  • To build or rebuild the images defined in the docker-compose.yml file use: docker-compose build
  • 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 Docker Commands for it: 

  • To initialize a Docker Swarm on the current node, use: docker swarm init
  • To join a Docker Swarm as a worker node use the following: docker swarm join
  • To list nodes in the Docker Swarm, use: docker node ls
  • To create a new service use the following: docker service create
  • To list all services running in the Swarm, use: docker service ls
  • To list tasks (containers) of a service use: docker service ps [service_name]
  • To scale a service to N replicas, use: docker service scale [service_name]=N
  • To update a service (e.g., change replicas, image version), use: docker service update
  • To remove a service from the Swarm, use: docker service rm [service_name]
  • 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 Docker 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.

Frequently Asked Questions?

Q1. What is Docker and its role in application development?

A. Docker simplifies application development, deployment, and management through containerization, ensuring consistency across environments.

Q2. How do containers and Docker enhance software deployment?

A. Containers, like those in Docker, package applications and dependencies, improving deployment efficiency.

Q3. What are the main components of Docker’s architecture?

A. Docker Engine consists of the Daemon, Client, and Docker Objects, essential for managing containers and images.

Q4. What benefits does Docker provide to developers and operations?

A. Docker offers consistency, isolation, portability, and scalability, benefiting both developers and operations teams.

Q5. Can you outline the typical Docker workflow?

A. The Docker workflow involves creating a Dockerfile, building images, launching containers, and distributing them across various environments.

A 23-year-old, pursuing her Master's in English, an avid reader, and a melophile. My all-time favorite quote is by Albus Dumbledore - "Happiness can be found even in the darkest of times if one remembers to turn on the light."

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details