#021 docker-compose
Tags: docker, devops, tutorial
Published: 2020-06-22 23:00:46

# Introduction

this is part 21 from the journey it's a long journey(360 day) so go please check previous parts , and if you need to walk in the journey with me please make sure to follow because I may post more than once in 1 Day but surely I will post daily at least one 😍.

And I will cover lot of tools as we move on.
***

# Download app_021
![app_015](https://dev-to-uploads.s3.amazonaws.com/i/vvdddf48acyxqdwnvfa7.png)

if you follow [part 9](https://dev.to/elkhatibomar/009-dockerfile-113f)

```bash
cd location/DevOpsJourney/
git pull
cd app_021/
```
replace location with where you put the DevOpsJourney

if your new go to [part 9](https://dev.to/elkhatibomar/009-dockerfile-113f) and do same steps will download old lecture files and new one.

***

# Django

Django is a python web-framework .
In this part I will use an real world dockerized Django that ready for production.
Our project will connect to postgres also as database.

***

# Without docker-compose

to build our images and run our containers before we run them manually and it's hard to remember what we need to pass as envirement variables such as port , also if we give our Dockerfile to someelse probably he doesn't know what to pass.
So here came the role of our docker-compose to make life more and more easy.
***
# docker-compose

docker-compose is using an YAML (Yet Another Markup Language) to do the boring work instead of us. 
So here how it works , it simply take as default input the yaml or yml file in the project the default is a file named docker-compose.yml , in our project we don't have it we have our own files called local.yml and production.yml , one for local development and the other for production. 

***
# Explain project

usually when we don't use docker , Django application run with
```bash
python manage.py runserver 0.0.0.0:8000
```
8000 is the port here , so we need to open that port in our docker.

I am going to take look inside the local.yml for now

![docker-local1](https://dev-to-uploads.s3.amazonaws.com/i/s9xwivrqph19rr3c5vn0.png)

we can see a lot of stuff here but if you follow me from start of journey you will be comfortable with what you see.

let's talk about each line here:
1. version: '3' mean we are using docker-compose api version 3 (I will talk more about in upcoming part)
2. volumes here we setup 2 postgres volumes one for data and one for backup (we talk about volumes also before)
3. services here the important part 
- django is the name of our service , build we need to specify the dockerfile path in my case it's located at ./compose/local/django/Dockerfile
- image is the name of our image
- container_name name of our container
- depend_on postgres it mean she need the postgres service
- volumes of our Django
- environment files let's take a look at .django
![.django](https://dev-to-uploads.s3.amazonaws.com/i/nlc5b2vzccfh7emv3g1s.png)
they are some prefixed variables that we can use later on our development
- ports are the port we need to open in our case is 8000
- command: /start
it's a script we need to run let's take a look at it
![start](https://dev-to-uploads.s3.amazonaws.com/i/zvvs5q8fqzktu6on429v.png)
first lines we talk before about them
- python manage.py migrate it's Django thing to migrate our changes to database
- python manage.py runserver_plus 0.0.0.0:8000 it's Django way of running our server

![docker-local2](https://dev-to-uploads.s3.amazonaws.com/i/e76yyq698yddc0an0zti.png)

same things for postgres , this is a look inside .postgres file
![.postgres](https://dev-to-uploads.s3.amazonaws.com/i/zxu5t7jyr51jy31ozosz.png)
It conatin some variables used by postgres.
***

# Running our compose file
Since we use an modified file (local.yml) , in able to run it we should use -f which stand for file we need right after docker-compose
```bash
docker-compose -f local.yml up -d
```
up here is to create and start containers
-d here is for running in background 
![finish](https://dev-to-uploads.s3.amazonaws.com/i/subg6qpv48a1nby5vmuv.png)
after waiting for some time it will download lot of thing because it's real world app.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/16i0mh6l6og7118j5e8w.png)
```bash
docker ps -a
```

we can see we have our containers running in the background.

in our browser go to 
```bash
127.0.0.1:8000
```

![final](https://dev-to-uploads.s3.amazonaws.com/i/9mr3c8ht7bc3wvxd8mw9.png)

we have a website running just with one command , amazing!

***
# Challenge

go and take a look at Dockerfile and try to understand it ,
also take a look at production.yml file