Docker Setup

Follow step-by-step instructions to set up Django on the Educative platform.

Dockerfile

The Dockerfile for setting up Django on the Educative platform is given below:

Press + to interact
FROM ubuntu:20.04
# Installing Python
RUN apt update && apt install -y software-properties-common &&\
add-apt-repository ppa:deadsnakes/ppa &&\
apt install -y python3.8 &&\
apt-get install -y python3-distutils python3-apt &&\
apt install -y curl &&\
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python3 get-pip.py
# Copying requirements.txt
COPY requirements.txt .
# Installing packages from requirements.txt
RUN pip install -r requirements.txt
# Copying the project
COPY helloworld helloworld

The Dockerfile starts with specifying the base image in line 1, ubuntu:20.04 in our case. Please avoid using the latest tag for the base image. Instead, specify the exact version, as we’ve specified 20.04.

The code in lines 4–9 sets up Python and pip. Please note how we’ve chained multiple RUN commands into one. This reduces the Dockerfile size and the build time.

In line 12, we copy the requirements.txt file and install it using pip in line 15.

Finally, in line 18, we copy the helloworld project.

Packages

The contents of the requirements.txt file are shown below:

Django==4.0.2

It contains only one package for now but will continue to contain more packages as we move along with the development of the Django web app. For example, Pillow can be added which is a Python library that allows us to manipulate, upload, and save different image file formats. The Psycopg2 and Psycopg2-binary libraries help us connect to the PostgreSQL database.

Tarball

The Dockerfile and all other necessary files are uploaded to the Educative platform in the form of a tarball. The tarball can be generated using the following command:

tar -zcvf Django.tar.gz Dockerfile helloworld requirements.txt

The tarball for setting up Django on the Educative platform can be downloaded from the link below:

Download Tarball for Django

Uploading tarball

Let’s see how to upload the Dockerfile tarball on the Educative platform.

Step 1: Click the “Change Docker File (Beta)” button in the “DOCKER CONTAINER” section on the collection editor page and upload the tarball.

After uploading the tarball, we see the following message:

Step 2: Click the “Save” button on the top-right corner of the collection editor page. After clicking the “Save” button, the Dockerfile will be uploaded and the image will start building.

After the image has been built successfully, we’ll see the following message:

Docker job

To add a docker job, click the + (Plus) button in the “DOCKER CONTAINER” section on the collection editor page as shown below:

The docker job for running a simple Django web app is shown below:

Let’s briefly talk about the “Run Script” and “Start Script”.

Start script

cd helloworld && python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:8000

First, we go into the project folder using the cd command. Then, we execute the commands to run the Django project.

Apps on Educative are run on host 0.0.0.0. Therefore, we need to specify it while running the project. 8000 refers to the port which we specify in the docker job.

Run script

cp -r usercode/* helloworld/

In the “Run Script”, we simply need to copy the files from the Single Page App (SPA) widget (residing in the usercode/ directory) into the project folder where the app is already running.

Note: If the code contains a syntax error, pressing the “Run” button will stop the server and show the error in the terminal. Even after fixing the code, the terminal still shows the error. At this stage, we need to start the server manually using the python manage.py runserver 0.0.0.0:8000 command.