10

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

Asim
  • 543
  • 4
  • 20

1 Answers1

4

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.

Radoslav Stoyanov
  • 1,460
  • 5
  • 27
  • 40
  • Thanks for your answer. I forgot to clarify I am running an ubuntu vm on a windows host and then running docker on the ubuntu vm. typing the ip a gives the internal ip address of the vm any ideas ? – Asim Feb 01 '17 at 15:13
  • Where is the database? On the physical (Windows) machine or on the virtual (Ubuntu) machine? – Radoslav Stoyanov Feb 01 '17 at 15:16
  • the database is in Microsoft Azure. I am building the application locally and will then deploy it to Azure later on. – Asim Feb 01 '17 at 15:44
  • @Asim Can you find the database settings in Azure? You will need the host (IP), and there should be a setting for which IPs are allowed to connect to it. There you should enter the IP of your local physical machine (the one with the Windows). After that you have to setup port forwarding between the physical and the virtual machine for port `27018`. Then you have to put the IP address that you found in Azure in the database configuration in your application. – Radoslav Stoyanov Feb 01 '17 at 17:32
  • [Here](http://www.howtogeek.com/122641/how-to-forward-ports-to-a-virtual-machine-and-use-it-as-a-server/) is a good tutorial for how to setup port forwarding when using a virtual machine. – Radoslav Stoyanov Feb 01 '17 at 17:33
  • @Radsolav Stoyanov Thank you very much I will give it a shot. – Asim Feb 02 '17 at 09:05