Connecting to a mysql running on a Docker container Connecting to a mysql running on a Docker container docker docker

Connecting to a mysql running on a Docker container


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.