mysql icon indicating copy to clipboard operation
mysql copied to clipboard

Connecting to a mysql running on a Docker container

Open dzlab opened this issue 11 years ago • 39 comments

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 -p 3306:3306 
$ docker-machine ip default
192.168.99.100
$ docker inspect CONTAINER_ID

Then I get the ip address (172.17.0.1), but when I ping it I see time outs!!! I tried to add port forward to the default machine:

VBoxManage controlvm "default" natpf1 "tcp-port3306,tcp,,3306,,3306";

But when connecting with Sequel pro (a mysql client) with [email protected] on 3306, I have a connection failure.

What's the appropriate way to connect to the running mysql server?

dzlab avatar Aug 21 '15 12:08 dzlab

It looks mysql interpreting the -p as an argument, I just had to move the argument:

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

dzlab avatar Aug 21 '15 13:08 dzlab

By any chance did you get this to work? I have a docker container that runs mysql, but i cannot figure out what the settings are in sequel pro to get it to connect.

What credentials are needed?

flacoman91 avatar Oct 06 '15 13:10 flacoman91

@flacoman91 how are you running your container (i.e., arguments to docker run, or docker compose service)?

helderco avatar Oct 06 '15 14:10 helderco

@helderco It's in docker-compose.yml mysql: build: docker-build-contexts/mysql environment:

  • MYSQL_ROOT_PASSWORD=mypassword

Running docker ps i get fc46f6b9e08e service_mysql "/entrypoint.sh mysql" 18 hours ago Up 18 hours 3306/tcp service_mysql_1

flacoman91 avatar Oct 06 '15 14:10 flacoman91

You need to publish your mysql port to your host. The port can be a specific one or random. If random use docker-compose port mysql 3306 to get it at runtime (you can also see it in docker-compose ps).

Since you're using Sequel Pro, I guess you're using a Mac, with a VM for docker server. If you're using docker-machine, you can get the IP address with docker-machine ip <name>.

Credentials (Standard connection in Sequel Pro):

Host: IP of your docker machine host Username: root Password: mypassword Database: mydatabase Port: your specific port or randomly attributed one

Example for random host port (avoids conflict with other projects):

mysql:
  build: docker-build-contexts/mysql
  ports:
    - 3306
  environment:
    - MYSQL_DATABASE=mydatabase
    - MYSQL_ROOT_PASSWORD=mypassword

I actually use a script to open directly in Sequel Pro from the command line. You can get it at https://gist.github.com/helderco/e9d8d072a362ad818f6a

This is how I use it:

db-open mydatabase

It launches a new window already opened in that database in my container. Note that I assume password is root (my convention in development). It also assumes you use docker-machine. The database is optional, and you can set the other credentials manually with arguments.

helderco avatar Oct 06 '15 14:10 helderco

THANK YOU THANK YOU!

That worked. i missed the step where you said:

If you're using docker-machine, you can get the IP address with docker-machine ip .

I kept putting in the ip for the individual container, and not the docker host, which in my case is just the default or 192.168.99.100 and the port was the randomly assigned one which was found through docker ps

flacoman91 avatar Oct 06 '15 15:10 flacoman91

Struggled with this for ages until I realised I was entering http://my-docker-ip-address instead of just my-docker-ip-address. Remove that http!

fariazz avatar May 24 '16 08:05 fariazz

Hi, I have this container:

$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8441f5d60a49 mysql:5.7 "docker-entrypoint.sh" 2 hours ago Up 2 hours 3306/tcp shopapp-mysql

And i run $ docker-machine ip shopapp-mysql

Getting Host does not exist: "shopapp-mysql"

I've tried with Container ID and have the same response. ¿How can I get the specific IP of the shopapp-mysql container?

dslopez avatar Dec 09 '16 11:12 dslopez

You're trying to use docker machine in a container, which are different tools.

Try:

$ docker inspect shopapp-mysql

helderco avatar Dec 09 '16 13:12 helderco

I've tried that too, it give me a IPAddress=172...... I try to connect to that IP from an mysql client but I get conection timeout....

dslopez avatar Dec 09 '16 13:12 dslopez

I assume you use docker machine with Mac or Windows.

Just find the forwarded port with:

$ docker port shopapp-mysql 3306
0.0.0.0:32768

And your docker machine IP with:

$ docker-machine ip
192.168.99.100

Now point your mysql client to (example): 192.168.99.100:32768

Note: The 3306 port needs to be exposed, which from your docker ps I can see it is not.

helderco avatar Dec 09 '16 13:12 helderco

thanks @helderco !! I have run my container exposing the port to my PC

$ docker run -p 3306:3306 --name shopapp-mysql -e MYSQL_ROOT_PASSWORD=myRootpwd32 -d mysql:5.7

And that works!

dslopez avatar Dec 15 '16 16:12 dslopez

In this Dockerfile, a "mysql" user is created but there is no "USER mysql" instruction. So why, when you run the image without "-u" or "--user" mysqld is running as "mysql" and not as "root" ?

TristanCP avatar Jan 25 '17 21:01 TristanCP

@TristanCP, the entrypoint script drops to the mysql user as soon as it has done the things that required root access: https://github.com/docker-library/mysql/blob/64e0cf22c56fa9ca9f53860d52aaee094f8a8ef3/8.0/docker-entrypoint.sh#L62-L67.

yosifkit avatar Jan 25 '17 21:01 yosifkit

It should also work with --user mysql if you setup permissions yourself before running the image. :+1:

tianon avatar Jan 25 '17 21:01 tianon

@dslopez @helderco I was going through your conversation and the rest of it in this issue but could not find a solution to my issue.

I have followed the https://hub.docker.com/_/mysql/ to setup a mysql container. So I did something like this docker run --name mysql-inst -e MYSQL_ROOT_PASSWORD=<my_pass> -d mysql:latest.

Then I have the app which is another container itself and link it on runtime by doing this docker run --name some-app --link mysql-inst:mysql -d application-that-uses-mysql. Now the app fails to connect to the database and timeouts.

So I did docker inspect mysql-inst and got the IPAddress and tried to connect to the mysql client through my local and it would not connect either.

mysql -h172.20.0.3 -P 3306 -uroot -p<my_pass>
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can't connect to MySQL server on '172.20.0.3' (60)

Any suggestion or idea what I am doing wrong? Ideally I would like to do using docker-compose but I want to know why this way is not working.

ssh24 avatar Mar 10 '17 19:03 ssh24

@ssh24, if you are on Windows or OSX using docker-machine/boot2docker, you won't be able to connect directly to a container's IP address from the host. You would need to start the container with -p 3306:3306 and then connect to the ip of the Docker VM on port 3306.

In the application that uses mysql how are you connecting to the mysql service? In other words, are you connecting to localhost? Since the --link alias is mysql (the second half of the link argument), you should be connecting from the application container to the mysql host and it will be properly resolved to the IP address.

yosifkit avatar Mar 10 '17 19:03 yosifkit

@yosifkit I was not using docker-machine to connect to the container's IP address.

I just tried this to connect to the mysql client locally after spinning up the container and still won't work. The IP address of the mysql container is 172.17.0.2 docker run --name some-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=<password> -d mysql:latest

mysql -h172.17.0.2 -P3306 -uroot -p<password>
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2003 (HY000): Can't connect to MySQL server on '172.17.0.2' (60)

ssh24 avatar Mar 10 '17 19:03 ssh24

@ssh24 Are you running on Windows or Mac?

If running on Windows or Mac the containers are created on a virtual machine. That is, the host of the containers is the virtual machine not your actual machine. So find out which is the IP of the docker VM and use it instead of 172.17.0.2

netname avatar Mar 14 '17 19:03 netname

@ssh24 you need to do a

$ docker-machine ip

to obtain the IP address of your container.

dslopez avatar Mar 15 '17 07:03 dslopez