The application and its database could be running on the same host or different hosts, and in Docker or not.

If the application and its database are running on different hosts, there is nothing unusual about setting this up in Docker. Configure your application with the DNS name of the database server. (I would recommend passing this via environment variables rather than modifying the settings.py file.)

Docker Compose syntax:

environment:
  MYSQL_HOST: mysql.example.com

If both are running in the same Docker setup, then Docker provides an internal DNS setup for one to reach the other. In Docker Compose, you can use the services: key as a host name; in plain Docker, you need to manually docker network create but then this trick works.

Plain Docker example:

docker network create app
docker run --net app --name mysql -v $PWD/mysql:/var/lib/mysql/data mysql
docker run --net app --name app -e MYSQL_HOST=mysql myapp

If the database is running on the same host as the application, but outside Docker, and the host is a Mac or Windows system running the Docker Desktop application, then there is a special host.docker.internal hostname

docker run -e MYSQL_HOST=host.docker.internal myapp

For a native-Linux host this shortcut doesn't exist and you need to find out the host's IP address, but then you can treat this like the first case.

Answer from David Maze on Stack Overflow
🌐
Medium
medium.com › @akshatgadodia › dockerizing-a-django-and-mysql-application-a-step-by-step-guide-d4ba181d3de5
Dockerizing a Django and MySQL Application: A Step-by-Step Guide | by Akshat Gadodia | Medium
May 2, 2024 - Next, create a docker-compose.yaml file in your project's root directory. This file defines the services and their configurations, including the Django application and MySQL database.
🌐
GitHub
github.com › DanielArian › django-mysql-docker
GitHub - DanielArian/django-mysql-docker: A ready to use Django & MySQL database on Docker
DEBUG=1 SECRET_KEY=generate-key-here # CHANGE THIS DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 0.0.0.0 [::1] DATABASE=mysql SQL_ENGINE=django.db.backends.mysql SQL_PORT=3306 SQL_DATABASE=mydb # name of the database SQL_ROOT_PASSWORD=rootpassword # CHANGE THIS SQL_USER=myuser # do not use 'root' SQL_PASSWORD=myuserpassword # CHANGE THIS SQL_HOST=db
Starred by 17 users
Forked by 13 users
Languages   Python 81.8% | Dockerfile 12.3% | Shell 5.9% | Python 81.8% | Dockerfile 12.3% | Shell 5.9%
🌐
Appliku
appliku.com › post › django-docker-tutorial-postgres
Django Docker Tutorial with PostgreSQL (2026) – Complete Guide
February 13, 2025 - Learn how to set up a Django Docker project with Postgres from scratch. Covers multi-stage Dockerfile builds, health checks, named volumes, and docker-compose.override.yml for dev vs production.
🌐
Codementor
codementor.io › community › django & docker - sqlite, mysql, and postgresql samples
Django & Docker - SQLite, MySql, and PostgreSQL samples | Codementor
November 16, 2022 - $ docker-compose run --rm appseed-app python manage.py migrate $ docker-compose run --rm appseed-app python manage.py createsuperuser ... Visit http://localhost:5085 in your browser. The app should be up & running. The last sample uses PostgreSQL for app persistence, a popular DB engine heavily used in production. ... The product can be started and used in a local environment by typing the same commands as for the MySql version:
🌐
TestDriven.io
testdriven.io › blog › dockerizing-django-with-postgres-gunicorn-and-nginx
Dockerizing Django with Postgres, Gunicorn, and Nginx | TestDriven.io
July 27, 2023 - This tutorial details how to configure Django to run on Docker along with Postgres, Nginx, and Gunicorn.
🌐
Reddit
reddit.com › r/django › how to connect dockerized django app to mysql database(phpmyadmin) on the host machine.
r/django on Reddit: How to connect dockerized Django app to Mysql database(phpmyadmin) on the host machine.
April 9, 2021 -

Hi, The title says it all. I have installed phpmyadmin which primarily use for managing databases. I have created a django app docker which trying to connect to the database on my machine it is showing this error,

 django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on '127.0.0.1' (115)") 

Here is my Dockerfile

# Dockerfile


FROM python:3.8

 install nginx

RUN apt-get update && apt-get install nginx vim -y --no-install-recommends COPY nginx.default /etc/nginx/sites-available/default RUN ln -sf /dev/stdout /var/log/nginx/access.log 
    && ln -sf /dev/stderr /var/log/nginx/error.log

 The enviroment variable ensures that the python output is set straight

 to the terminal with out buffering it first

ENV PYTHONUNBUFFERED 1

 create root directory for our project in the container

RUN mkdir /opt/app

 set working dir

WORKDIR /opt/app

ENV PORT=8080

EXPOSE 8080

 Copy the project files to working dir

COPY . /opt/app

 install dependencies,  you can change it to production.txt to deploy on the production env

RUN pip install -r requirements/development.txt

CMD ["python", "manage.py", "runserver"]

This is my database config

DATABASES = {

"default": { 'ENGINE': 'django.db.backends.mysql', 'NAME': env.str('DB_DATABASE'), 'HOST': env.str('DB_HOST'), 'USER': env.str('DB_USERNAME'), 'PASSWORD': env.str('DB_PASSWORD') } } DATABASES["default"]["ATOMIC_REQUESTS"] = True

Any help would be appreciated....

Find elsewhere
🌐
Docker
docker.com › blog › how-to-dockerize-django-app
Dockerize a Django App: Step-by-Step Guide for Beginners | Docker
September 30, 2025 - We show how to create a Docker container for your Django application, which gives you a standardized environment and makes it easier to get up and running and be more productive.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-build-a-django-and-gunicorn-application-with-docker
How to Build a Django and Gunicorn Application with Docker | DigitalOcean
October 26, 2019 - Interested in Django but not sure where to start with Docker? This tutorial will walk you through first steps: modifying a sample Django application to work …
🌐
Reddit
reddit.com › r/django › django docker best practices for dummies?
r/django on Reddit: Django Docker best practices for dummies?
September 11, 2024 -

I have created a fair few small (and one giant sprawling) Django project that are in use by small groups of consistent people (think work groups).

Up to this point, I've built sites inside python venv's and hosted with Apache mod_wsgi, all on a couple of AWS virtual machines (EC2 instances).

As I make more little Django sites, it seems like it's getting time to move into containers to keep a bit more explicit definition around package requirements/versions, transition between servers, easier local testing, etc. It seems like most tutorials out there are for toy projects on bare metal (raises hand) or using Django for Kubernetes style dynamic deployment, load balancing, etc.

Does anyone have a good resource for building / deploying relatively simple Django projects to a container for general containerization. Things like, packaging process, pros and cons of running the database in the same container / different container / bare metal, etc.

🌐
Docker Community
forums.docker.com › general
Docker can't connect my mysql container with djngo container - General - Docker Community Forums
June 23, 2024 - I have two separate container. When I am trying to connect MySQL with my Django container I am getting this error: django_farhyn | super().__init__(*args, **kwargs2) django_farhyn | django.db.utils.OperationalError: (2005, "Unknown server host 'mysql' (-2)") This docker compose file for mysql , phpmyadmin and using for others services: version: '3' services: mysql: image: mysql:8 container_name: mysql environment: MYSQL_ROOT_PASSWORD: test MYSQL_US...
🌐
Better Programming
betterprogramming.pub › dockerize-your-django-apps-428189407c69
Dockerize Your Django Apps. From local development to production | by Mariano Martinez Grasso | Better Programming
April 14, 2022 - The first step to start dockerizing the system is to identify the system components or services to be containerized. A typical architecture of a Django app in development looks like this: So we can identify two main components in the diagram: The Django app: Usually served by runserver, which is the development server packaged with Django. The database: Django support multiple databases, for example PostgreSQL, MySQL and SQLite.
🌐
GitHub
github.com › BalanPradeepKumar › docker_django_mysql
GitHub - BalanPradeepKumar/docker_django_mysql: Dockerize Django-Mysql Application with docker-compose
Dockerize Django Mysql Application using docker-compose
Starred by 4 users
Forked by 2 users
Languages   Python 97.1% | Dockerfile 2.9% | Python 97.1% | Dockerfile 2.9%
🌐
.NET Thailand
dotnetthailand.com › web-frameworks › django › docker-compose-for-django-mysql
Docker compose for Django and MySQL | .NET Thailand
We create a Dockerfile for a custom a Django image. ... We use requirements.txt for installing all Python requirement. ... CD to the root of the project where we have docker-compose.yml file.
🌐
Medium
medium.com › @minghz42 › docker-setup-for-django-on-mysql-1f063c9d16a0
Docker setup for Django on MySQL. A friend of mine had a project on… | by mhz | Medium
November 16, 2018 - version: '3' services: db: image: mysql:5.7 ports: - '3306:3306' environment: MYSQL_DATABASE: 'my-app-db' MYSQL_USER: 'root' MYSQL_PASSWORD: 'password' MYSQL_ROOT_PASSWORD: 'password' web: build: . command: python manage.py runserver 0.0.0.0:8000 volumes: - .:/my_app_dir ports: - "8000:8000" depends_on: - db · Django==1.11.5 mysqlclient==1.3.12 django-mysql==2.2.0 ...
🌐
Medium
poojan-shah-14320.medium.com › setup-mysql-in-local-with-django-docker-container-ef6096cd324d
Setup MySQL in Local with Django Docker container | by Poojan Shah | Medium
December 15, 2021 - # Database Settings Mandatory for docker-compose.yml DATABASE_HOST=127.0.0.1 # This is the database host as for local DATABASE_NAME=<databasename> # the database you created DATABSE_USER=<username> #The username of Mysql. DATABASE_PASSWORD=<password> #The password of Mysql. DATABASE_PORT=3306 ... Make sure you change the settings file to apply the .env file changes. DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': os.environ.get('DATABASE_NAME'), 'USER': os.environ.get('DATABSE_USER'), 'PASSWORD': os.environ.get('DATABASE_PASSWORD'), 'HOST': os.environ.get('DATABASE_HOST'), 'PORT': os.environ.get('DATABASE_PORT'), } }
🌐
GeeksforGeeks
geeksforgeeks.org › linux-unix › dockerizing-a-simple-django-app
How to Dockerize a Django Application? - GeeksforGeeks
July 12, 2025 - To start both the Django app and the database containers, simply run: ... This will launch both services in the right order. To stop everything, run: ... Using Docker Compose simplifies your development process by automatically managing dependencies, and it makes scaling or deploying your app easier with minimal changes to the configuration.
🌐
Docker Hub
hub.docker.com › _ › django
django - Official Image | Docker Hub
For most usages of this image, it was already not bringing in django from this image, but actually from your project's requirements.txt, so the only "value" being added here was the pre-installing of mysql-client, postgresql-client, and sqlite3 for various uses of the django framework.