Docker x Django: A match that eases Collaboration.

·

4 min read

Introduction

I have worked with many applications in the past that included Docker to enable programmers to collaborate, but recently, I built a wallet management system that needed the use of Docker, and I would love to share a step by step guide to how I was able to set it up. First, I'd like to explain what Docker is, in simple clear terms, and what it is used for.

What is Docker?

Docker is a tool, an application, a "container builder" that among other things, helps create a separate, isolated virtual environment for your application. For example, a team of 5 engineers is working on a project, and everyone makes contributions that demand them to install various packages, the tendency for issues to arise from improperly configured environment variables, packages, and dependencies, etc, is high. With Docker, you can create a separate environment for your project to run. In this case, anyone who clones the repository follows the prepared docker commands and is able to successfully install all dependencies, run all services and use the application easily.

Docker and Django

Django is a Python framework for web development and is slowly becoming one of my favorite frameworks to use. It comes with a lot of access to packages. In python, there is almost a package for everything. I will be walking you through how I wrapped up my Django project in a Docker container so that anyone could have access to the project when needed.

Steps to Using Docker in a Django application

  1. Prepare, set up, and create a Django Project. If this is new, check out this Django documentation on how to set up a Django project here

  2. On your local computer, download and install Docker. Follow this link to install Docker. Pick the choice that applies to your machine.

  3. Once Docker is running, In the root directory of your Django project, the same location with your "manage.py" file, create two files, one is a docker-compose.yml and the other a Dockerfile. The Dockerfile defines your application’s image content via one or more build commands that configure that image. Once built, you can run the image in a container. Here is the official Docker Documentation for Django Dockerfiles. A typical Dockerfile for Django should look like the code below:

        # The first instruction is what image we want to base our container on
        FROM python:3.8
    
        # The environment variable ensures that the python output is set straight
        # to the terminal without buffering it first
        ENV PYTHONUNBUFFERED 1
    
        # create a root directory for our project in the container, mine is the name of my 
        Django project
        RUN mkdir /walletsystem
    
        # Set the working directory to your working directory
        WORKDIR /walletsystem
    
        # Copy the current directory contents into the container at your working 
        directory
        ADD . /walletsystem/
    
        # Install any needed packages specified in requirements.txt (this would be 
        created)
        RUN pip install -r requirements.txt
    
  4. You would need to create a requirements.txt file. This holds a list of all the dependencies installed and used in your application. This file is used by the RUN pip install -r requirements.txt command in your Dockerfile. In your django project, run

       pip freeze
    

    This returns a list of all the requirements used in your project. copy the list into a file named "requirements.txt"

  5. Next thing is to write the content in your docker-compose.yml file. A typical docker-compose file should look like this:

version: "3.8"

services:
  db:
    image: postgres #mine is postgresql
    environment:
      - POSTGRES_DB=dbname
      - POSTGRES_USER=dbuser
      - POSTGRES_PASSWORD=dbpassword
    volumes:
      - ./tmp/db:/var/lib/postgresql/data
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/walletsystem #(nameofworkingdirectory)
    ports:
      - 8000:8000
    depends_on:
      - db
  1. 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.

  2. In a different terminal, make migrations docker-compose exec web python manage.py makemigrations

  3. Run migrate docker-compose exec web python manage.py migrate

  4. Your Docker x Django project should be running smoothly.

Conclusion

I may have left somethings out, so I also advise, as I love to do, read and follow the official documentation for these programs.