Accessing mysql running in another container by a Jar running in another container
- A server having the following packages installed
Nginx, Mysql, Java, Redis.
- The server is having one jar file which uses the Mysql server for database support.
- The Mysql JDBC driver use
localhostas the hostname in the URL.
In process of replicating the existing environment using Docker we perform the following tasks:
- Created four containers: nginx,redis,mysql and container running a Java application running in the same host.
- We have linked the container running the java application with the container mysql (which is already running mysql-server).
- After starting all the docker containers, when we get inside the container running the Java application and try to run the Java application we get the following error:
Caused by: java.sql.SQLException: Access denied for user ‘application’@’172-17-0-7’ (using password: YES)
Here the IP in the above exception is belong to the container running the jar and not the mysql container.
Since the jar tries to connect the mysql server using the hostname as localhost via mysql jdbc driver we have changed the
/etc/hosts file and made
localhost point to the mysql container IP.
Still, when we run the jar we are getting the same error.
We are using bridge network. Can anybody let us know where may be the mistake from our side.