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 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 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
To install Docker on your system, follow the appropriate instructions for your operating system. Below are the steps for Windows and MacOS.
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.
Once installed, Docker Desktop should start automatically. You’ll see the Docker icon in the system tray when it’s running.
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.
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-
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
- 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].
- 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
- 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
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 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:
- 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 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
- 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.
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.