Docker compose
- This lets you bring up multiple containers with simple command
docker compose up -d
docker compose down
# bring down and remove volumes
docker compose down -v
-
To use docker compose we need to create an yaml file with name
docker-compose.yml
-
YAML file is used to represent data in the form of key value pairs
- Each key value pair has the following syntax
key: <value>
- Values can come in many forms
- Simple forms
- Text
name: QT
name: 'QT'
name: "QT"
recommendation_letter: |
Aarav has shown exceptional coding skills.
He always submits assignments before the deadline.
He is highly recommended for the internship.
recommendation_letter: >
Aarav has shown exceptional coding skills.
He always submits assignments before the deadline.
He is highly recommended for the internship.
age: 15
price: 10.99
online: True
online: yes
extraprice: no
extraprice: False
colors: ["Red", "Blue"]
colors:
- Red
- Blue
colors:
- Red
- Blue
address:
flatno: 601-A
building: nilgiri
city: Hyderabad
pincode: 500016
- YAML is used to represent data which is easier to understand to both humans and applications
- When an yaml is meant for application, application defines the structure
This is my yaml schema
- This is my yaml schema to document your favorite movie
title: <text> | movie name | required
release_year: <number> | required
witness_date: <text> | when you watched the movie for first time | required
repeats: <number> | how many time you watched this movie | required
last_watch_date: <text> | when did you watch for the last time | optional
title: Avengers Infinity War
release_year: 2018
witness_date: 'April 27, 2018'
repeats: 100
title: Avengers Infinity War
release_year: 2018
witness_date: 'April 27, 2018'
repeats: 100
last_watch_date: 'May 27, 2026'
Docker compose yaml
Lets write a docker compose file
Case 1
- I want to create an nginx container
- equivalent command
docker run -d -p 19000:80 --name web1 nginx
- Create a file
docker-compose.yml with the following
---
services:
web:
image: 'nginx:1.31'
ports:
- "19000:80"
- Now cd in the folder where you have this compose file
docker compose up -d
-
Now access your application on
http://localhost:19000/
-
To bring them down
docker compose down
Case 2
- I want an nginx container and a mysql contianer
- Mysql and nginx should be in same network
- mysql container requires a volume and it has some enivronment variabls to be passed
# Create network
docker network create app-network
# Create persistent volume for MySQL
docker volume create mysql-data
# Start MySQL container
docker run -d \
--name mysql \
--network app-network \
-v mysql-data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=rootpassword \
-e MYSQL_DATABASE=myappdb \
-e MYSQL_USER=myuser \
-e MYSQL_PASSWORD=mypassword \
mysql:8.4
# Start Nginx container
docker run -d \
--name nginx \
--network app-network \
-p 18880:80 \
nginx:latest
# Verify
docker ps
# Inspect network
docker network inspect app-network
Case 3
- Till now we are using images from docker hub what if we have a docker file
- This means before bringing up the contaienrs it needs to build images.
- To be discussed tomorow