6

I'm trying to run mysql server on a Docker (installed with Docker Toolbox for Mac) container and access it from my machine running OS X Yosemite. The documentation from the official repo does not explain how to connect from outside the docker host !!

I've created a container using the official repository as follows:

$ docker pull mysql
$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest
$ docker inspect CONTAINER_ID

Then I get the ip address (172.17.0.1), but when I ping it I see time outs!!! What's the appropriate way to connect to the running mysql server?

bachr
  • 5,780
  • 12
  • 57
  • 92

1 Answers1

5

It says:

This image exposes the standard MySQL port (3306), so container linking makes the MySQL instance available to other application containers

First, make sure your docker run map that port: -p 3306:3306 (or the exposed port from the Dockerfile wouldn't be accessible from the Linux host)

Then, you need

  • either to add a port forwarding rule to your VirtualBox VM, and access 127.0.0.1:3306,

      VBoxManage controlvm "boot2docker-vm" natpf1 "tcp-port3306,tcp,,3306,,3306";
    
  • or access the boot2docker VM IP address $(boot2docker ip), using port 3306.

After discussion, it turn out adding the port mapping at the end is wrong:

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest -p 3306:3306

This does not work because "-p 3306:3306" is just interpreted as arguments to pass to the ENTRYPOINT command.

This works (meaning a docker ps -a shows the container as "running", not "exited"):

 docker run -p 3306:3306 --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:latest

Then root@127.0.0.1:3306 or root@$(docker-machine ip):3306 should be correct.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    I'm not using boot2docker but the new Docker Toolbox – bachr Aug 21 '15 at 10:54
  • @elsoufy same idea: there is still a VM. Plus if the port is not mapped on the docker run, it won't be accessible anyway. – VonC Aug 21 '15 at 10:54
  • It looks like it works but I'm having a denied access for `root` I guess I have to garant remote access! – bachr Aug 21 '15 at 11:21
  • @elsoufy which solution did you use? – VonC Aug 21 '15 at 11:22
  • The one with `VBoxManage` but I think it's not working, I have another mysql running on vagrant and the client was connecting to this one and not the one on docker!!! – bachr Aug 21 '15 at 11:27
  • @elsoufy then try to connect to `$(boot2docker ip):3306` (if your docker run does use `-p 3306:3306`). With toobox, it might be `$(toolbox ip)` – VonC Aug 21 '15 at 11:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87581/discussion-between-elsoufy-and-vonc). – bachr Aug 21 '15 at 11:31
  • @elsoufy I don't see you in the chat anymore. Does it work? – VonC Aug 21 '15 at 11:56
  • 1
    I faced with similar issue and I could be able to solve the problem with docker command at the bottom. "docker run --name db -d -e MYSQL_ROOT_PASSWORD=yourPassword -p 3306:3306 mysql:latest" – thread-game Dec 31 '16 at 15:51