~/python/django-how-build-docker-image-run-web-service-apache-python-39.md

Django Python - How to Build Docker Image and Run Web-service on Apache with Python 3.9

2 min readMay 20, 2022by Gorav Singal#python#django
// TL;DR

Create a multi-stage Docker build for a Django project using a base image with Apache and Python 3.9, configure mod_wsgi, and run the web service in a container.

Django Python - How to Build Docker Image and Run Web-service on Apache with Python 3.9

Introduction

So you have a Django project, and want to run it using docker image. We will run it on Apache and Python 3.9.

Make sure, you have a running Django project, before proceeding.

Parent Docker Image

First we will create a base image, where we will install following:

  • Apache
  • Python 3.9
FROM centos:7

USER root

RUN yum makecache fast \
    && yum -y install epel-release \
    && yum -y update \
    && yum groupinstall "Development Tools" -y \
    && yum install openssl-devel libffi-devel bzip2-devel -y \
    && yum install httpd httpd-devel mod_ssl wget -y \
    && yum clean all \
    && rm -rf /var/cache/yum

RUN wget https://www.python.org/ftp/python/3.9.10/Python-3.9.10.tgz \
    && tar xvf Python-3.9.10.tgz \
    && cd Python-3.9.10/ \
    && ./configure --prefix=/usr/local --enable-optimizations --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib" \
    && make altinstall \
    && python3.9 --version \
    && python3.9 -m pip install --upgrade pip \
    && pip3.9 install virtualenv

Build this image,

docker build -t django_base . 

Django App Image

# Using multi-stage builds to avoid showing secrets in docker history
FROM python38_test as intermediate

WORKDIR /var/www/html/
COPY . /var/www/html/

RUN virtualenv env --python python3.9 \
    && source /var/www/html/env/bin/activate \
    && python3.9 -m pip install -r /var/www/html/requirements.txt \
    && python3.9 -m pip install mod_wsgi \
    && virtualenv env --python python3.9 \
    && source /var/www/html/env/bin/activate \
    && mod_wsgi-express install-module 

COPY ./docker/httpd.conf /etc/httpd/conf/httpd.conf
COPY ./docker/sites-enabled /etc/httpd/sites-enabled

ENV CONFIG "/var/www/html"
EXPOSE 8080
USER root

CMD  ["/usr/sbin/httpd","-D","FOREGROUND"]

Build this image.

Note, if you are installing pip modules from a private artifactory, See other post on How to securely create docker image

Run Django App

Now, just pass your required environment variables to your app and run. It will run on port 8080. You can use this image either on Kubernetes or regular docker run environment.

Thanks for reading

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Introduction This post has the complete code to send email through smtp server…

Nodejs with MongoDB - Number of Opened Connections Keep on Increasing with Mongoose Library

Nodejs with MongoDB - Number of Opened Connections Keep on Increasing with Mongoose Library

Introduction In one of my app, I was using to talk to . I have used some event…

Python Build Quality Gates with Pylint and Unittest Coverage Thresholds

Python Build Quality Gates with Pylint and Unittest Coverage Thresholds

Introduction It is very important to introduce few process so that your code and…

Python - How to Implement Timed-Function which gets Timeout After Specified Max Timeout Value

Python - How to Implement Timed-Function which gets Timeout After Specified Max Timeout Value

Introduction We often require to execute in timed manner, i.e. to specify a max…

How to Solve Circular Import Error in Python

How to Solve Circular Import Error in Python

Introduction To give some context, I have two python files. (Both in same folder…

Python Code - How To Read CSV with Headers into an Array of Dictionary

Python Code - How To Read CSV with Headers into an Array of Dictionary

Introduction Lets assume we have a csv something similar to following: Python…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…