Create Docker container with both mysql and tomcat

Good evening everyone, let me start by saying that I am not a Docker expert. So far I have created Docker images independent of the database and all API resources and, if someone helps me, I will take this opportunity to learn something new. I’m trying to build a container composed of a sql container and a web container. I can do it without problems but, once I access the url I get: Failed to load resource: the server responded with a status of 404 () to the api: midoripol/beats . I think it’s a network-related problem, could it be? If so, what should I do? I’m also a bit confused about the role of my persistence.xml and web.xml files. In the persistence I have some red lines, specifically:

<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
 <class>com.midoriPol.model.Person</class>
 <class>com.midoriPol.model.Product</class>
 <class>com.midoriPol.model.Cart</class>

and here:

<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>

which makes me think there are configuration issues related to Jpa-Hibernate.

This is my docker-compose:

version: “3”
services:
db:
image: mysql:latest
environment:
MYSQL_ROOT_PASSWORD: edam15!k2Mp_
MYSQL_DATABASE: newdb
MYSQL_PASSWORD: edam15!k2Mp_
ports:

  • “3307:3306”
    volumes:
  • ./db:/docker-entrypoint-initdb.d
    networks:
  • mynetwork

web:
image: tomcat:latest
environment:
JDBC_URL: jdbc:mysql://db:3306/newdb?connectTimeout=0&socketTimeout=0&autoReconnect=true
JDBC_USER: root
JDBC_PASS: edam15!k2Mp_
ports:

  • “8080:8080”
    volumes:
  • ./tomcat/webapps:/usr/local/tomcat/webapps
    networks:
  • mynetwork

networks:
mynetwork:
drivers: bridge

However, I have put a public repository: GitHub - Luca-Liseros-Ferrari/dockerComposeBeats: dockerComposeBeats

Any help is truly appreciated and I am willing to provide details, in case they are requested.

Use 3 backticks before and after code/config to make it more readable.

Your XML is application specific, has nothing to do with Docker, so for that you probably won’t find answers here.

In general it is correct to use db as DB host, that should be resolved by Docker DNS to the appropriate container IP.

Check you application (container) log for what’s happening.

hi, thanks @bluepuma77 for the reply. I solved the problem, it was, in fact, a networking problem. I solved it with this docker-compose:

version: '3.8'

 networks:
 mynetwork:
 drivers: bridge
 ipam:
 driver: default
 config:
 - subnet: "172.21.0.0/16" 
 gateway: "172.21.0.1" 

services:
 web:
 image: tomcat:9-jdk17-openjdk
 container_name: web
 ports:
 - "8080:8080"
 volumes:
 - ./tomcat/webapps/midoripol.war:/usr/local/tomcat/webapps/ROOT.war
 networks:
 - mynetwork

 db:
 image: mysql:latest
 container_name: db
 environment:
 MYSQL_ROOT_PASSWORD: edam15!k2Mp_
 MYSQL_DATABASE: newdb
 MYSQL_PASSWORD: edam15!k2Mp_
 ports:
 - "3307:3306"
 volumes:
 - ./db:/docker-entrypoint-initdb.d
 networks:
 - mynetwork

after docker-compose up

I ran this command to verify they were connected:

docker network inspect mynetwork

since they weren’t and the container property had an empty object {} I linked them:

docker network connect mynetwork web

docker network connect mynetwork db

I hope this will help someone who, like me, has stumbled across this issue and been left stumped for days :exploding_head:

Greetings everyone.