Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker container communicating with external database

I am working on an application to move it to Docker. The application has a reverse proxy nginx sitting at the front that directs the calls to the the front end application. The front end application is then linked to a back end java application. At the end there is a mongo db. I don't want to move mongo db to a container so want my application to communicate directly with the mongo db. I use docker-compose to bring my containers to life. My question is how can I make my containers to communicate with the mongo db using docker-compose. I have searched the and found the following link where it is being spoken about and plans to be added to Docker. However I couldn't find a solid example. I a new to docker so any help will be highly appreciated. https://github.com/docker/compose/issues/1110

like image 891
Asim Avatar asked Jan 31 '17 09:01

Asim


People also ask

How do you connect an external database to a container?

Connect to other containerized services # Run server $ docker run --name mysql_server -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=password mysql # Run client $ docker run --rm --name my_client -it --link mysql_server:mysql_server alpine ping mysql_server > PING mysql_server (172.17. 0.5): 56 data bytes 64 bytes from 172.17.

Can Docker container connect to outside world?

Your Docker container can connect to the outside world, but the outside world cannot connect to the container. To make the ports accessible for external use or with other containers not on the same network, you will have to use the -P (publish all available ports) or -p (publish specific ports) flag.


1 Answers

First of all, it is a best practice to leave any persistent data outside the image, so you made a good decision with that ;).
So, to connect to a database that is on the host machine, follow these steps:

  1. First you have to find the IP address of host machine in the docker network. You can do this by typing ip a in the console if you are running some Linux distribution, or ipconfig /all if you are running Windows. You can see the IP address next to the docker bridge adapter (if you are using the default adapter it should be docker0 and the row should be starting with inet).
  2. Then make sure the database user has no limitations to be used to connect through an outside network.
  3. When you have the host (IP address), the database, the database user and the database password, just put them in the database configuration of your application.

That should do the job.

like image 119
Radoslav Stoyanov Avatar answered Sep 19 '22 23:09

Radoslav Stoyanov