My first Django app in Docker

· 602 words · 3 minute read

In the part I, we have explored the key concepts related to containerisation and Docker. We’ve also created the simplest container possible and made it run a very simple script that prints "Hello, World" to the terminal.

Today we are going to create a very simple Django application. This will be the occasion to add a couple of things to our learning list:

  • how to manage requirements with Docker
  • how to tag your Docker image to a specific version
  • how to expose (or publish) Docker containers ports to the host

If you haven’t checked the first tutorial and if you are not familiar with Docker at all, you can check it out here. You don’t need to be familiar with Django for this tutorial but if you are not familiar with it and if you are curious, you should definitely go through the Django tutorial.

Create the Django project 🔗

Let’s run the following in your terminal

# create a demo directory and change to it
mkdir demo && cd demo

# create a virtualenv and activate it
virtualenv ~/.virtualenvs/demo && source ~/.virtualenvs/demo/bin/activate

# install Django and start a Django project
pip install Django
django-admin startproject djdocker && cd djdocker

# write the current requirements to a requirements file
pip freeze > requirements.txt

# create a Dockerfile
touch Dockerfile

I have added comments along the way but don’t hesitate to take a few minutes to understand each command if you need to. We have basically created the simplest Django app possible (an empty one) and a requirements file for our managing our dependencies. That’s all we need for the moment!

Time for the Dockerfile 🔗

Open your Dockerfile in a text editor and write the following:

# Use python 3.7 as a parent image
FROM python:3.7

# Set the working directory to /app
WORKDIR /app

# Copy the local directory content into the container at /app
ADD . /app

# Install the packages specified in requirements.txt in the container
RUN pip install --trusted-host pypi.python.org -r requirements.txt

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

If you have checked the part I, you should be familiar with most of the Dockerfile instructions. The new thing is that we now run pip install within the Docker file. By this very simple command, you can now install the requirements from your requirements file onto the container and obtain a 100% predictable environment (the one you we’ve just created above!).

The last step in the Dockerfile will run your Django app in the container at the port 8000.

Building and running the image 🔗

From your command line, you can then build your image and run it:

# build your docker image and tag it as demo
docker build -t demo:0.1 .

# run the image and map TCP port 8000 in the container to port 80 on your machine
docker run -p 80:8000 demo:0.1

As you can see, you can simply add versioning to your container image by separating it with a colon (:). You can then run the image you want by specifying the image name with the version.

The -p option is a shortcut for -publish and allows you to map your container’s port(s) to your machine port(s), in the following order {hostPort}:{containerPort}.

You can now go to the following address in your browser: http://localhost/. If everything went well, you should see a default Django page saying “The install worked successfully! Congratulations!”.

Conclusion 🔗

Indeed… congratulations… for running your first Django app in a Docker container!

In the next article, we’ll use docker-compose for running a multi-container Docker application. See you there!