Django / MySQL with Docker  – A step by step guide for local development part 1

A short introduction

What is ..?

1. Django

Django is a Python-based Web framework that encourages rapid development and clean, pragmatic design. It is a free and open-source web framework. Some popular sites that utilize Django include PBS, Instagram, Disqus, Washington Times, Bitbucket and Mozilla. If you’re new to Django, I would recommend you to start here.

2. MySQL

MySQL is an open source relational database management system (RDBMS) that uses Structured Query Language (SQL). SQL is the most popular language for creating, accessing and manipulating content in a database. MySQL is one of the most commonly used database systems on the web around the world. You can learn more about MySQL here.

3. Docker

Docker is a tool that automates the deployment of an application inside a software container. It is open-source and designed to make it easier to create, deploy, and run applications by utilizing containers. Containers allow a developer to package up an application with any of the components it needs, such as libraries and other dependencies, and ships it out as one package. This would allow it to run on any machine. Docker is a bit like a virtual machine. You can read more about Docker here.

Well, that was not a short one.

Ok, let us get to business.

Installation

First, you need to make sure to install Docker. You could do it from here. In the left pane choose your OS.

You can confirm the installation by

docker run hello-world

This would output

$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client,
which sent it to your terminal.

To try something more ambitious, you can run an Ubuntu container
with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Some useful docker commands

Gets the logs from the container

docker logs -f <CONTAINER_NAME>

List containers

docker ps -a

Stop the container

docker stop <CONTAINER_NAME>

Starts the container

docker start <CONTAINER_NAME>

To configure Docker we create 2 files “Dockerfile” and “docker-compose.yml“.

Create a new file called “Dockerfile” and add the following to it.

# Pull the base image
FROM python:3

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN mkdir /code
WORKDIR /code
#Upgrade pip
RUN pip install pip -U
ADD requirements.txt /code/
#Install dependencies
RUN pip install -r requirements.txt
ADD . /code/

This simply means.

  • FROM python:3.7-slim use an official Docker image for Python 3.7.
  • ENV PYTHONDONTWRITEBYTECODE 1 Python won’t try to write .pyc files which we do not want.
  • ENV PYTHONUNBUFFERED 1 This ensures our console output looks familiar and is not buffered by Docker, which we don’t want.

Create a new file called “docker-compose.yml” and add the following to it.

version: "3"
services:
  app:
    restart: always
    build: . # Current directory!
    command: "python3 manage.py runserver 0.0.0.0:8000"
    volumes:
      - .:/code
    ports:
      - "8000:8000"

You need to have a Django application first in the directory which you have the Dockerfile. For more information on how to setup a basic Django application go here

Now run the command

docker-compose up  -d

This would start building the image and run it in detached mode. Sit back and have a cup coffee as this is going to take a bit of time. It should show something like below.

Starting django_app_1 ... done

At this point, your Django app should be running at port 8000 on your Docker host. On Docker for Mac and Docker for Windows, go to http://localhost:8000 on a web browser to see the Django welcome page. If you are using Docker Machine, then docker-machine ip MACHINE_VM returns the Docker host IP address, to which you can append the port (:8000).

docker-machine ip <MACHINE_NAME>

Navigate to this ip using your browser with port 8000. You should see the Django start screen.

You can now give yourself a tap on your shoulder. You have successfully run Django app in Docker.

In the next part, we will add a MySQL Database to this setup.