Docker Cheat Sheet
4 min read

Table of contents
Docker is a popular open-source platform that allows developers to automate the deployment, scaling, and management of applications using containerization. It consists of several key components that work together to enable containerization and provide a comprehensive container runtime environment.
you can refer to my blog on medium for basic commands and components on docker: https://medium.com/@srisanthseth28/docker-a8e606bbf350
Docker-compose
Docker Compose is a tool that allows you to define and manage multi-container Docker applications or replicas. It simplifies the process of creating and running complex containerized applications by providing a way to describe the services, networks, and volumes required for your application in a single YAML file.
following steps can be used:
we need to create a docker-compose.yml file containing Services and configuration about docker containers.
use the docker-compose build command to build the container
docker-compose build
- use docker-compose up to start the container
docker-compose up
use the following commands, to manage, stop, and scale containers.
to scale up containers
docker-compose --scale <service_name>=<no_of_replicas>to stop a container
docker-compose downto see running containers
docker-compose logs
Docker Volume
Docker volumes are a feature of Docker that enable persistent storage for containers. Volumes provide a way to share and store data between containers and the Docker host. Here are some key points to understand about Docker volumes:
Separate from containers: Volumes are managed independently of containers. They can be created, attached, and detached from containers as needed, allowing data to persist even if the container is deleted.
Flexible storage options: Docker volumes support various storage options, including local disk, network-attached storage (NAS), cloud-based storage, and more. This flexibility allows you to choose the storage solution that best fits your requirements.
Sharing data between containers: Volumes provide an effective way to share data between containers. Multiple containers can attach to the same volume, allowing them to read from and write to the shared data.
Named volumes: Docker allows you to create named volumes that can be referenced by name when starting containers. Named volumes make it easy to manage and identify volumes across different containers and hosts.
Volume drivers: Docker supports different volume drivers that allow you to use different types of storage backends for volumes. You can use default drivers like the local driver or choose third-party drivers for specific storage solutions.
Persistent data: Docker volumes allow you to persist data even when containers are stopped or removed. This makes them suitable for storing important or frequently updated data, such as databases, user uploads, or configuration files.
Here are a few commands and examples related to Docker volumes:
Create a volume: To create a Docker volume, use the
docker volume createcommand. For example:docker volume create my-volumeList volumes: You can list all the available volumes using the
docker volume lscommand:docker volume lsAttach a volume to a container: When starting a container, you can attach a volume using the
-vor--mountoption. For example:docker run -d -v my-volume:/path/in/container my-imageMount a host directory: You can also mount a directory from the Docker host into a container using volumes. For example:
docker run -d -v <volume_name>:<container_path> my-imageRemove a volume: To remove a Docker volume, use the
docker volume rma command followed by the volume name or ID:docker volume rm my-volume
Docker Networking
Container networking refers to the ability for containers to connect to and communicate with each other, or to non-Docker workloads.
when you create or run a container using docker create or docker run, the container doesn’t expose any of its ports to the outside world. Use the --publish or -p flag to make a port available to services outside of Docker. This creates a firewall rule in the host, mapping a container port to a port on the Docker host to the outside world.
docker run -p 127.0.0.1:8080:80 mongo