2

I have two VM's running, one (A) has the MYSQL database inside a docker network. The other (B) VM has my front-end application docker container.

I run my MYSQL docker container (A) as follow:

docker run --name db --net=netname -v /path/to/mysql/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password  -d mysql:5.6.32

docker exec -i db mysql -pyourpassword -e 'CREATE DATABASE mydb'

After editing the MYSQL my.cnf file to bind-address = 0.0.0.0 I've tried to connect from VM (B) using the ip address given to me by the MYSQL docker container with ip addr show eth0 command.

which is:

 eth0@if46: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff
inet 172.18.0.2/16 scope global eth0
   valid_lft forever preferred_lft forever

host: 172.18.0.2 port: 3306

The response i receive:

OperationalError: (2003, "Can't connect to MySQL server on '172.18.0.2' ([Errno 110] Connection timed out)")

I've also tried VM (A) ip address but the response becomes connection refused

I'm sure i'm missing something -or many things- i'm open for enlightenment. How would i go about successfully connecting to (A) my MYSQL container from server (B) ? Thank you in advance.

Alphawarz
  • 79
  • 1
  • 8
  • I will have to research it first. Have you already read docker network documentation? – Raynal Gobel Jul 25 '17 at 03:46
  • @aDeveloper how do you run each containers? Do you run them with `docker run`, or with `docker-compose`? What documentation are you reading? Please add more information. – cizixs Jul 25 '17 at 03:47

2 Answers2

1

You might as well just map the mysql port to the host.

docker run -p 3306:3306 --name db --net=netname -v /path/to/mysql/:/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=password  -d mysql:5.6.32

then you can access mysql using the host ip or name.

If you are trying to reach containers directly by ip address.. in most cases that means you are doing it wrong. Assume the assigned ip is different every time you start it the container.

Grimmy
  • 3,992
  • 22
  • 25
  • Thank you very much, i guess i was overthinking and also, i was missing something very obvious. I was able to connect the application with the database thanks to you. – Alphawarz Jul 27 '17 at 07:58
0

If you want to connect two containers, they have to be in the same network. In this way you can access containers through their network-alias. For example run the following commands:

docker network create my-network
docker run --name db --net=my-network --network-alias=db -v /path/to/mysql/:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=password  -d mysql:5.6.32
docker run -it --name ping --net=my-network --network-alias=ping debian:strech /bin/bash

The inside the ping containers you can run ping -c2 db

If you want to have several containers working together, you can take a look to docker-compose

D. Gonçalves
  • 689
  • 6
  • 12