When to use this stack
This stack runs a Spring Boot 3 application on Java 21 with MariaDB 11.4 as its relational database. Spring Boot reads its database settings from environment variables, so the same jar can run against a local database on a laptop and a production database on a server without changing any code. Compose starts MariaDB first, waits until its healthcheck passes, and then starts the Java service. MariaDB is a drop-in relative of MySQL, which makes it a common choice for Java services that need a reliable SQL engine under an open license.
Choose it when your team already builds services with Spring, uses JPA or JDBC against a SQL database, and wants a predictable deployment on one host. It fits internal APIs, back-office systems, e-commerce backends and reporting services. Do not choose it when the workload needs a horizontally scaled cluster, multi-region replication or a database with specialized features; those need a managed database platform and a container orchestrator. Also avoid putting the jar build inside a slow runtime image if your build machine is limited, because the JVM and Maven together use a lot of memory.
The Dockerfile uses a Java 21 JRE image, which is smaller than a JDK image, and runs the jar as the entrypoint. It expects that you have already built the jar with Maven or the Maven wrapper in the ./app folder. The jar must include the MariaDB driver as a dependency, which is org.mariadb.jdbc:mariadb-java-client in your pom.xml. The Spring profile is set to prod in the compose file, so put production-only settings in application-prod.properties.
docker-compose.yml
services:
app:
build: ./app
restart: unless-stopped
depends_on:
db:
condition: service_healthy
ports:
- "8080:8080"
environment:
SPRING_PROFILES_ACTIVE: prod
SPRING_DATASOURCE_URL: jdbc:mariadb://db:3306/${MARIADB_DATABASE}
SPRING_DATASOURCE_USERNAME: ${MARIADB_USER}
SPRING_DATASOURCE_PASSWORD: ${MARIADB_PASSWORD}
JAVA_TOOL_OPTIONS: "-XX:MaxRAMPercentage=75.0"
db:
image: mariadb:11.4
restart: unless-stopped
environment:
MARIADB_ROOT_PASSWORD: ${MARIADB_ROOT_PASSWORD}
MARIADB_DATABASE: ${MARIADB_DATABASE}
MARIADB_USER: ${MARIADB_USER}
MARIADB_PASSWORD: ${MARIADB_PASSWORD}
volumes:
- mariadb_data:/var/lib/mysql
healthcheck:
test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
interval: 10s
timeout: 5s
retries: 10
start_period: 30s
volumes:
mariadb_data:# MariaDB root password, used only inside the db container
MARIADB_ROOT_PASSWORD=CHANGE_ME_root_password
# Application database and its user, created on the first start
MARIADB_DATABASE=appdb
MARIADB_USER=appuser
MARIADB_PASSWORD=CHANGE_ME_app_db_passwordDockerfile (app/Dockerfile)
Build the jar first, for example with ./mvnw -DskipTests package, then place this file in ./app. The build context must contain the target folder.
FROM eclipse-temurin:21-jre-alpine
WORKDIR /app
COPY target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "/app/app.jar"].env
# MariaDB root password, used only inside the db container
MARIADB_ROOT_PASSWORD=CHANGE_ME_root_password
# Application database and its user, created on the first start
MARIADB_DATABASE=appdb
MARIADB_USER=appuser
MARIADB_PASSWORD=CHANGE_ME_app_db_passwordStart and verify
# Build the jar on the host first (requires Java 21 and Maven or the wrapper)
cd app && ./mvnw -DskipTests package && cd ..
docker compose config --quiet
# Build the image and start both services
docker compose up -d --build
# db must be healthy before app starts
docker compose ps
# Check the Spring log for "Started Application" and any datasource errors
docker compose logs --tail=80 app
# Call an endpoint from your controller, or check the default error page
curl -i http://localhost:8080/If you use Spring Boot Actuator, add the spring-boot-starter-actuator dependency and call curl http://localhost:8080/actuator/health, which should return a status of UP.
Production checklist
- Put the service behind HTTPS with a reverse proxy, and set server.forward-headers-strategy=framework in the prod profile.
- Create a database user with only the rights the application needs, and keep the MariaDB root password out of the application.
- Keep port 3306 unpublished, which this file already does.
- Use Flyway or Liquibase for schema changes, and set spring.jpa.hibernate.ddl-auto to validate or none in production.
- Set JAVA_TOOL_OPTIONS memory limits and a matching memory limit on the container, so the JVM does not get killed by the host.
- Expose only the health endpoint of Actuator publicly, and keep other endpoints behind authentication.
- Back up mariadb_data with mariadb-dump on a schedule, and test a restore into a new container.
- Pin eclipse-temurin:21-jre-alpine and mariadb:11.4 to versions you have tested, and rebuild after security updates.
Common problems
- Symptom: “Communications link failure” at startup. Cause: The app started before MariaDB was ready, or the URL uses localhost. Fix: Keep depends_on with service_healthy, and use db as the host name in SPRING_DATASOURCE_URL.
- Symptom: “Access denied for user” in the Spring log. Cause: SPRING_DATASOURCE_PASSWORD does not match MARIADB_PASSWORD, or the volume was created earlier with other values. Fix: Make the values match, or change the password inside MariaDB with ALTER USER on a test install.
- Symptom: “no main manifest attribute” when the container starts. Cause: The copied jar is the plain library jar, not the Spring Boot repackaged jar. Fix: Build with the spring-boot-maven-plugin repackage goal, which runs in the package phase by default.
- Symptom: The container is killed with exit code 137. Cause: The JVM heap is too large for the memory limit. Fix: Keep MaxRAMPercentage at 75 or lower, and set a container memory limit with enough headroom.
- Symptom: The schema is not created on the first start. Cause: The application uses validate mode, but there are no migration scripts. Fix: Add Flyway migration files under src/main/resources/db/migration, or set ddl-auto to update on a test install only.
Variations
- MySQL instead of MariaDB: Use the mysql:8.4 image, switch the URL to jdbc:mysql://db:3306, and use the MySQL Connector/J driver. Keep the same environment variable names.
- Caddy or Nginx in front: Add a reverse proxy service that terminates HTTPS and forwards to app:8080, then remove the public 8080 mapping.
- Multi-stage build: Replace the copied jar with a Maven build stage based on maven:3.9-eclipse-temurin-21, so the image is built from source inside Docker. This is slower, but nothing needs to be built on the host.
Questions
Where must the Spring Boot jar be?
The Dockerfile copies target/*.jar from the ./app folder, so build the jar there first with ./mvnw -DskipTests package or mvn package. The jar must be an executable Spring Boot jar.
Why does Spring fail with 'Communications link failure'?
The app started before MariaDB accepted connections, or SPRING_DATASOURCE_URL points at the wrong host. Inside compose the host is db, not localhost. The depends_on healthcheck prevents most of these cases.
Can I use MySQL instead of MariaDB?
Yes, with the MySQL Connector/J driver and a jdbc:mysql URL. This file uses MariaDB and its Connector/J driver because the mariadb image has a built-in healthcheck script.
Adapt this stack
Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.
Open in builderRelated stacks
Laravel + PostgreSQL + Redis
Run a Laravel app on Apache with PostgreSQL 16 and Redis 7 in Docker Compose, with a Dockerfile, queue and cache settings, volumes and a production checklist.
Next.js + PostgreSQL + Redis
Containerize a Next.js app with PostgreSQL 16 and Redis 7 in Docker Compose, using a minimal Dockerfile, healthchecks, named volumes and a production checklist.
Nuxt + PostgreSQL
Run a Nuxt 3 app built with Nitro and PostgreSQL 16 in Docker Compose, with a Node 22 Dockerfile, a database healthcheck, persistent data and a checklist.
Strapi + PostgreSQL
Run a Strapi 5 headless CMS with PostgreSQL 16 in Docker Compose, using a Node 22 image, secret variables, persistent uploads and a production checklist.
Browse more Web & Frameworks stacks or the full library.