2

I built a MySQL container from an image, found its IP address but unable to connect from command line or mysqlworkbench. Here's my configuration.

Dockerfile

FROM mysql

ENV MYSQL_DATABASE=test
ENV MYSQL_ROOT_PASSWORD=password

COPY ./schema.sql /docker-entrypoint-initdb.d/

Command

docker build -t mysql-image .
&& docker run
-p 6603:3306
--name mysql-container
mysql-image 

I can see that the container is running from docker container ls. I found its IP address using this command with the help of this answer.

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql-container

It gave me this ip -> 172.17.0.2

Error

enter image description here

Edit

The linked duplicate tag isn't the solution, the problem lies with the following command that misled me.

docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mysql-container
halfer
  • 19,824
  • 17
  • 99
  • 186
Developer
  • 924
  • 3
  • 14
  • 30
  • Does your phpmyadmin works? check it on http://localhost:yourMySQLportNumber/ – Invincible May 22 '19 at 08:25
  • I don't have phpmyadmin, my env is nodejs – Developer May 22 '19 at 08:26
  • yes localhost worked, actually 127.0.0.1 – Developer May 22 '19 at 08:27
  • Possible duplicate of [How to connect mysql workbench to running mysql inside docker?](https://stackoverflow.com/questions/33827342/how-to-connect-mysql-workbench-to-running-mysql-inside-docker) – Tomm May 22 '19 at 08:31
  • I think you'll need phpmysql to see mysql interface... if not, then check your mysql port number(default is 3306) so in your case, try 127.0.0.1:3306 – Invincible May 22 '19 at 08:31
  • I used `127.0.0.1:6603` instead of `172.17.0.2:6603` and it worked, The linked answer to find ip address of db actually misled me. – Developer May 22 '19 at 08:33

2 Answers2

2

You need to use localhost (127.0.0.1) instead the container IP (in your case 172.17.0.2) at hostname

Schwarz54
  • 964
  • 1
  • 9
  • 18
  • why is that, the tutorial i followed used 172 as the ip and it worked for him but for me 127 worked, wondering why? and what should i put in the production code for db connection? – Developer May 22 '19 at 09:55
  • Here you have some explanation about that, https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach (at the end of the answer) – Schwarz54 May 22 '19 at 10:07
  • so basically 127 is host mode and 172 is bridge mode? – Developer May 22 '19 at 10:31
  • 1
    Something like that, I'm not an expert in docker yet and I cant say yes but its seems like that. – Schwarz54 May 22 '19 at 10:35
2

You need to set the allow login from any ip with MYSQL_ROOT_HOST=%

Full docker command line as follows:

>docker run --name mysql-server -e MYSQL_ROOT_HOST=% -e MYSQL_ROOT_PASSWORD=12345 -p 3306:3306 -d mysql/mysql-server

--name means that it's container name

-d means that docker will run in deattach mode. Final it's mysql docker image

When docker status is Up 3 minutes (healthy) you can easy connect to DB via mysql Workbench

enter image description here

Hope to help you!

phancuongviet
  • 311
  • 2
  • 11