1

I have a network:

docker network create -d bridge --subnet 172.25.0.0/16 container_network

I have one container with mysql running. Run command:

docker run -tid -p 3306:3306 --name container_mysql --network container_network container_mysql

And one container with code:

docker run -tid -v $(pwd):/code -p 5000:5000 --name container_code --network container_network container_code

I am trying to access mysql DB from my container with code, but nothing works:

mysql -h 172.17.0.1 -P 3306 -u root -p
mysql -h container_mysql -P 3306 -u root -p
mysql -h 127.0.0.1 -P 3306 -u root -p
mysql -h 0.0.0.0 -P 3306 -u root -p

First command gives me:

ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0

Others:

ERROR 2003 (HY000): Can't connect to MySQL server on <host> (111)

Any ideas how to achieve the goal?

Edit 1 Dockerfile for Mysql container:

FROM ubuntu:16.04
RUN apt-get update
RUN echo "mysql-server mysql-server/root_password password 1234" | debconf-set-selections
RUN echo "mysql-server mysql-server/root_password_again password 1234" | debconf-set-selections
RUN apt-get install mysql-server -y
Snobby
  • 1,067
  • 3
  • 18
  • 38

1 Answers1

0

First of all, you should consider to use networks insted of link. First, link should be deprecated very soon. Second, when you put 2 container on the same network, docker add to the /etc/hosts of both containers a line for resolve the IP from container name. This is for allows you to access to a container with their name. In your case you can access to the mysql container from code container like this
mysql -h container_mysql

Look this link for more details about docker networking. This link for more info about docker-compose and networking.

Giorgio Cerruti
  • 896
  • 6
  • 17
  • I have tried `mysql -h container_mysql -P 3306 -u root -p` already. It didn't work. Updated question with docker network. – Snobby Jan 30 '17 at 10:37
  • Is it started mysql on container? – Giorgio Cerruti Jan 30 '17 at 10:40
  • Yep `Starting MySQL database server mysqld ...done.` before trying to connect it – Snobby Jan 30 '17 at 10:42
  • This is interesting :). Can you ping the `mysql_container` from `mysql_code` container? Can you see the process if you connect to `mysql_container` using `docker exec` and do a command `ls -al`. At last, I think you can remove the port exposition when containers are both in the same network. – Giorgio Cerruti Jan 30 '17 at 10:55
  • Ping from container_code to container_mysql works: `ping container_mysql`: `6 packets transmitted, 6 received, 0% packet loss, time 5198ms`. In container_mysql `la -al` gives me folders. `ps aux | grep mysql` gives me `/usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --log-error=/var/log/mysql/error.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306 --log-syslog=1 --log-syslog-facility=daemon`. Removed port exposing but that gives nothing – Snobby Jan 30 '17 at 11:02
  • 1
    Ok, maybe you should configure mysql for accepts connections from `TCP` instead of `socket`. [See this](http://stackoverflow.com/questions/14779104/how-to-allow-remote-connection-to-mysql?answertab=active#tab-top). This is my last solution :). I'm sorry. – Giorgio Cerruti Jan 30 '17 at 11:07
  • Thanks for trying:) Didn't help also, aI don't have any line `bind-address = 127.0.0.1` in `/etc/mysql/my.cnf` and after granting priveleges to connect with root from remote host, the same error: `ERROR 2003 (HY000): Can't connect to MySQL server on 'container_mysql' (111)` – Snobby Jan 30 '17 at 11:17
  • Try `bind-address = 0.0.0.0` and restart the `mysql` service – Giorgio Cerruti Jan 30 '17 at 11:45