I am trying to Dockerize a Spring Boot (gradle) Application, and connect it to RabbitMQ. I had already connected the application to an H2-database before Dockerizing, with the tables automatically generated from @Entity
-classes.
I have created @Document
classes which I want to store in a MongoDB
later for an analytics component, but that's a problem for later.
I had managed to Dockerize my application by running the commands:
docker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:4.0.1-management
and then
docker-compose up --build
and made sure that it worked by testing the backend with Postman, and that I could connect to RabbitMQ in localhost:15672
.
The problem is that when I try to connect to the H2-database in the browser (through http://localhost:8080/h2-console/
), I get this errormessage:
Database "mem:fullstack_db" not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-224] 90149/90149 (Help)
application.properties:
spring.application.name=FeedApp
# H2 configurations
spring.h2.console.enabled=true
spring.datasource.driver-class-name=.h2.Driver
spring.datasource.username=admin
spring.datasource.password=admin
spring.jpa.database-platform=.hibernate.dialect.H2Dialect
spring.h2.console.settings.web-allow-others=true
# Use 'update' to automatically update schema without data loss
spring.jpa.hibernate.ddl-auto=create
# Use persistent H2 database instead of an in-memory (both work, but this one stores data)
#spring.datasource.url=jdbc:h2:mem:./fullstack_db;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.url=jdbc:h2:file:./fullstack_db;DB_CLOSE_ON_EXIT=FALSE
#spring.datasource.url=jdbc:h2:tcp://feedapp-h2/feedapp_db
# Make sure the database matches the entity definitions
spring.jpa.properties.hibernate.dialect=.hibernate.dialect.H2Dialect
# Add logging to see what's happening during entity creation
logging.level.hibernate.SQL=DEBUG
logging.level.hibernate.type.descriptor.sql.BasicBinder=TRACE
# RabbitMQ configurations
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
Dockerfile:
# Use the official OpenJDK image as a base image
FROM openjdk:21-jdk-slim
# Set the working directory inside the container
WORKDIR /FeedApp
# Copy the JAR file into the container
COPY build/libs/FeedApp-0.0.1-SNAPSHOT.jar feedapp-docker.jar
# Expose the port your application will run on (default for Spring Boot is 8080)
EXPOSE 8080
# Run the JAR file to build the image of the app
CMD ["java","-jar","feedapp-docker.jar"]
docker-compose.yml:
x-project:
name: feedapp_project
services:
feedapp:
build:
# Path to backend service
context: ./FeedApp
# Specify image- and container names for the feedapp service
image: feedapp-image1.5:latest
# Ensures that RabbitMQ is started and working before the backend
depends_on:
- rabbitmq
ports:
# Expose the backend on port 8080
- "8080:8080"
rabbitmq:
image: rabbitmq:4.0.1-management
container_name: feedapp-rabbit-docker
ports:
- "5672:5672" # RabbitMQ port
- "15672:15672" # RabbitMQ management UI on port 15672
environment:
RABBITMQ_DEFAULT_USER: guest
RABBITMQ_DEFAULT_PASS: guest
I tried to connect to a H2-database deployed through Docker by following this, but I couldn't figure out how to do it for my project. I have also tried to look at other questions (like this, and this, and this), but I cannot get it to work. Do I need to add something to the docker-compose? What am I doing wrong?