6

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?

6
  • Check if mysql container is exposing 3306 port. You can then enter the container and use for example telnet to see if containers can talk with each other. Commented Apr 1, 2015 at 11:11
  • They can talk to each other, I confirm that using telnet. The problem is that the JDBC driver does not know the values of ${DB_PORT_3306_TCP_ADDR} and the others env variables. The error is reported in the tomcat log from my web app. Commented Apr 1, 2015 at 11:23
  • 1
    You can use env property to pass enviroment variables: docs.docker.com/reference/run/#env-environment-variables Commented Apr 1, 2015 at 11:25
  • I already do that too and it does not work, but I was passing the env variables from the Dockerfile. I will try to pass the env variables from the Docker run command in order to test if these variables are recognized at the Tomcat environment. Commented Apr 1, 2015 at 11:31
  • Strange..., another option you can try is exporting CATALINA_OPTS. There you can specify another environment variables available for tomcat. Commented Apr 1, 2015 at 11:37

2 Answers 2

4

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?

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Greg, you are right. The /etc/hosts contains the IP from the db container, that's solved my problem.
I tried using this method in my META-INF/context.xml but I am getting errors on tomcat startup around the "mysql:db:3306" because it is not resolving "db" to an IP.
Take a look at your /etc/hosts file. It will contain the wrong IP address. If you're trying to connect another container to it, you need to use the docker-machine IP address and external port you've defined.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.