0

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

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

My understanding is that Docker automatically links the running containers to the default ‘bridge’ network. In fact, with both containers running, if I run ‘Docker inspect network bridge’ it shows both containers running on the bridge network with each of their individual IP addresses... I tried creating a custom network and running them the same way you did with no success again...
Yes containers are attached by default to the default bridge network. They can communicate however using only IPs and not DNS names. Docker does not support automatic service discovery on the default bridge network docs.docker.com/engine/userguide/networking/…
So are you saying that I need to change my HOST in my DATABASES config to “mysql” in order for it to access the MySQL database, after connecting on a custom network?
@Naji Yes. And make sure you give the my-sql container the name mysql as specified in the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.