i solved my problem

i used IPv4Address of mysql container like 172.17.0.3

i ran command docker network inspect bridge and check IPv4Address

and insert my settings.py like this

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'testdb',
    'USER': 'root',
    'PASSWORD': 'test',
    'HOST': '172.17.0.3', # <---- mysql container IPv4Address
    'PORT': '3306',
    'OPTIONS': {'auth_plugin': 'mysql_native_password'},
    }
}

and its work!

Answer from Jade Han on Stack Overflow
🌐
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 ...
🌐
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%
Discussions

how can i connect django container to mysql container - Stack Overflow
Hi i want to connect django - mysql in each container :) i tried a lot of way but failed.. here is my mysql docker-compose.yaml version: "3" services: db: platform: linux/x86_64 ... More on stackoverflow.com
🌐 stackoverflow.com
python - Dockerize a Django app with a MySQL container - Stack Overflow
I have an app developed in Django (2.2.7) with python (3.8.0), Docker (19.03.5) and docker-compose (1.25.2) running in Windows 10 pro. I want to Dockerize it with changing the sqlite3 database for a More on stackoverflow.com
🌐 stackoverflow.com
February 7, 2020
Linking django and mysql containers using docker-compose - Stack Overflow
I've been following with the docker-compose tutorial here (linking django and postgres container). Although I was able to go through with the tutorial I'm however not able to proceed with repeating... More on stackoverflow.com
🌐 stackoverflow.com
March 7, 2017
How to connect dockerized Django app to Mysql database(phpmyadmin) on the host machine.
You can't connect to the host database because Docker container isolation prevents this from happening. Docker configured the firewall (iptables) to prevent access from containers to the host. The most reliable solution is to use the "network: host" mode, which skips all Docker isolation features by using the host network adapter directly. Then you can talk to localhost like you would if you install phpmyadmin locally. Running services in the host OS that need to be consumed by a Docker enviroment is a bit of an anti-pattern because Docker is design to use Docker internal networks (the default bridge mode) so you can run multiple similar containers in a single Docker host ( for horizontal scaling) More on reddit.com
🌐 r/django
3
1
April 9, 2021
🌐
DEV Community
dev.to › foadlind › dockerizing-a-django-mysql-project-g4m
Dockerizing a Django + MySQL project - DEV Community
March 3, 2021 - In this guide I show you how to run your Django app in one Docker container and your MySQL database in another and make them talk to each other.
🌐
Doprax
doprax.com › tutorial › deploy-django-using-docker-nginx-mysql
Deploy Django on Docker [the ultimate guide]
Build and deploy applications with scalable virtual machines and an extensive app market. Power to run your code with Doprax's cloud platform.
🌐
YouTube
youtube.com › random code
How to connect Django to MySQL in a Docker Container - YouTube
Based on a comment request I wanted to showcase how we can connect a Django project to a MySQL database inside a docker container. In this video I run my doc...
Published   October 28, 2022
Views   14K
🌐
Jameshw
jameshw.dev › blog › 2021-09-19 › connect-mysql-to-docker-and-django
How to Connect your MySQL Database to your Dockerized Django Application
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: .
Find elsewhere
🌐
GitHub
github.com › DanielArian › django-mysql-docker › blob › main › docker-compose.yml
django-mysql-docker/docker-compose.yml at main · DanielArian/django-mysql-docker
A ready to use Django & MySQL database on Docker. Contribute to DanielArian/django-mysql-docker development by creating an account on GitHub.
Author   DanielArian
🌐
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.
🌐
YouTube
youtube.com › watch
Docker Compose | How to configure PostgreSQL or MySQL database with Python Django - YouTube
In this Docker compose tutorial we start a Django application with Docker Compose and run it in a container. I will then show you how to configure a volume t...
Published   April 3, 2021
🌐
GitHub
github.com › dakrauth › docker-django-mysql
GitHub - dakrauth/docker-django-mysql
A very simplistic demo of composing MySQL and Django containers. $ git clone https://github.com/dakrauth/docker-django-mysql.git $ cd docker-django-mysql $ docker-compose up -d db $ docker-compose exec db mysql -u root -p -e 'CREATE DATABASE `ddm` DEFAULT CHARACTER SET = `utf8mb4`' $ docker-compose up -d app $ docker-compose exec app python app.py createsuperuser
Starred by 7 users
Forked by 6 users
Languages   Python 81.0% | HTML 12.4% | Shell 6.6% | Python 81.0% | HTML 12.4% | Shell 6.6%
🌐
Bluethinkinc Blog
blog.bluethinkinc.com › home › dockerizing a django + mysql project
Dockerizing a Django + MySql project - Bluethinkinc Blog
May 20, 2025 - In this guide I show you how to run your Django app in one Docker container and your MySQL database in another and make them talk to each other.
🌐
.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.
Top answer
1 of 2
2

Hi i think this answer helps you ##1.- Reset all your migrations

find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete

##2.- See and apply your migrations again

python manage.py showmigrations
python manage.py makemigrations
python manage.py migrate
2 of 2
0

You should not reset your migrations unless you want to wipe all of the data completely and start over. Your migrations should exist beforehand. So if you dont mind about old migrations you can delete them and use python manage.py makemigrations and then proceed to following steps:

So if your applications starts at the moment, we need to update your docker-compose file in a way that it uses entrypoint.sh. An ENTRYPOINT allows you to configure a container that will run as an executable. First things first, create your entrypoint.sh file on the same level as docker-compose.yaml. Next, don't forget to add chmod +x entrypoint.sh so entrypoint can be executed

entrypoint.sh file:

#!/bin/bash

set -e

echo "${0}: running migrations."
python manage.py migrate --noinput

echo "${0}: collecting statics."

python manage.py collectstatic --noinput
python manage.py runserver 0.0.0.0:8000

Afterwards update your docker-compose.yaml file. Change your command line to:

command:
  - /bin/sh
  - '-c'
  - '/code/entrypoint.sh'

Additionally you should store all of your pip requirements in requirements.txt file and in your Dockerfile you should run pip install -r requirements.txt

You can dump your pip requirements with a command pip freeze > requirements.txt

🌐
Skonik
skonik.me › setup-django-with-mysql-using
Setup Django with Mysql using docker - Sergei Konik's Blog
October 15, 2021 - I encountered some errors during mysql setup with django using docker. So, this is why I created a quick reference for myself. -------------------------------------------------------------------------------- Put this into your settings.py file.
🌐
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....

🌐
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...
🌐
YouTube
youtube.com › watch
Docker Compose for Django & MySQL: Step-by-Step Guide - YouTube
In this tutorial, I show you how to set up a Django web application with a MySQL database using Docker Compose. You’ll learn how to create and configure a do...
Published   September 22, 2024