Dockerize your app

How to use docker and docker compose in your app

ยท

4 min read

Dockerize your app

Dockerise your django app

Introduction

Docker is a platform that allows you to develop, ship, and run applications in containers. Containers allow a developer to package up an application with all parts it needs, such as libraries and other dependencies, and ship it all out as one package. This makes it easy to deploy and run the application on any machine that has Docker installed.

Docker compose is a tool for defining and running multi-container Docker applications.

Long are the days of it only works on my machine.๐Ÿ˜‚๐Ÿ˜‚

In this article, we will learn how to dockerize a Django application that also uses a PostgreSQL database.

Prerequisites

Install Docker and Docker Compose

To install Docker, follow the instructions on the official Docker website: docs.docker.com/get-docker To install Docker Compose, follow the instructions on the official Docker Compose website: docs.docker.com/compose/install

To install on mac run the following command:

brew install docker
brew install docker-compose

Install Django

Follow more instructions on the official Django website: djangoproject.com/download

To install Django, run the following command:

python -m pip install Django==5.1.4

Create a Django project

To create a Django project, run the following command:

django-admin startproject dockerhelloworld

Create a Django app

To create a Django app, run the following command:

cd dockerhelloworld
python manage.py startapp helloworld

Create a view

To create a view, open the views.py file in the helloworld app and add the following code:

from django.http import HttpResponse

def hello(request):
    return HttpResponse("Hello, Dockerized World!")

Create a URL pattern

To create a URL pattern, open the urls.py file in the helloworld app and add the following code, create the file if it does not exist:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.hello, name='hello'),
]

Update the project URL configuration

To update the project URL configuration, open the urls.py file in the dockerhelloworld project and add the following code:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('helloworld.urls')),
]

Run the Django server

To run the Django server, run the following command:

python manage.py runserver

Test the Django server

To test the Django server, open a web browser and navigate to http://localhost:8000/. You should see the message Hello, Dockerized World!.

Create a requirements.txt file

To create a requirements.txt file, run the following command:

pip freeze > requirements.txt

Dockerize the Django application

Create a Dockerfile

To create a Dockerfile, open the root directory of the Django project and create a file named Dockerfile with the following content:

# Use an official Python runtime as a parent image
FROM python:3.12

# Set the working directory in the container
WORKDIR app

# Copy the current directory contents into the container at /app
COPY . /app

# Copy the requirements file into the container 
COPY requirements.txt /tmp/requirements.txt

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make port 8000 available to the world outside this container
EXPOSE 8000

# Define environment variables if needed

# Run app when the container launches
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]

Create a docker-compose.yml file

To create a docker-compose.yml file, open the root directory of the Django project and create a file named docker-compose.yml with the following content:

version: '3.8'

services:
  db:
    image: postgres:13
    container_name: docker-hello-db
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    volumes:
      - pg_data:/var/lib/postgresql/data

  app:
    build:
        context: .
        dockerfile: ./Dockerfile
    container_name: docker-hello-app
    ports:
      - "8000:8000"
    depends_on:
      - db
    volumes:
      - .:/app/
    environment:
      - DB_HOST=db
      - DB_NAME=postgres
      - DB_PASSWORD=postgres
      - DB_USER=postgres
      - DJANGO_DEBUG=True


volumes:
  pg_data:

Build and run the Docker containers

To build and run the Docker containers, run the following command:

docker-compose up --build

Test the Dockerized Django application

To test the Dockerized Django application, open a web browser and navigate to http://localhost:8000/.

You should see the message Hello, Dockerized World!.

Conclusion

In this article, we learned how to dockerize a Django application that uses a PostgreSQL database. Dockerizing your Django application allows you to easily deploy and run your application on any machine that has Docker installed.

The next steps could be to deploy the Dockerized Django application to a cloud provider like AWS, Google Cloud, or Azure.

ย