I trying to connect a web server that runs in an Apache Tomcat container to a MySQL database that runs another container. In order to do that I am using the linking mechanism from Docker.
docker run -it --name ${CONTAINER_NAME} --link db:db -p 8080:8080 -d tomcat
After running the container I can see that the containers are linked and the environment variables are exposed properly.
In order to connect the web application that is running in the Tomcat container to the database, I am using the following configuration file:
<Context>
<Resource
name="jdbc/MYDB"
type="javax.sql.DataSource"
auth="Container"
username="user"
password="password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://${DB_PORT_3306_TCP_ADDR}:${DB_PORT_3306_TCP_PORT}/epcis?autoReconnect=true">
</Resource>
</Context>
Now the problem is that I can't establish the connection to the database because the environment variables exposed by Docker are not recognized at the Tomcat environment.
There is a way to make these environment variables exposed by Docker visible to the Apache Tomcat environment?
Here are the steps you can follow to install the Dockerhub MySQL Container: Step 1: Pull the Docker Image for MySQL. Step 2: Deploy and Start the MySQL Container. Step 3: Connect with the Docker MySQL Container.
Could you use the dns declaration for db and hard code the reference? I think the /etc/hosts file is updated with db and it's ip address when you --link it. So you could:
<Context>
<Resource
name="jdbc/MYDB"
type="javax.sql.DataSource"
auth="Container"
username="user"
password="password"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql:db:3306/epcis?autoReconnect=true">
</Resource>
</Context>
Another technique I use is skydns and registrator, then you get the ip and port in a DNS srv record.
I don't remember Tomcat but it should have access to the variables. Are you sure that Tomcat evaluates the url definition before using it?
Indeed those env vars are not exposed in the contect in which tomcat is running. The solution is to create your own Dockerfile based on the tomcat one (i.e. "FROM tomcat") and create your own startup script where you pass the relevant env vars into /etc/tomcat/tomcat.conf:
echo DB_PORT_3306_TCP_ADDR=${DB_PORT_3306_TCP_ADDR} >> /etc/tomcat/tomcat.conf
For a fully working tomcat6/postgres example see this Dockerfile with this startup script
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With