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.
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.
Another response would be adding:
RUN apk add --no-cache bash mariadb-dev mariadb-client mariadb-libs python3-dev build-base
On the dockerfile.
python - Dockerize a Django app with a MySQL container - Stack Overflow
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
Cannot connect to local MySQL server in docker container with django
how can i connect django container to mysql container - Stack Overflow
Videos
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
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
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:- dbnetworks:- my-networkbuild: .command: ["python", "manage.py", "runserver", "0.0.0.0:8000"]volumes:- .:/appports:- "8000:8000"
db:image: mysql:latestcontainer_name: my-mysqlenvironment:MYSQL_ROOT_PASSWORD: my-secret-pwMYSQL_DATABASE: dockertestMYSQL_USER: rootMYSQL_PASSWORD: my-secret-pwvolumes:- C:/Users/Aaron Liu/Documents/testdb:/var/lib/mysqlnetworks:- my-networkports:- "3306:3306"phpmyadmin:depends_on:- dbimage: phpmyadmin/phpmyadmincontainer_name: my-phpmyadminenvironment:PMA_HOST: dbPMA_USER: mydatabaseuserPMA_PASSWORD: mypasswordports:- "8080:80"networks:- my-networknetworks:my-network:driver: bridge
and here is my Dockerfile:
# Use an official Python runtime as a parent imageFROM python:3.8-slim-buster# Ensure system packages are up to date and install the MySQL clientRUN apt-get update \&& apt-get install -y default-libmysqlclient-dev gcc \&& apt-get clean \&& rm -rf /var/lib/apt/lists/*# Set environment variables for PythonENV PYTHONDONTWRITEBYTECODE 1ENV PYTHONUNBUFFERED 1# Set the working directory in the container to /appWORKDIR /app# Copy the current directory contents into the container at /appCOPY . /app/RUN apt-get update && apt-get install -y default-libmysqlclient-dev libssl-dev && apt install -y default-mysql-clientRUN apt-get update && apt-get install -y pkg-config
# Install any needed packages specified in requirements.txtRUN pip install --upgrade pip \&& pip install -r requirements.txt
Any help is greatly appreciated!
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!
pip install dj-database-url
settings.py
import dj_database_url
DATABASES = {"default": dj_database_url.config()}
and in your django container's config you need to add:
environment:
- DATABASE_URL=mysql://<username>:<password>@<db_container_name>/<db_name>