title: Dockerized web app connecting to MySQL DB on host
body: <p>I want to migrate my application deployment to use docker/docker hub.  Current production set up is to load balance traffic across 2 Linux servers, running Apache, Tomcat and MySQL.  The Java web application connects to a master+slave MySQL DB via JDBC.</p>
  
  <p>To minimize the impact of this change, as a first step I am looking at containerizing the tomcat and web application.  Leaving Apache and MySQL running on the host(s) as it does now.</p>
  
  <p>The problem I am encountering is connecting the application, running in the docker container, with the MySQL DB running on the host.</p>
  
  <p>The best I can do at the moment is connect to the docker container, get it's IP address and then use an SQL GRANT to allow that IP address access to the DB.  The trouble with this is that every time you run a container you potentially get a new IP address and therefor have to run a grant each time.</p>
  
  <p>I've been playing around with this stuff for a while and I can't believe there is not a better solution than what I have proposed above.</p>
  
  <p>Anyone done anything like this?  Anyone got any suggestions as to how to best approach this problem.</p>
  
  <p><strong>Edit</strong>
  It's been suggested that "<a href="https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach">From inside of a Docker container, how do I connect to the localhost of the machine?</a>" is a duplicate of this - but the underlying problem still exists.  Every time I create a Docker container from an image I potentially get an new IP address for the container that needs to be granted access to the DB. I wanted to try and avoid having to do this.</p>
  
  <p>Many thanks</p>