How to contain your first Django in docker and access it from AWS | by Abdelilah OUASSINI | Towards Data Science

Today, a lot of junior software engineering Jobs require a tech stack including Django, python, docker, and AWS. If you are a beginner and want to discover these techs, then you are in the right place. I will guide you through this article to get hands dirty manipulating theses tools and creating your first Python Django application contained in docker and accessible from AWS.
All you will need :
Docker Hub free account: https://hub.docker.com/
AWS free account https://aws.amazon.com/account/
MAC or Windows laptop.
Pycharm Framework https://www.jetbrains.com/pycharm/download/
A Brain :)
One hour maximum from your valuable time.
Are you ready?
So let’s start... The first step is to open your pycharm and create a new project.
Next, you have to install Django library in pycharm:
pip install django
And then, just start your project by the below command (we named our project DjangoDocker, but you may name anything else).
django-admin startproject DjangoDocker
This will create the project directory and a few files inside, we will check them in details later on.
After that, you need to create your first application inside your Django project using the below command:
python manage.py startapp myApp
This will create a new directory inside your main project directory, it will contain the following files:
You may note that we can create multiple applications inside the same Django project, this is very useful in case we want to create a backend API and use it across multiple projects.
Let’s now try to fill in the code in our first app and see how we can dockerize and access it from the cloud !!
On the next steps, we will fill the minimum required coding to get a “Salam World” Django application.
First, open the file “views.py” in myApp directory and fill it with the following code :
from django.shortcuts import render# Create your views here.from django.http import HttpResponsedef index(request):    return HttpResponse(" Django on Docker on AWS ")
Then, you should indicate to Django that this file will be your index, for this, just put the following code in myApp => urls.py
And just fill the root URLs paths at urls.py (root folder).
from django.contrib import adminfrom django.urls import include, pathpath(‘<appname>/’, include(‘<appname>.urls’)),
After that, you may initiate the database and check if your application is working :
## initiate the databasepython manage.py migrate## check it workspython manage.py runserver
Just launch the below URL and you should see a similar result:
An intermediate step before pushing the code to docker image is to put it in GITHUB to keeps track of the various changes made to every iteration. This will also allow you to implement your DevOps CI/CD pipeline so to push the changes directly and “ Automatically” to AWS without the need of repeating all these steps.
## initiate GIT# Go to directory and tape:git init## Create and fill .gitignore to untrack filesgit status #( to check )git add — all .git commit -m “My first commit”## push to GitHIB## Connect to Github (login/password)## create a new repogit remote add origin https://github.com/<login>/<repo>.gitgit push -u origin master
Now, we will see how to contain the code in a docker image and push it to the docker hub.
To create a docker image, just create the following 3 files in Django root directory :
Dockerfiledocker-compose.ymlrequirements.txt
— — — — — Dockerfile# We Use an official Python runtime as a parent imageFROM python:3.6# The enviroment variable ensures that the python output is set straight# to the terminal with out buffering it firstENV PYTHONUNBUFFERED 1# create root directory for our project in the containerRUN mkdir /<dir-in-container># Set the working directory to /music_serviceWORKDIR /<dir-in-container># Copy the current directory contents into the container at /music_serviceADD . /<dir-in-container>/# Install any needed packages specified in requirements.txtRUN pip install -r requirements.txtEXPOSE 8000CMD exec gunicorn <Django-project_name>.wsgi:application — bind 0.0.0.0:8000 — workers 3— — — — docker-compose.ymlversion: ‘3’services: web: build: . command: bash -c “python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000” container_name: <dir-in-container> volumes: — .:/<dir-in-container> ports: — “8000:8000”   — — — — requirements.txtDjango==2.1.5gunicorn==19.9.0
After that, create your docker image by running the following command :
docker build -t <DockerLogin>/<Docker-Image_name> .# PS: don’t forget the “.” at the end of the above command
Where DockerLogin is your docker Hub login and Docker-Image_name is the name you want for your image.
To test your image is working, just launch:
docker run -p 8000:8000 -i -t <DockerLogin>/<Docker-Image_name>
You should see the result on your declared IP 0.0.0.0 at the routed port 8000 like below :
To push the image into docker hub, just launch the following:
docker push <DockerLogin>/<Docker-Image_name>
PS: Check that your local docker is running as shown in the picture below (for Mac users).
Now, your Django app is pushed to Docker Hub Image, and anyone may pull it from the hub, and instantiate it in a docker to run it in his local environment, without worrying about dependencies, or having Django, Python ...etc. locally installed.
let’s see now how to host your docker image and access it from AWS.
First of all, you need to create a JSON file telling to AWS how to find your docker image, please find below an example:
— — — -Dockerrun.aws.json{ “AWSEBDockerrunVersion”: “1”, “Image”: { “Name”: “abdel1979/<image-name>”, “Update”: “true” }, “Ports”: [ { “ContainerPort”: “8000” } ], “Logging”: “/var/log/nginx”}
After that, just navigate to management console in your AWS account :
And then to Elastic BeansTalk.
After that, create a new application by giving a name, then select the webServer environment:
Next, fill the required information, environment name, domain and choose docker as a platform and finally upload your JSON file.
Finally, wait a few minutes for creating your environment, once ready, open the domain you chose, and you will see your application displayed as shown below :
And here we are :)As always, I hope you’ve learned something new.