How To: Deploy Django using Docker & MySQL

Haris Bin Nasir Avatar

·

·

During the setup of MySQL with Django using Docker, I ran into few issues. As a result, I decided to write this article to help people trying to figure out on how to deploy a Django app using Docker and MySQL.

MySQL is a relational database management system that is free and open-source. “My” is the name of co-founder Michael Widenius’s daughter, and “SQL” is the acronym for Structured Query Language.

Docker is a group of platform as a service tools that distribute software in containers using OS-level virtualization. There are two tiers to the service: free and premium. Docker Engine is the program that runs the containers. Docker, Inc. founded it in 2013 and continues to develop it.

Deploy Django App In Docker Container Alongside MySQL

Update the settings.py file with the following code.

Copied!
ATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': os.environ.get('MYSQL_DATABASE', 'mysql-db'), 'USER': os.environ.get('MYSQL_USER', 'mysql-user'), 'PASSWORD': os.environ.get('MYSQL_PASSWORD', 'mysql-password'), 'HOST': os.environ.get('MYSQL_DATABASE_HOST', 'db'), 'PORT': os.environ.get('MYSQL_DATABASE_PORT', 3306), } }

Install the mysql client package using pip.

Copied!
pip install mysqlclient

Create a .env file for environment variables, this file will be used by Docker to setup the enviroment variable for your container.

Copied!
MYSQL_DATABASE=mysql-db MYSQL_USER=mysql-user MYSQL_PASSWORD=mysql-password MYSQL_ROOT_PASSWORD=mysql-root-password MYSQL_DATABASE_HOST=db MYSQL_DATABASE_PORT=3306

Create the docker/mysqldirectory and the Dockerfile and init_db.sh files in your project directory.

Place the following code inside of Dockerfile

Copied!
FROM mysql/mysql-server:8.0 COPY ./docker/db/init_db.sh /docker-entrypoint-initdb.d/ RUN chmod +x /docker-entrypoint-initdb.d/init_db.sh

Workaround for getting rid of issues while running Django tests, place the following code inside of init_db.sh file.

Copied!
#!/bin/bash mysql -u root --password="$MYSQL_ROOT_PASSWORD" << EOF USE ${MYSQL_DATABASE}; GRANT ALL PRIVILEGES ON test_${MYSQL_DATABASE}.* TO '${MYSQL_USER}'; EOF

Because Django creates a database with the prefix test on each test run, the script above is required in order to execute tests. The mysql image entrypoint runs files in the docker-entrypoint-initdb.ddirectory when you copy the file into your image.

Make a docker-composefile file.

Copied!
version: "3.8" services: mysql-db: build: context: . dockerfile: ./docker/mysql/Dockerfile volumes: - mysql_data:/var/lib/mysql restart: unless-stopped env_file: .env ports: - "3306:3306" backend: build: context: . dockerfile: docker/django/Dockerfile env_file: .env restart: always volumes: - .:/app/ depends_on: - mysql-db working_dir: "/app/mysql_example" expose: - 8000 ports: - "8000:8000" command: ["python", "manage.py", "runserver", "0.0.0.0:8000"] volumes: mysql_data:

That’s it. You are all done.

Conclusion

In this article, we learned how to deploy a Django web app using a docker container that also includes mysql inside of it as a database for our application. As always, If you have found this article useful do not forget to share it and leave a comment if you have any questions. Happy Coding

Leave a Reply

Your email address will not be published. Required fields are marked *