Docker Container: Centos 7, Apache, mod_wsgi, and Python 3

September 20, 2021

On this post we are creating a Docker Container with Linux Centos 7, Apache Web Server, and mod_wsgi for web development with Python 3.

Dockerfile

Create a file named Dockerfile and copy the following content on the file.


FROM centos:7

# Install Apache
RUN yum -y update
RUN yum -y install httpd httpd-tools

# Install Python 3
RUN yum -y install python3

# Install mod_wsgi
RUN yum -y install mod_wsgi

# Update Apache Configuration
RUN sed -E -i -e '/<Directory "\/var\/www\/html">/,/<\/Directory>/s/AllowOverride None/AllowOverride All/' /etc/httpd/conf/httpd.conf

RUN echo "" >> /etc/httpd/conf/httpd.conf \
 && echo "WSGIScriptAlias / /var/www/html/wsgi.py" >> /etc/httpd/conf/httpd.conf \
 && echo "<Directory /var/www/html>" >> /etc/httpd/conf/httpd.conf \
 && echo "<Files wsgi.py>" >> /etc/httpd/conf/httpd.conf \
 && echo "Require all granted" >> /etc/httpd/conf/httpd.conf \
 && echo "</Files>" >> /etc/httpd/conf/httpd.conf \
 && echo "</Directory>" >> /etc/httpd/conf/httpd.conf

EXPOSE 80

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

You can download this Dockerfile, and the python script described below, as a zipped file here


  • We start installing the Apache web server, which correspond to the packages httpd and httpd-tools.
  • After that, we install Python. We use the default Python version 3 that comes with Centos 7.
  • To use Python for web development, we install the Apache module mod_wsgi.
  • Then we update the Apache configuration on the file httpd.conf.

Create the Docker Image and Container

Now we create the Docker Image.


docker build -t image_apache_python .

Then we create the Docker Container.

Notice that we need to indicate the path of our local folder that will be served as the root of the Apache Web Server, which in this example is /path_to/my_website. Replace it with the location of your site's root folder.


docker run -tid -p 4000:80 --name=container_apache_python -v /path_to/my_website:/var/www/html image_apache_python

Python script

Now we need to create a python script that will be run when we open our website on a browser.
Create a file named wsgi.py and copy the following content on the file.

wsgi.py


def application(environ, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"]

Copy the Python script onto Docker container

The python script created above have to be copied into the root folder of our website.

The copy can be done in one of two ways.

1. Open a terminal window and copy the file to the local folder that you selected as the site's root folder:


copy wsgi.py /path_to/my_website

Or

2. On the terminal window, using the Docker cp command, copy the file directly to the site's root folder on the container:


docker cp wsgi.py container_apache_python:/var/www/html/

Test

Then we can test our site by opening the url http://localhost:4000/ on your browser.

You would see the text "Hello World" on your browser.


Downloads

Dockerfile-apache-python.zip