DEV Community

Cover image for #021 docker-compose
Omar
Omar

Posted on

#021 docker-compose

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

if you follow part 9

cd location/DevOpsJourney/
git pull
cd app_021/
Enter fullscreen mode Exit fullscreen mode

replace location with where you put the DevOpsJourney

if your new go to part 9 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

python manage.py runserver 0.0.0.0:8000
Enter fullscreen mode Exit fullscreen mode

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

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
  4. 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
  5. image is the name of our image
  6. container_name name of our container
  7. depend_on postgres it mean she need the postgres service
  8. volumes of our Django
  9. environment files let's take a look at .django .django they are some prefixed variables that we can use later on our development
  10. ports are the port we need to open in our case is 8000
  11. command: /start it's a script we need to run let's take a look at it start first lines we talk before about them
  12. python manage.py migrate it's Django thing to migrate our changes to database
  13. python manage.py runserver_plus 0.0.0.0:8000 it's Django way of running our server

docker-local2

same things for postgres , this is a look inside .postgres file
.postgres
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

docker-compose -f local.yml up -d
Enter fullscreen mode Exit fullscreen mode

up here is to create and start containers
-d here is for running in background
finish
after waiting for some time it will download lot of thing because it's real world app.

Alt Text

docker ps -a
Enter fullscreen mode Exit fullscreen mode

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

in our browser go to

127.0.0.1:8000
Enter fullscreen mode Exit fullscreen mode

final

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

Top comments (0)