Containerizing Django App on Docker Using VSCode editor.

  • Created a Dockerfile file describing a simple Python container.
  • Build, run, and verify the functionality of a Django app.
  • Debug the app running in a container.

Steps required to create a Django App On Visual Studio Code.

— Downloaded Python by specifying the path to be in my \C drive when installing. (On Windows, make sure the location of your Python interpreter is included in your PATH environment variable. You can check the location by running path at the command prompt. If the Python interpreter’s folder isn’t included, open Windows Settings, search for “environment”, select Edit environment variables for your account, then edit the path variable to include that folder).

— Downloaded and install Git, check git is enabled from settings.

— Create a directory (A folder on Desktop called practiceddjango)

— In the folder created, activated the environment using gitbash with this command(python -m venv env) by right clicking and using the the GitBash GUI.

  • On VSCode, the path can added by selecting Python Interpreter using the search bar.
  • Also select the activated environment into VScode.

Creating the Django Project

  • Launched the terminal on VS code and run

python -m pip install Django

  • In the activated virtual environment run

django-admin startproject web_project

  • The new web-project would be same directory with manage.py file. To verify the new created django project

— cd into web_project then run

python manage.py runserver

After verifying my app is running properly, Next is to Dockerize the Django application.

Adding Docker files to the project

  • Created a dockerfile in same directory as manage.py
  • Added the following command :
  • FROM python:3
    ENV PYTHONUNBUFFERED=1
    WORKDIR /code
    COPY requirements.txt /code/
    RUN pip install -r requirements.txt
    COPY . /code/
  • Saved and closed the dockerfile.
  • Created a requirements.txt in the same project directory.

Added the following into the requirement.txt, save and close.

Django>=3.0,<4.0
psycopg2-binary>=2.8

  • Create a file called docker-compose.ymladded the following and saved and closed.

version: "3.9"

services:
db:
image: postgres
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
web:
build: .
command: python manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db

This file defines two services: The db service and the web service.

The dockerfile and docker-compose.yml file will be used in building the image from the build context defined in the previous procedure.

On your local machine, ensure that you have created a database with the listed parameters. Run docker-compose up --build To get your project up and running.

In the settings.py of the web_project created, the database needs to be edited and replaced with the following command:

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

Also edited this and replace with this ALLOWED_HOSTS = ['*']

Then run docker-compose up to have your docker running and verify with running the url http://0.0.0.0:8000/.

Run the following docker commands

  • docker container ls
  • docker ps

On the terminal and cd into the project directory where we have manage.py and run the following command:

docker compose up

docker compose up — build

docker ps

docker container ls

This shows that your app has been containerized.

--

--

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
Adeniye Kehinde Afusat

Kehinde is a technology enthusiast, experienced with Azure DevOps. She has expertise in Cloud technologies and software programming.