Run Django using Docker compose

Posted By : Abhishek Kumar | 29-Dec-2016

Docker-compose is a tool used to define multiple docker container applications. Docker compose we can run multiple containers applications as a service.

Similar to Dockerfile which is used to launch a docker image, Docker compose also uses docker-compose.yml to run the contain services in an isolated environment.

Installing Docker-compose

There are three ways to install docker compose:

* As binary
 Download the binary and move to bin directory

sudo curl -L "https://github.com/docker/compose/releases/download/1.9.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose

* Using python pip
Docker compose can also be installed using pypi.
Python packages can conflict with docker-compose so it is advisable to use virtualenv

sudo pip install docker-compose

* As shell
Download the script in the bin directory and rename them as docker-compose

 curl -L https://github.com/docker/compose/releases/download/1.9.0/run.sh > /usr/local/bin/docker-compose
 chmod +x /usr/local/bin/docker-compose

 This script runs docker-compose inside a container through a bash script wrapper.


Install Docker-compose command completion from here   

curl -L "https://github.com/docker/compose/releases/download/1.9.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose


Quick reference for running Docker-compose

1) Define Dockerfile for your app.
2) Define docker-compose.yml
3) run "docker-compose up" to start and run your app


Here we will use docker-compose to launch a django application

 

Prerequisites

 

1) Dockerfile
2) Python Dependencies file
3) Docker-compose.yml

** Let us create a new base directory as "mydocker" for our project

** Creat python dependencies file dependencies.txt with following contents
    Django
    psycopg2

** Create a Dockerfile and define an application's image content by using one or more build commands to configure our image.

 FROM python:2.7       # Downloads the Python Docker image
 MAINTAINER Abhishek Kumar <[email protected]>   #
 ENV PYTHONUNBUFFERED 1  # Defines an environment variable pythonunbuffered with value 1
 RUN mkdir /code       # Runs a command
 WORKDIR /code         # Creates a directory
 ADD dependencies.txt /code/  # copies the file to docker image
 RUN pip install -r dependencies.txt
 ADD . /code/

This Dockerfile will download a python2.7 docker image will create a directory /code and also installs the python dependencies.

** Create docker-compose.yml and define it as

 version: '2'
 services:
   db:
     image: postgres
   web:
     build: .
     command: python manage.py runserver 0.0.0.0:8000
     volumes:
       - .:/code
     ports:
       - "8000:8000"
     depends_on:
       - db
     

 Here it is creating Two Services named as
   1) db : which is using the official postgres docker image
   2) web: which is building the image form the dockerfile.
           command: Override the default command
           volumes : mount paths from host to container
           ports: binds host port to container port
           depends_on: starts the db and proxy services before the web service


** Now change your working directory to "mydocker"
   
    
** Now run the Initialize the web server

   docker-compose run web django-admin.py startproject mydjango .
 
  This will run the web service build the "web" docker image as specified in build: . in docker-compose file.
  Once the image is built it will run the django-admin.py startproject command in the containe

** Now to edit the mydjango/settings.py to  setup the database connection for Django


   Replace DATABASES = {...} with the following

   DATABASES = {
       'default' : {
         'ENGINE': 'django.db.backends.postgresql',
         'NAME': 'postgres',
         'USER': 'postgres',
         'HOST': 'db',
         'PORT': 5432,
       }
   }

These are default values which are specified in the postgres docker image

** Save the file and run the

docker-compose up
  

This will search for the docker-compose.yml or docker-compose.yaml file in the present working directory and will download the images specified by the images take in the docker-compose file
    
    #docker-compose up  -d
    -d will run the docker-compose as daemon
    
    You can selectively run any service using
      docker-compose run service-name


** Now your docker service will be running now you can access your using localhost:8000

 

You can now enter your container and modify your application accordingly using

 docker exec -it container-name /bin/bash

 

 

 

 

About Author

Author Image
Abhishek Kumar

Abhishek is Redhat and AWS Certified and a keen python enthusiast. His hobbies are cycling and volleyball.

Request for Proposal

Name is required

Comment is required

Sending message..