How to connect your Django-Docker App to AWS

David Möller
Aug 23, 2016 · 2 min read

My colleague and i are working on a prototype where we need a backend application to manage a huge amount of data. We decided to go with Django, Docker and the Cookiecutter-Template. That package allows us to work independent while using the same system and requirements for the app. Yesterday we faced the problem that we need a common developer database where we will put our data to research on.

We decided to go with a Postgres Database on the Amazon Web Services (AWS) Relational Data Storage (RDS). Now there are two ways to connect the AWS to the Django Project in different environments. On one hand we could have uses the Environment Variables through the .env file and on the other hand the environment section of the docker container.

As we are complete newbies on this topic we tried both and ended with a solution by using the docker environment and changing the entrypoint.sh. The original dev.yml file of the template:

'dev-local.yml'version: '2'

volumes:
postgres_data_dev:
{}
postgres_backup_dev: {}

services:
postgres:
build:
./compose/postgres
volumes:
- postgres_data_dev:/var/lib/postgresql/data
- postgres_backup_dev:/backups
environment:
- POSTGRES_USER=*YOURPOSTGRESUSER*

django:
build:
context:
.
dockerfile: ./compose/django/Dockerfile-dev
command: python /app/manage.py runserver_plus 0.0.0.0:8000
depends_on:
- postgres
environment:
- POSTGRES_USER=*YOURPOSTGRESUSER*
- POSTGRES_NAME=*YOURPOSTGRESDBNAME*
- POSTGRES_URL=postgres
- USE_DOCKER=yes
volumes:
- .:/app
ports:
- "8000:8000"
links:
- postgres
- mailhog

pycharm:
build:
context:
.
dockerfile: ./compose/django/Dockerfile-dev
depends_on:
- postgres
environment:
- POSTGRES_USER=*YOURPOSTGRESUSER*
volumes:
- .:/app
links:
- postgres

mailhog:
image:
mailhog/mailhog
ports:
- "8025:8025"

has been modified to:

'dev-aws.yml'version: '2'

volumes:
postgres_data_dev:
{}
postgres_backup_dev: {}

services:

django:
build:
context:
.
dockerfile: ./compose/django/Dockerfile-dev
command: python /app/manage.py runserver_plus 0.0.0.0:8000
environment:
- USE_DOCKER=yes
- POSTGRES_USER=*YOURPOSTGRES-AWS-USER*
- POSTGRES_NAME=*YOURPOSTGRES-AWS-DBNAME*
- POSTGRES_URL=*YOUR AWS RDS ENTRYPOINT*
- POSTGRES_PASSWORD=*YOURPOSTGRESUSERPASSWORD*
volumes:
- .:/app
ports:
- "8000:8000"
links:
- mailhog

mailhog:
image:
mailhog/mailhog
ports:
- "8025:8025"

So now we had two separate configuration files for docker which enabled to work in different development environment. The local one will be used for building new models and to change the Data System. The one on the AWS is for recording data and to try different machine learning algorithms on top of it.

One last step is necessary so that it will work. In the /compose/django/ folder is entrypoint.sh file. The entry for the DATABASE_URL has to be changed to the following line to work:

export DATABASE_URL=postgres://$POSTGRES_USER:$POSTGRES_PASSWORD@$POSTGRES_URL:5432/$POSTGRES_NAME

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store