How to Connect your MySQL Database to your Dockerized Django Application

Running an example project first

In this first section, I will walk you through how you can get a running example working on your local machine. You will clone a repo I have prepared and run through the steps to get it working locally, then I will go through how I created this example.
Check out the repo here

Clone my repo

cd user/Github # (or wherever) git clone https://github.com/JamesDHW/blog-docker-django-mysql.git cd docker-django-mysql

Build and run the container

docker-compose build # build the container docker-compose up -d # run the container as a daemon
Go to localhost:8000 and you should now see a basic Django page running. That's it - everything should be connected and ready for development! Just to be sure, let's do some checks to see that everything is doing what we expect.
There are two services - web (where Django runs) and db (where MySQL runs) - which we will run a bash for each service and have a look at what's going on for each one.

Checking it works: Run the web service

# open a bash for the web container docker-compose exec web bash ping db # ping the db service.
Expected output:
... 64 bytes from docker-django-mysql_db_1.docker-django-mysql_default (172.22.0.2): icmp_seq=27 ttl=64 time=0.220 ms 64 bytes from docker-django-mysql_db_1.docker-django-mysql_default (172.22.0.2): icmp_seq=28 ttl=64 time=0.451 ms ...
This shows that your web service can talk to your db service. You can also run:
ps aux | grep mysql
to get any process that's running which contains mysql in the name - if you get output, a process has been found.
Quit the bash shell for web with cntrl+d.

Checking it works: Run the db service

# open a bash for the `db` container docker-compose exec db bash # (where `root` is username and `my-app-db` is the database name) mysql -u root -p my-app-db
type in your password (password).
You should now be connected to the mysql client for your database.
# view all the tables (should be none) SHOW TABLES # create a table (you can press enter, the command is only executed after a `;`) CREATE TABLE users ( firstName varchar(255) ); # see the table you created SHOW TABLES
Quit the bash shell for db with cntrl+d.
If you've followed until now, you should have a working example of a Django app, running in Docker and connected to a MySQL database. Read on for more details on how I created this repo and the steps you'll need to take to replicate it in your own project.

How I made this project

Now that we know this is working, let's see how I made the project from scratch!
# make new git repo mkdir DockerMySQL cd DockerMySQL git init git remote add origin... # django init new project python -m django startproject DockerMySQL

Add a Dockerfile

In the root of the project I added a Dockerfile to define the dependencies (etc.) for the container I want.
FROM python:3.6 ENV PYTHONUNBUFFERED 1 RUN pip install --upgrade pip COPY ./requirements.txt /requirements.txt RUN pip install -r /requirements.txt RUN mkdir /DockerMySQL WORKDIR /DockerMySQL COPY ./DockerMySQL /DockerMySQL

Add a docker-compose.yml

I then created a docker-compose.yml to define two services, one to run the database on (called db) and one to run the Django site on (called web).
version: '3' services: db: image: mysql:5.7 ports: - '3306:3306' environment: MYSQL_DATABASE: 'my-app-db' MYSQL_ROOT_PASSWORD: 'password' web: build: . command: python manage.py runserver 0.0.0.0:8000 volumes: - ./DockerMySQL:/DockerMySQL ports: - "8000:8000" depends_on: - db

Add a requirements.txt

I now actually define what the requirements for the Django app will be in requirements.txt.
Django==1.11.5 mysqlclient==1.3.12 django-mysql==2.2.0

In settings.py add DATABASES variable

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'my-app-db', 'USER': 'root', 'PASSWORD': 'password', 'HOST': '192.168.1.97', 'PORT': 3306, } }

Remove the default DATABASES variable

DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } }

Summary

This should have been a clear step by step how-to on creating a Django app that connects to MySQL. If you want to look at any files more specifically, you can always check the repo here. Happy coding!