How to Deploy Django Project into Docker Container.

Reading Time : ~ .

Deploying app in containers as microservices has its own pro's and cons. For a good understanding of the role of Containers in MicroServices, Read this article. In this blog post we will look into deploying a sample Django app into a docker container.

Using Dockerfile:

Creating Sample Django project.

sudo apt-get install python-dev python-pip python-virtualenv
mkdir /webapps && cd /webapps
virtualenv env
source /webapps/env/bin/activate
pip install django
django-admin.py startproject testapp

now that you have a sample django project, copy /webapps/testapp to a folder called web where we will build Docker APP. the folder structure should be

`-- <some-folder>
    |-- Dockerfile
    |-- web
       `--scripts
        |-- testapp
        |-- confs

You can prehandedly prepare a docker image that contains all your system apt/yum requirements or write a script that can install it on runtime and save it in scripts folder.

Prepare a <project-home>/scripts/project_setup.sh and give it execution permission and include your confs files like gunicorn, uwsgi, supervisor,nginx files. Build a shell script that does the rest of processing for you.

FROM ubuntu:latest

ENV APP_NAME=testapp
ENV DJANGO_SOURCE=web
ENV PROJECT_HOME=/webapps/$APP_NAME
RUN mkdir -p $PROJECT_HOME
COPY $DJANGO_SOURCE $PROJECT_HOME
RUN $PROJECT_HOME/scripts/project_setup.sh
ENTRYPOINT ["/<some-script that starts required services>"]

Build a Docker Image with below command:

docker build -t <image-name> .

Now Deploy your app with standard DOcker run command.

Docker run -d <image-name>

 Happy Dockerizing!

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
TMUX - Sharing terminal between Users Jagadeesh V

More Often you wouldve come across sharing screens with applications like Teamviewer, Windows Remote Desktop Connection or remmina (if you're a linux user). But if ...

Continue Reading...
Web Hooks for Gitlab using PHP and Shell Scripts Jagadeesh V

Web-hooks play vital role if you are in Continuous Integration(CI). Higher Level organizations follow GitLab for CI purposes if they operate on open source solutions ...

Continue Reading...
How to deploy django with uwsgi and nginx using Ansible play book. Dinesh Deshmukh

Ansible is a scripting language mostly used to automate installing applications, deploying apps etc..
Why Ansible:
You may think why learn new language if I ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties