Connecting to a mysql running on a Docker container

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:

  • Gitlab CI: How to run tests in pipeline using docker and shell runner
  • docker daemon exec: “dockerd” : executable not found in %PATH%
  • Docker exec command
  • Uploading and storing Docker certificates for connecting to remote Docker machines
  • In docker,how to close the port expose by dockerfile?
  • Is it possible to show the `WORKDIR` when building a docker image?
  • $ 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 (, but when I ping it I see time outs!!! What’s the appropriate way to connect to the running mysql server?

  • docker Job for docker.service failed because the control process exited with error code
  • Why doesn't the container start?
  • make new docker container after it has been deleted
  • Within Docker VM, Gulp-Watch Seems to not work well on volumes hosted from the host OS
  • How can I set the time zone in Dockerfile using gliderlabs/alpine:3.3
  • Dockerfile drupal console access denied mkdir
  • One Solution collect form web for “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,

      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@ or root@$(docker-machine ip):3306 should be correct.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.