Docker Community Forums

Share and learn in the Docker community.

Django not connecting to MySQL through my.cnf inside Docker

I apologize if I am missing something obvious, but I spent literally hours trying to find out why my Django app is not connecting to the MySQL inside a Docker container. I checked my code and compared it to many tutorials for dockerizing Django and all of them were working, and they had one thing in common - the MySQL connection details were directly entered in the Django settings file:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    PASSWORD': 'root',
    'USER': 'root',
    'NAME': 'ddm',
    'HOST': 'db',    
 }
} 

I, on the other side, was using a my.cnf file:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'OPTIONS': {
        'read_default_file': 'my.cnf',
    }
 }
} 

my.cnf:

[client]
host = db
database = ddm
user = root
password = root

From Django’s perspective it’a absolutely the same which one would you use so I could never even imagine this could be the reason for the error. However, once I switched to the first option (with the connection data hard-coded in settings.py), it worked! Why is that?