Docker Compose

We use docker based on commands like docker build, docker run,-v,-p, etc., every time we develop images or containers. Even though commands are long and repeated many times, we still type in command prompt/shell.


When it comes to technology, it's all about convenience and reducing repetition. So docker comes with the solution called 'Docker Compose.' This comes as default when you download docker from the official site.


So Docker Compose helps us to manage multiple containers with ease. We need just one file 'docker-compose. yaml' file with inputs is given as key-value pairs. When we have this file, we have to give one command - docker-compose up or docker-compose down from the terminal that has the file - docker-compose .yaml.

Docker-compose is called management/orchestration (everything can be staged into a configuration file) of multiple containers.


How to write a yaml file?

yaml files depend on indentation for execution like python file.


Basic commands are


version: [what version of docker-compose you want to use] --> available official website

services:
 [service_name]:-->frontend,backend,db etc.
   #a child will be two space indented
   image:[official image name/custom image name/path to repository] or if no image exists  
   build:[docker_file_path]
   ports:
   #we use '-' in front for lists in YAML
     -['containerport:external_port']
   volumes:
     -[host directory]:[container directory] --> for bind mounts
     -[named of volume]:[container directory] --> named volume --> needs to be included in meta volume at the end of file
     -[container_path] --> for anonymous volume
   depends_on:
     -[service_name]
     #we use depends on key when we want to start some service before this service needs to be started

By default, docker-compose puts all services in a single network - They can be communicated with the service name, or we can also create a custom network in addition to the default network.