skip to Main Content

I have two docker containers running, one with a Tomcat, one with an H2 database.
To build the containers I use docker compose build with the following compose.yml:

services:
  tomcat:
    build: tomcat
    ports:
      - "8080:8080"
    networks:
      - my-network

  h2:
    build: h2
    ports:
      - "8082:8082"
      - "9092:9092"
    networks:
      - my-network

networks:
  my-network:
    driver: bridge

This is the Tomcat container’s Dockerfile:

FROM tomcat:10.1.28-jre21
COPY my-app.war /usr/local/tomcat/webapps/my-app.war
COPY h2-2.3.232.jar /usr/local/tomcat/lib/h2.jar
CMD ["catalina.sh", "run"]

This is the H2 container’s Dockerfile:

FROM openjdk:21
COPY h2-2.3.232.jar /usr/local/h2.jar
COPY startup.sh /usr/bin/startup.sh
WORKDIR /
ENTRYPOINT ["/usr/bin/startup.sh"]

This is how I start the H2 database with startup.sh:

#!/bin/bash

cd /usr/local
java -cp h2.jar org.h2.tools.Server -tcpAllowOthers -webAllowOthers

Running docker network inspect docker_my-networkshows that both containers are connected to the network. The Tomcat and H2 database are both working, I checked it in my browser.

Running curl http://tomcat:8080 inside the H2 container works.

However, running curl http://h2:8082 inside the Tomcat container returns "Host h2 not found". Please, what am I missing?

2

Answers


  1. Chosen as BEST ANSWER

    So apparently this is the correct behavior. If you try e.g. curl http://h3:8082 (notice h3) you get an error message along the lines of "Could not resolve host name 'h3'". I was eventually able to almost connect the Tomcat in the other docker container to the h2 database. I'm saying almost because the database connected to the Tomcat appeared to be empty while the database that I saw in my browser in the H2 console contained entries.

    Moral of the story: Don't use H2 as the main database for your docker application (or maybe only in embedded mode). You have much more documentation and resources available when using database systems like PostreSQL or MySQL and you avoid a lot of pain (in my opinion).


  2. ports section should be inside tomcat section. The correct docker-compose.yaml is below:

    services:
      tomcat:
        build: tomcat
        ports:
          - "8080:8080"
        networks:
          - my-network
    
      h2:
        build: h2
        ports:
          - "8082:8082"
          - "9092:9092"
        networks:
          - my-network
    
    networks:
      my-network:
        driver: bridge
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search