Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker - Tomcat and PostgreSQL containers in same host - No Route to host

I have containerized a web application running on Tomcat and started the container using the command,

docker run -d -p 8080:8080 tveuser/tve-repository:tve-services

I am also running a PostgreSQL container on the same host using the following command:

docker run -d  -p 80:80 -p  5432:5432 tveuser/tve-repository:tve-postgresql

i verified that the PostgreSQL is running by using phpPgAdmin but could not get tomcat to connect to it. 'docker ps' also tells me that both the containers are up and running.

I connect the web application with the database through tomcat context.xml which has an entry like

<Parameter name="abc.connection.url" value="jdbc:postgresql://1.2.3.4:5432/dbname" />

where 1.2.3.4 is the docker host Ip in which the container is running.But i get the following error when i run the tomcat container:

org.postgresql.util.PSQLException: The connection attempt failed.
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:225)
        at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:64)
        at org.postgresql.jdbc2.AbstractJdbc2Connection.<init>(AbstractJdbc2Connection.java:136)
...............

Caused by: java.net.NoRouteToHostException: No route to host
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)

Any help appreciated.

like image 624
cucucool Avatar asked Jul 31 '14 22:07

cucucool


1 Answers

TL;DR : check the firewall on your host

You need to connect to your host's IP address on the port that you've exposed for postgres on your postgres container, not to the IP address of the docker container.

E.g.,

<Parameter name="abc.connection.url" value="jdbc:postgresql://192.168.x.x:5432/dbname" />

You'll also need to configure postgres inside your postgres docker container to listen for connections from somewhere other than 127.0.0.1, which is the default.

E.g., in pg_hba.conf:

host    all             all             0.0.0.0/0               md5

This tells postgres to accept incoming connections from any IP address (you'll probably want to lock this down to something like 192.168.0.0/16.

You'll also need to change the value of listen_addresses in postgres.conf:

listen_addresses = '*'      # what IP address(es) to listen on;

Once you've made these configuration changes, you can check to see if postgres is listening to all requests from all IPs inside your docker container:

netstat -tunlp

Active Internet connections (only servers)                                 
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      816/postgres         
tcp6       0      0 :::5432                 :::*                    LISTEN      816/postgres         

And then from your host, make sure you can telnet into port 5432:

telnet 192.168.x.x 5432
Trying 192.168.x.x...
Connected to 192.168.x.x.
Escape character is '^]'.
like image 107
Chris McKinnel Avatar answered Oct 19 '22 13:10

Chris McKinnel