Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to localhost mysql server from inside docker container on macOS

How can I connect to localhost mysql server from a docker container on macOS ? One way to do it using --add-host but that requires me to pass some name like "myhost". Is there any way in macOS so that references to localhost from inside docker container actually refer to docker host ?

like image 623
rigal Avatar asked Mar 28 '17 14:03

rigal


People also ask

How do I connect to a MySQL Docker container?

Here are the steps you can follow to install the Dockerhub MySQL Container: Step 1: Pull the Docker Image for MySQL. Step 2: Deploy and Start the MySQL Container. Step 3: Connect with the Docker MySQL Container.

How do I make my Docker container talk to localhost?

Use --network="host" in your docker run command, then 127.0.0.1 in your docker container will point to your docker host.

Can Docker access local database?

In a gist, you can use the --network=host to bind the localhost with your Docker container and they access the MySQL service inside your container using the hostname “127.0. 0.1”.


2 Answers

On MacOS docker provide special DNS name docker.for.mac.localhost which will resolve to the internal IP address used by the host.

like image 160
Serge Avatar answered Sep 18 '22 18:09

Serge


use host.docker.internal

to connect to the host running the Docker. this works From docker version 18.03 onwards only and This is for development purposes and will not work in a production environment outside of Docker Desktop for Mac. ( refer the page https://docs.docker.com/docker-for-mac/networking/ for more info )

sample connection string for oracle, jdbc:oracle:thin:@host.docker.internal:1521/orcl

From inside of a Docker container, how do I connect to the localhost of the machine?

like image 38
Shanid Avatar answered Sep 17 '22 18:09

Shanid