Docker compose for Django and MySQL
To use Django and MySQL Docker compose, we need to create these required files and add some contents to them.
- Dockerfile for a custom docker image
- requirements.txt
- docker-compose.yml
- .env file
Dockerfile
- We create a Dockerfile for a custom a Django image.
- Example content of
Dockerfile
# DockerfileFROM python:3.9-alpine3.14# Set Python output is sent straight to terminal to see the output in realtime.ENV PYTHONUNBUFFERED=1WORKDIR /codeCOPY requirements.txt /code/RUN pip install -r requirements.txt
requirements.txt
- We use requirements.txt for installing all Python requirement.
- Example content of
requirements.txt
Django>=3.0,<4.0mysqlclient>=2.0.3
docker-compose.yml
- Example content of docker-compose.yml
# docker-compose.ymlversion: "3.9"services:web:container_name: webbuild: .# TODO remove hard code sleep, it should use short sleep retry in entrypoint.shcommand: bash -c "sleep 15 && python manage.py runserver 0.0.0.0:8000"volumes:- .:/code # mount current folder to /code folder in side a containerports:- 8000:8000depends_on:- dbnetworks:- compose_networkdb:container_name: mysql-serverimage: mysql:8.0restart: alwaysenvironment:MYSQL_ROOT_PASSWORD_FILE: "/run/secrets/mysql_root_password"MYSQL_USER: my-userMYSQL_PASSWORD_FILE: "/run/secrets/mysql_password"MYSQL_DATABASE: my-dbports:- 3306:3306secrets:- mysql_root_password- mysql_passwordhealthcheck:test: mysqladmin ping -h localhost -u $$MYSQL_USER --password=$$(cat /run/secrets/mysql_password)timeout: 10sretries: 10volumes:# Use name volumes, managed by Docker for local development- mysql-data:/var/lib/mysql- mysql-logs:/var/log/mysqlcommand:[--character-set-server=utf8mb4,--collation-server=utf8mb4_unicode_ci,--lower-case-table-names=1,--default-authentication-plugin=mysql_native_password]networks:- compose_network# https://docs.docker.com/compose/compose-file/compose-file-v3/#external-1volumes:mysql-data:mysql-logs:# https://serverfault.com/questions/871090/how-to-use-docker-secrets-without-a-swarm-clustersecrets:mysql_root_password:file: ./mysql_root_password.txtmysql_password:file: ./mysql_password.txtnetworks:compose_network:
docker secrets
- Create docker secret files to be used for database passwords
echo "MySQL1234\!" > mysql_root_password.txtecho "pa\$\$w@rd" > mysql_password.txt
.env file
- Explicit prefix volume/network name with this value or use -P with docker run
- More details https://docs.docker.com/compose/reference/envvars/#compose_project_name
# .envCOMPOSE_PROJECT_NAME=django
File structure of our Django Docker compose
$ tree . -a├── .env├── Dockerfile├── docker-compose.yml└── requirements.txt
Create a new Django project
- CD to the root of the project where we have docker-compose.yml file.
- Launch a web container and run
django-admin
to create a new Django project by running the following commands:
$ projectName=my_django_site$ docker-compose run web django-admin startproject $projectName . // docker compose run if you use compose v2
- This will create a new Django project to a current working directory
- Optionally, you can change a project name to any name that you like.
Change files permission
- At the root of project, execute the following command.
$ sudo chown -R $USER:$USER .
- Then checking files permission to make sure that the current user must have ownership of all files.
$ ls -l
Update a database connection
- Open
my_django_site/settings.py
file with your text editor. - Update
DATABASES
as the following:.
# my-django-site/settings.pyDATABASES = {'default': {'ENGINE': 'django.db.backends.mysql','HOST': 'db','PORT': 3306,'NAME': 'my-db','USER': 'my-user','PASSWORD': 'pass@rd',}}
Launch a website
- Run the following command to launch web and db containers.
$ docker-compose up # docker compose up if you use compose v2
Stop and remove containers
- To stop containers, press
ctrl + c
. - To remove containers and all volumes, run
docker-compose down ---volumes or docker compose down ---volumes if you use compose v2
Useful resources
Loading comments...