Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect locally hosted MySQL database with the docker container

Through docker-compose.yml I am able to run the application. Now we want to move the application to production, But we don't want to use the container database. So is there any way so that I can connect my local MySQL database with the application using docker-compose?

My docker-compose.yml looks like:

version: '3' services:   web-app:     build:       context: .       dockerfile: web-app/Dockerfile     ports:       - 8080:8080     links:       - app-db    app-db:     build:       context: .       dockerfile: app-db/Dockerfile      environment:     - MYSQL_ROOT_PASSWORD=password     - MYSQL_DATABASE=Optimize     ports:       - 3306:3306 

Instead of app-db part I just want to connect to my locally hosted mysql database.

like image 273
Prateek Naik Avatar asked Jun 14 '17 11:06

Prateek Naik


People also ask

How can Docker container connect 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. Note: This mode only works on Docker for Linux, per the documentation.


1 Answers

Just use host.docker.internal instead of localhost.

like image 100
Júlio Falbo Avatar answered Oct 15 '22 18:10

Júlio Falbo