mysqlclient has native dependencies that must be installed before you can pip install it. When running in docker, and especially in alpine, you probably want to switch over to using mysql-connector-python which is a pure python library that does not have any native dependencies l,ike mysqlclient. Update your requirements file and update your settings to use mysql.connector.django if you want to use mysql-connector-python.

Answer from 2ps 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 - Django==1.11.5 mysqlclient==1.3.12 django-mysql==2.2.0 ... (whatever else your app requires) ... ...DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'my-app-db', 'USER': 'root', 'PASSWORD': 'password', 'HOST': 'db', 'PORT': 3306, } }... ... That should build the images defined in the docker-compose.yml file.
Discussions

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 MySQL database. I've already write this Dockerfile: FROM python:3.7 ENV PYTHONUNBUFFERED 1 RUN mkdir /code WORKDIR /code ADD . /code/ RUN pip install --upgrade pip && pip install -r requirements.txt RUN pip install mysqlclient ... More on stackoverflow.com
🌐 stackoverflow.com
Getting "django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on 'db' (115)")" when running Django app on Docker container trying to connect to another Docker container running MySQL
I see you installed mysqlclient via the Linux side, but did you also install via pip? More on reddit.com
🌐 r/django
5
1
September 11, 2023
Cannot connect to local MySQL server in docker container with django
I am building a django project in docker with mysql. However, I met some errors. Here is my docker-compose.yaml file: version: "3" services: mysql8: platform: linux/amd64 container_name: ... More on github.com
🌐 github.com
1
November 18, 2023
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
🌐
Roy Tutorials
roytuts.com › home › python › django › docker compose – dockerizing django mysql app
Docker Compose - Dockerizing Django MySQL App - Roy Tutorials
October 26, 2023 - Next you need to create a Dockerfile under your djangomysql directory with the following content. FROM python:3.11.5-alpine #FROM python:3.8.5-alpine COPY . /app WORKDIR /app RUN apk add --no-cache mariadb-connector-c-dev RUN apk update && apk add python3 python3-dev mariadb-dev build-base && pip3 install mysqlclient ...
🌐
Jameshw
jameshw.dev › blog › 2021-09-19 › connect-mysql-to-docker-and-django
How to Connect your MySQL Database to your Dockerized Django Application
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 · 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
🌐
GitHub
github.com › docker-library › django › blob › master › 3.4 › Dockerfile
django/3.4/Dockerfile at master · docker-library/django
ENV DJANGO_VERSION 1.10.4 · · RUN pip install mysqlclient psycopg2 django=="$DJANGO_VERSION"
Author   docker-library
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

🌐
DevPress
devpress.csdn.net › mysqldb › 62e901e36484667128339900.html
Dockerize a Django app with a MySQL container_weixin_0010034-MySQL数据库
August 2, 2022 - mysqlclient==2.0.1 · django-mysql==3.8.1 · As you can see, django itself is registered here, as well as packages for communicating with the mysql database. Next, we will use this file to configure the contents of the image. In the web directory, create a file called Dockerfile.
Find elsewhere
🌐
Reddit
reddit.com › r/django › getting "django.db.utils.operationalerror: (2002, "can't connect to mysql server on 'db' (115)")" when running django app on docker container trying to connect to another docker container running mysql
r/django on Reddit: Getting "django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on 'db' (115)")" when running Django app on Docker container trying to connect to another Docker container running MySQL
September 11, 2023 -

Hi all,

I am trying to run a simple Django app on Docker with one container for the MySQL server and another for the actual app. However, when I start the app using 'docker compose up', I get the error in the title.

Here is my DATABASES section in settings.py:

DATABASES = {'default': {'ENGINE': 'django.db.backends.mysql','NAME': 'dockertest','USER': 'mydatabaseuser','PASSWORD': 'mypassword','HOST': 'db','PORT': '3306',}}

I am able to connect to the MySQL database (dockertest) from the container running Django with the credentials specified in settings.py, so that means all the Docker networking should be working fine and the credentials are valid. I do this with the command mysql -h db -u mydatabaseuser -p, then entering the password. The port 3306 should also be correct as I did not specify otherwise in the docker compose file.

I also have a phpmyadmin Docker container that runs with the other containers and it is able to connect to the MySQL server and interact with the database.

Here is my docker-compose.yml:

version: '3'
services:
web:
depends_on:
- db
networks:
- my-network
build: .
command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]
volumes:
- .:/app
ports:
- "8000:8000"

db:
image: mysql:latest
container_name: my-mysql
environment:
MYSQL_ROOT_PASSWORD: my-secret-pw
MYSQL_DATABASE: dockertest
MYSQL_USER: root
MYSQL_PASSWORD: my-secret-pw
volumes:
- C:/Users/Aaron Liu/Documents/testdb:/var/lib/mysql
networks:
- my-network
ports:
- "3306:3306"
phpmyadmin:
depends_on:
- db
image: phpmyadmin/phpmyadmin
container_name: my-phpmyadmin
environment:
PMA_HOST: db
PMA_USER: mydatabaseuser
PMA_PASSWORD: mypassword
ports:
- "8080:80"
networks:
- my-network
networks:
my-network:
driver: bridge

and here is my Dockerfile:

# Use an official Python runtime as a parent image
FROM python:3.8-slim-buster
# Ensure system packages are up to date and install the MySQL client
RUN apt-get update \
&& apt-get install -y default-libmysqlclient-dev gcc \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Set environment variables for Python
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set the working directory in the container to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app/
RUN apt-get update && apt-get install -y default-libmysqlclient-dev libssl-dev && apt install -y default-mysql-client
RUN apt-get update && apt-get install -y pkg-config

# Install any needed packages specified in requirements.txt
RUN pip install --upgrade pip \
&& pip install -r requirements.txt

Any help is greatly appreciated!

🌐
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 · Run docker compose --env-file .env.dev up --build ·
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%
🌐
Medium
medium.com › @vishamay501 › containerizing-a-django-application-with-mysql-and-nginx-using-docker-8929224d5c76
🚀 Containerizing a Django Application with MySQL and Nginx Using Docker | by vishamay radadiya | Medium
August 12, 2025 - Our Django app needs to be containerized. Here’s the Dockerfile: FROM python:3.9 WORKDIR /app/backend COPY requirements.txt /app/backend RUN apt-get update && apt-get upgrade -y \ && apt-get install -y gcc default-libmysqlclient-dev pkg-config \ && rm -rf /var/lib/apt/lists/* RUN pip install --upgrade pip RUN pip install mysqlclient ...
🌐
.NET Thailand
dotnetthailand.com › web-frameworks › django › docker-compose-for-django-mysql
Docker compose for Django and MySQL | .NET Thailand
To use Django and MySQL Docker compose, we need to create these required files and add some contents to them.
🌐
GitHub
github.com › docker-library › mysql › issues › 1012
Cannot connect to local MySQL server in docker container with django · Issue #1012 · docker-library/mysql
November 18, 2023 - My Dockerfile: FROM python:3.10-buster RUN /usr/local/bin/python -m pip install --upgrade pip -i https://mirrors.aliyun.com/pypi/simple RUN mkdir /code WORKDIR /code COPY requirements-prd.txt /code/ RUN pip install -i https://mirrors.aliyun.com/pypi/simple -r requirements-prd.txt COPY . /code/ The corresponding requirements-prd.txt: Django==3.1.3 pymysql mysqlclient mysql-connector-python ·
Author   aMaverickDragonfruit
🌐
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%
🌐
MySQL
forums.mysql.com › read.php
Connect to MySQL using Django and Docker
April 22, 2021 - Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party
🌐
Medevel
medevel.com › deploying-a-django-application-with-mysql-using-docker-compose
Deploying a Django Application with MySQL Using Docker Compose
August 4, 2024 - Django==4.0 mysqlclient · Create a docker-compose.yml File: version: '3' services: db: image: mysql:5.7 restart: always environment: MYSQL_ROOT_PASSWORD: password MYSQL_DATABASE: mydatabase ports: - "3306:3306" volumes: - mysql_data:/var/lib/mysql web: build: .
🌐
Medium
medium.com › @muskanpokhriyal2911 › deploying-a-django-web-application-with-nginx-and-mysql-905463062adf
Deploying a Django web application with NGINX and MYSQL | by Muskan Pokhriyal | Medium
June 17, 2025 - && apt-get install -y gcc default-libmysqlclient-dev pkg-config \ # installs tools required to compile and run the mysqlclient Python package (used to connect Django with MySQL). # Install app dependencies RUN pip install mysqlclient RUN pip install --no-cache-dir -r requirements.txt # Installs all the Python packages listed in requirements.txt. The --no-cache-dir option saves space by not keeping downloaded files after installation COPY . /app/backend # Copies all your project files from your local machine to the container’s /app/backend directory. EXPOSE 8000 # Tells Docker that your app will run on port 8000, which is the default port Django uses.
🌐
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...
🌐
Docker Hub
hub.docker.com › r › mio101 › py3-mysqlclient-alpine › dockerfile
mio101/py3-mysqlclient-alpine Dockerfile
Welcome to the world's largest container registry built for developers and open source contributors to find, use, and share their container images. Build, push and pull.