A pre-authenticated web application connecting to a MySQL database on a host

I want to migrate my application deployment to use docker / docker hub. Current production is configured to load traffic balance on two Linux servers running Apache, Tomcat, and MySQL. The Java web application connects to the master + slave MySQL DB via JDBC.

To minimize the impact of these changes, as a first step, I consider tomcat and web application containerization. Exiting Apache and MySQL running on the host (s) as it is now.

The problem I am facing is connecting an application running in a docker container with a MySQL database running on the host.

The best I can do at the moment is to connect to the docker container, get its IP address, and then use SQL GRANT so that this IP address can access the database. The problem is that every time you start a container, you potentially get a new IP address and therefore you must run a grant every time.

I played with this material for a while, and I cannot believe that there is no better solution than what I suggested above.

Has anyone done something like this? Everyone has any suggestions on how best to approach this issue.

Edit It has been suggested that “ Inside the Docker container, how can I connect to the local host of the machine? Is a duplicate of this, but the main problem still exists. Each time I create a Docker container from the image, I potentially get a new IP address for the container that should be granted access to the database, I wanted to try to avoid this.

Many thanks

+5
source share
2 answers

You must use the MySQL server-based server and the Docker link command, which connects the two containers.

Start MySQL first:

docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql

Then launch the application using link :

docker run --name some-app --link some-mysql:mysql -d app-that-uses-mysql

Now you can get the address and port of the database from environment variables:

docker exec -it some-app printenv

Look at something like: MYSQL_PORT_3306_TCP_PORT . Now you just need to use these variables in your application.

Learn more about Docker MySQL Repo and Docker docs .

+4
source

SQL GRANT should use the IP interface from docker0 instead of the specific IP address from the container.

To find it, use:

 ifconfig | grep -A 1 docker0 

and IP after inet addr: (most likely it will be 172.17.42.1 )

This will allow you to use all containers from the docker interface.

+3
source

All Articles