How to run a python django app in docker

How to run a python django app in docker

Django, described as the The web framework for perfectionists with deadlines is a high-level Python Web framework that encourages rapid development and clean design.

In this post I will show you how to get Django up and running in a docker container.

Steps

  1. The first thing you will need to do is install docker compose. Compose is a tool that allows you to run multi container applications.
    I will be installing compose on an Ubuntu 18.04 system. To install compose on another OS check out the compose documentation here.To run Compose you need to have docker installed. If you don't already have docker installed run the following commands

    sudo apt-get remove docker docker-engine docker.io containerd run
    
    sudo apt-get update
    sudo apt-get install \
        apt-transport-https \
        ca-certificates \
        curl \
        gnupg-agent \
        software-properties-common
    
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    sudo add-apt-repository \
       "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
       $(lsb_release -cs) \
       stable"
    
    sudo apt-get update
    sudo apt-get install docker-ce docker-ce-cli containerd.io

    Now check to see if docker has installed successfully by running the following command

    sudo docker run hello-world

    You should see the following output

    How to run a python django app in docker

  2. Once docker is installed you can then install docker compose.
    To install compose run the following command

    sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

    Once the download is complete apply executable permissions to the binary

    sudo chmod +x /usr/local/bin/docker-compose

    How to run a python django app in docker-000004

    Now test the installation

    docker-compose --version

    How to run a python django app in docker-000005

    The docker compose version should be returned. If not check your path. You can also create a symbolic link to /usr/bin or any other directory in your path.

  3. Now that docker compose is installed you can start working on getting django up and running.
    To do this we will create a Dockerfile, a Python dependencies file, and a docker-compose.yml file.Start by creating an empty directory for your project.
    Then create a file called Dockerfile. Read more about Dockerfiles here.How to run a python django app in docker-000007
  4. Within your Dockerfile add the following.
    FROM python:3
    ENV PYTHONUNBUFFERED 1
    RUN mkdir /code
    WORKDIR /code
    COPY requirements.txt /code/
    RUN pip install -r requirements.txt
    COPY . /code/

    The Dockerfile starts by pulling a python3 image
    RUN mkdir creates the /code directory
    COPY will copy the requirements.txt file into the /code directory in the docker container
    RUN will run the pip command against your requirements.txt file
    COPY . will copy the contents of your local working directory to the code directory in the container.

    Save your Dockerfile.
    Now create a requirements.txt file  and add the following

    Django>=2.0,<3.0
    psycopg2>=2.7,<3.0

    This file will be used to install Django and psycopg2 in the docker container using the command RUN pip install -r requirements.txt.
    Save this file.

  5. Now create a file called docker-compose.yml
    This file describes the services that make your app.
    Add the following to the file

    version: '3'
    services:
      db:
        image: 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, a db service and a web service.
    The db service will pull in a postgres docker image and the web service will build using the Dockerfile already in your container, start the django server on port 8000 and expose port 8000.
    Save and close this file.

  6. We will now create the django project.
    Run the following command from your project directory

    sudo docker-compose run web django-admin startproject djangoapp .

    Compose will now build the services specified in the docker-compose file, db and web.
    When the web service image is built, Compose runs it and executes the django-admin startproject command in the container. This command instructs Django to create a set of files and directories representing a Django project.

    How to run a python django app in docker-000009

    Once Compose is finished running list the content of your project folder

    How to run a python django app in docker-000008

    You will see a new directory. This is your django project directory, created by the

    django-admin startproject command.

    Within this folder you will see your django related files.

    How to run a python django app in docker-000011

    You can see these file are all owned by root. Change the permissions on this directory by running

    sudo chown -R $USER:$USER .

    How to run a python django app in docker-000012

  7. Now that your django app is created, we need to configure it.
    We will start by connecting our postgres database.

    Within your djangoapp directory, open the settings.py file

    How to run a python django app in docker-000013

    Go to the DATABASE section

    How to run a python django app in docker-000014

    Replace what is there with

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

    How to run a python django app in docker-000015

    These settings relate to the db service in you docker-compose file.
    Save and close your settings.py file.

  8. Now lets try to run our Django app.
    Run the following command from the root of your project directory

    docker-compose up

    Once the command completes your django app should be up and connected to your postgres db.
    You should see something similar to the following.

    How to run a python django app in docker-000016

    Browse to http://localhost:8000 and you should see the django welcome screen

    How to run a python django app in docker-000017

    Thats it, you have successfully deployed django and a postgres db in docker containers using docker compose.
    To see your containers run

    docker ps

    You should see something like the following

    How to run a python django app in docker-000019

    To bring down your application run

    docker-compose down

     

    docker compose down
    This will remove your containers.

    That's it. If you found this post helpful, please leave a comment below.

Leave a Comment