title: Docker Tomcat container unable to access Postgres container
body: <p>I have a alpine docker with postgres, with listen address '*' and listening to 5432, which I'm deploying using</p>
  
  <pre><code>docker run -d --name postgres me/postgres:v1
  </code></pre>
  
  <p>and my tomcat container with oracle jre8, on which I'm deploying my rest web service using:</p>
  
  <pre><code># Set environment
  ENV CATALINA_HOME /opt/tomcat
  
  EXPOSE 8080    
  
  # Launch Tomcat on startup
  CMD ${CATALINA_HOME}/bin/catalina.sh run
  RUN rm -rf ${CATALINA_HOME}/webapps/docs \
      ${CATALINA_HOME}/webapps/examples \
      ${CATALINA_HOME}/webapps/ROOT
  
  # Deploying war file
  ADD myapp.war ${CATALINA_HOME}/webapps/ROOT.war
  
  # Restarting server after deploying
  CMD ${CATALINA_HOME}/bin/catalina.sh run
  </code></pre>
  
  <p>And deploying it with </p>
  
  <pre><code>docker run -d -p 8080:8080 --name tomcat --link postgres:postgres me/tomcat:v1
  </code></pre>
  
  <p>Both are being executed on my laptop, with IP address 192.168.x.x, and I checked the port is listening.</p>
  
  <p>Unfortunately my web service on tomcat cannot connect to the postgres service using </p>
  
  <pre><code>jdbc:postgresql://192.168.x.x:5432/dBName
  </code></pre>
  
  <p><strong>Alternate I already tried:</strong> I launched postgres on it's own port using,</p>
  
  <pre><code>docker run -d -p 5432:5432 --name postgres me/postgres:v1
  docker run -d -p 8080:8080 --name tomcat me/tomcat:v1
  </code></pre>
  
  <p>Then used </p>
  
  <pre><code>jdbc:postgresql://192.168.x.x:5432/dBName
  </code></pre>
  
  <p>and </p>
  
  <pre><code>jdbc:postgresql://localhost:5432/dBName
  </code></pre>
  
  <p>but neither seems to work.</p>
  
  <p>In both cases I can see my web server running in tomcat manager, and I am able to access my dB using</p>
  
  <pre><code>psql -h localhost -p 5432 -d dBName -U myUser
  </code></pre>
  
  <p>as well as pgAdmin.</p>
  
  <p>Any help in resolving this is appreciated.</p>
  
  <p><strong>Solution Update:</strong> While using --link, point to postgres (i.e., your postgresql container name) instead of IP</p>
  
  <pre><code>jdbc:postgresql://postgres:5432/dBName
  </code></pre>
  
  <p>Many thanks to @larsks for pointing it out.</p>