How to Dockerize an Existing Django Application

What is Docker?

Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package.

Note: This article assumes you already have Docker & Docker Compose installed and know basic Django and Python development.

Create a new file called Dockerfile in your project root directory

This is simply a file that contains a list of instructions for docker to build our image. Basically it describes all the dependencies you need in your project in the dockerfile.

Add the following to your Dockerfile

# Step 1/10 Here we're creating our image from the python alpine image
FROM python:3.8-alpine

# Step 2/10 This is just to keep track or show that you own this image
MAINTAINER Chuckz Okoye

# Step 3/10 This helps avoid complications from running python in containers
ENV PYTHONUNBUFFERED=1

# Step 4/10 Here we copy our requirements.txt file to our docker image
COPY ./requirements.txt /requirements.txt

# Step 5/10 Here we install packages from the requirements file we just copied
RUN pip install -r /requirements.txt

# Step 6/10 Here we create a directory in our docker image
RUN mkdir /code

# Step 7/10 Here we set the created directory as our working directory in our docker image
WORKDIR /code

# Step 8/10 Here we copy the contents of our local code directory to our docker image
COPY . /code/

# Step 9/10 Here we create a user to run our application in docker
RUN adduser -D user

# Step 10/10 Here we switch the docker user to the user we created in step 9
USER user

Next we create a file called docker-compose.yml in your project root directory

The docker-compose.yml file is a tool for defining and running multiple services that make up our application. For example one service may be a Postgres Database and another our Django application.

Add the following to your docker-compose.yml file

version: "3"

services:
  code:
    build:
      context: .
    ports:
    - "8000:8000"
    volumes:
    - .:/code
    command: python manage.py runserver 0.0.0.0:8000

This file defines a single service called code. We set the build context to . indicating that we're running from our current directory.

In the ports config we map our project from port 8000 on our localhost to port 8000 in our image.

The volume config allows us to work synchronously with the code folder in our image in real time.

The command contains the command that is used to run our application in our docker container. age

Building and Running our Docker image

Now we can build and run our image by using the docker-compose up command in our terminal. Also to run other django commands from the container, we can use the docker-compose run followed by our command.

Conclusion

It takes some trial and error to get the working principle down, but once you do, development and deployment would definitely be easier. If you come across any issue setting this up, let me know in the comments section below 🙂

Let's keep in touch:

Blog: chuckzokoye

Twitter: @chuckzokoye

Github: github.com/tricelex

No Comments Yet