Error when using Django with Docker - "Unable to connect to MySQL server on" 127.0.0.1 "(111)")
I am trying to use Docker with Django but I get the error - db_1 | error: database is uninitialized and password option is not specified
db_1 | You need to specify one of MYSQL_ROOT_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (111)")
. When I use my application without Docker, it works. Any suggestions?
My Dockerfile:
FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
My docker-compose.yml:
version: '3'
services:
db:
image: mysql
web:
build: .
command: python3 Project/myVirtual/backend/pri/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
I changed docker-compose.yaml to:
version: '3'
services:
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: ""
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
web:
build: .
command: python3 virtualPRI/PRI/backend/pri/manage.py runserver 0.0.0.0:8000
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
but at that moment the processes were blocked
db_1 | 2017-06-13T05:16:16.457122Z 0 [Note] End of list of non-natively partitioned tables
or sometimes i get web_1 | django.db.utils.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (111)")
manage:py
:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pri.settings")
try:
from django.core.management import execute_from_command_line
except ImportError:
# The above import may fail for some other reason. Ensure that the
# issue is really that Django is missing to avoid masking other
# exceptions on Python 2.
try:
import django
except ImportError:
raise ImportError(
"Couldn't import Django. Are you sure it installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
raise
execute_from_command_line(sys.argv)
127.0.0.1 (111)
means MySQL is discarding your connection request. My guess is that MySQL server and Django don't run on the same Docker instance, right?
If MySQL is running elsewhere, you need to make sure you set up the MySQL connection correctly in your Django config file.
Solution 1: You can enable remote access to MySQL (but I don't recommend that you do this) by changing the MySQL bind address from 127.0.0.1
to 0.0.0.0
. and update your MySQL DB connection settings correctly.
On Linux, you can comment out these lines from /etc/my.cnf
(location may vary)
skip-networking
bind-address = 127.0.0.1
For more detailed instructions see also https://dev.mysql.com/doc/refman/5.5/en/can-not-connect-to-server.html
Solution 2: (Inspired by @programerq's comment). You can run MySQL on the host operating system or on another Docker instance. Since MySQL is bound to 127.0.0.1
by default, you can map MySQLMachine:MySQLPort
to DjangoDocker:MySQLPort
when you start your Django Docker instance. So Django can still connect to 127.0.0.1
MySQL without noticing that MySQL is actually running somewhere else.
I have done some port mapping in a Docker cluster project before, you can check the commands I run or check the Docker white paper.
Hope these ideas can be helpful, cheers.
source to share