Home >Database >Mysql Tutorial >Why is my Django app in Docker Compose unable to connect to MySQL, even though I have the correct port and container name?
Troubleshooting Failed Django-MySQL Connection in Docker Compose
In this article, we will address the issue of establishing a connection between Django and MySQL within Docker containers using Docker Compose. Despite configuring the containers as outlined in the question, the user encounters the following error:
django.db.utils.OperationalError: (2003, 'Can\'t connect to MySQL server on \'mariadb55\' (111 "Connection refused")')
After exhaustive troubleshooting, the user discovered the root cause and solution. Here are the crucial insights:
Key Points:
Updated Docker Compose Config:
services: db: ... ports: - "3302:3306" web: ... command: python3 manage.py runserver 0.0.0.0:6001 ... volumes: - .:/djcode ports: - "6001:6001" depends_on: - db
Updated Django Settings File:
DATABASES = { 'default': { ... 'HOST': 'db', 'PORT': '3306', ... } }
Additional Tips:
By implementing these changes, the user successfully established the connection between Django and MySQL in Docker containers, resolving the "Can't connect to MySQL server" error.
The above is the detailed content of Why is my Django app in Docker Compose unable to connect to MySQL, even though I have the correct port and container name?. For more information, please follow other related articles on the PHP Chinese website!