Using this tutorial https://semaphoreci.com/community/tutorials/dockerizing-a-python-django-web-application, I'm dockering my Django application in a VirtualBox using docker-machine
. Everything has gone splendidly until I go to my browser and my application says that it's having issues with MySQL.
Then i found this documentation for dockerizing an instance of mysql https://github.com/mysql/mysql-docker which I followed, creating the image in the same development VirtualBox that I created. The error I was originally getting was
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'
My Django DATABASES looked like this
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'db_name',
'USER': 'root',
'HOST': 'localhost',
'PORT': ''
}
}
I then changed the host to 127.0.0.1
and even tried specifying the port as 3306
and I got a new error which was
(2003, "Can't connect to MySQL server on '127.0.0.1' (111)")
I also went in to MySQL workbench and changed the connection of my Local instance to be 127.0.0.1:3306
and that hasn't helped.
The commands that I'm running are
eval "$(docker-image env development)"
---> supposedly does THESE things:
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://123.456.78.910:1112"
export DOCKER_CERT_PATH="/Users/me/.docker/machine/machines/development"
export DOCKER_MACHINE_NAME="development"
Then, now that i'm in my virtualbox, I run:
docker run -it -p 8000:8000 <docker image name>
---> forwards exposed port 8000 to port 8000 on my local machine
docker run -it -p 3306:3306 mysql/mysql-sever
---> forwards exposed port 3306 to port 3306 on my local machine
The problem is that you are trying to connect with 127.0.0.1
or localhost
which from the perspective of the django container will refer to itself and not to the mysql container.
In general, for containers to communicate, the best "docker way" is to make the containers share a common docker network.
docker network create mynet
docker run -it --network mynet -p 8000:8000 <docker image name>
docker run -it --network mynet -p 3306:3306 --name mysql mysql/mysql-sever
Now the application container can connect to mysql using mysql
as a hostname and 3306
as the port.