Day 13: Docker Compose

Hi, I am an AWS-certified cloud engineer and I write about my progress and learnings of DevOps.
Hello Everyone!
Let's explore another concept of Docker which is Docker Compose.
Docker Compose is a tool that was developed to help define and share multi-container applications.
With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.
It can start all containers with a single command i.e.
docker compose upIt can stop all containers with a single command i.e.
docker compose down
What is YAML?
YAML is a data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ain’t markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.
YAML is a popular programming language because it is human-readable and easy to understand.
YAML files use a .yml or .yaml extension.
Docker-compose
Install docker-compose:
I'm using an AWS instance with Amazon Linux 2 OS. These are the commands by which I installed on my server.
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose docker-compose version
Create a Docker compose file:
touch docker-compose.yml
version: '3'
services:
db:
image: mysql
container_name: mysql_db
restart: always
environment:
- MYSQL_ROOT_PASSWORD="secret"
web:
image: apache
build: .
container_name: apache_web
restart: always
ports:
- "8080:80"
- Check the docker-compose file by using
docker-compose config
Execute the file:
docker-compose upCheck for the containers after the above command.
Similarly, we can stop all containers by using
docker-compose down
The difference between a Docker file and Docker compose
Dockerfile
A Dockerfile is a simple text file that contains the commands a user could call to assemble an image.
Example,
Dockerfile:
FROM ubuntu:latest
MAINTAINER john doe
RUN apt-get update
RUN apt-get install -y python python-pip wget
RUN pip install Flask
ADD hello.py /home/hello.py
WORKDIR /home
Docker Compose
Docker Compose
is a tool for defining and running multi-container Docker applications.
define the services that make up your app in
docker-compose.ymlso they can be run together in an isolated environment.get an app running in one command by just running
docker-compose up
Example, docker-compose.yml
Dockerfile
A Dockerfile is a simple text file that contains the commands a user could call to assemble an image.
Example, Dockerfile
FROM ubuntu:latest
MAINTAINER john doe
RUN apt-get update
RUN apt-get install -y python python-pip wget
RUN pip install Flask
ADD hello.py /home/hello.py
WORKDIR /home
Docker Compose
Docker Compose
is a tool for defining and running multi-container Docker applications.
define the services that make up your app in
docker-compose.ymlso they can be run together in an isolated environment.get an app running in one command by just running
docker-compose up
Example
docker-compose.yml:
version: "3"
services:
web:
build: .
ports:
- '5000:5000'
volumes:
- .:/code
- logvolume01:/var/log
links:
- redis
redis:
image: redis
volumes:
logvolume01: {}
difference between dockerfile and docker-compose.yml
References:
Thanks for reading ;)







