Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to Docker MySQL container from localhost?

I have a docker mysql image running, following is what the docker-compose.yml file looks like:

db:
  image: mysql
  environment:
    MYSQL_ROOT_PASSWORD: ""
    MYSQL_ALLOW_EMPTY_PASSWORD: yes
  ports:
    - "3306:3306"

This works fine.

My question is: How can I connect to the MySQL instance running on that container from the command line mysql client on my the host (my macbook)?

To clarify:

  • I have a macbook with Docker installed
  • I have a docker container with mysql
  • I want to connect to the mysql instance running on the aforementioned container from the Terminal on my macbook
  • I do NOT want to user a docker command to make this possible. Rather, I want to use the mysql client directly from the Terminal (without tunneling in through a docker container).

I don't have MySQL running locally, so port 3306 should be open and ready to use.

The command I am using to start the container is: docker-compose run

like image 476
Caleb Avatar asked Sep 02 '15 18:09

Caleb


People also ask

How can Docker container connect to localhost?

You just need to reference it by its Docker network IP, instead of localhost or 127.0. 0.1 . Your host's Docker IP will be shown on the inet line. Connect to this IP address from within your containers to successfully access the services running on your host.

Does Docker container have localhost?

docker run --network="host" Alternatively you can run a docker container with network settings set to host . Such a container will share the network stack with the docker host and from the container point of view, localhost (or 127.0. 0.1 ) will refer to the docker host.


3 Answers

Using docker-compose up

Since you published port 3306 on your docker host, from that host itself you would connect to 127.0.0.1:3306.

Using docker-compose run

In that case the port mapping section of the docker-compose.yml file is ignored. To have the port mapping section considered, you have to add the --service-ports option:

docker-compose run --service-ports db 

Additional note

Beware that by default, the mysql client tries to connect using a unix socket when you tell it to connect to localhost. So do use 127.0.0.1 and not localhost:

 $ mysql -h 127.0.0.1 -P 3306 -u root 

Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

$ mysql -h localhost -P 3306 -u root 

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

like image 113
Thomasleveil Avatar answered Sep 19 '22 18:09

Thomasleveil


I got it!! The answer is to use the --service-ports option when running docker-compose:

docker-compose run --service-ports db (the original docker-compose.yml file works fine)

Thanks to all for the help!

like image 30
Caleb Avatar answered Sep 20 '22 18:09

Caleb


A simple way to login to MySQL inside a Docker image is:

sudo docker exec -it <CONTAINER_ID> mysql -u root -p

for mySQL's root account by default password is not set, its BLANK, just press enter/return key, unless you have changed root password.

On successful execution, above command gives you mysql prompt.

Cheers!

like image 24
cross_handle Avatar answered Sep 21 '22 18:09

cross_handle