Windows - Powershell
docker-compose build | docker-compose up
Linux - Bash
docker-compose build && docker-compose up
This project serves as a boilerplate for building Java 17 applications using the following technologies and tools:
- Java 17: The latest long-term support version of Java, ensuring modern language features and improvements.
- Maven: Used for project management and dependency management.
- Docker Compose: Simplifies local development by allowing you to run the entire application stack (including MySQL) within Docker containers.
- Dirk: A lightweight dependency injection library for managing object creation and dependencies.
- SLF4J with Log4j2: Provides robust and flexible logging through the SLF4J API, using Log4j2 as the logging backend.
- JDBC: Utilized for establishing a connection to relational databases and performing SQL operations.
- MySQL Database: The database for storing and managing application data.
This setup is ideal for jump-starting a Java project with dependency injection, logging, and database connectivity, all contained in a local development environment via Docker Compose.
Install JDK 17 (optional)
Install Maven (optional)
Tip: you can run Maven commands without having Maven installed.
./mvnw foo
Clone the repository
git clone https://github.com/elidaniel92/java-17-mysql-docker-compose.git
Switch to the repo folder
cd java-17-mysql-docker-compose
mvn clean install
Build the App and MySQL Container Image
docker-compose build
Started the container
docker-compose up
Since you cannot load an environment file using the build command
command, go to the MySQL Dockerfile and uncomment the environment variables section.
docker build -t db-img -f docker/Dockerfile.mysql .
docker run --name db-container -ip 3306:3306 db-img
After the message below, the server is ready for connections. You can close the terminal:
2024-09-07T22:48:15.362678Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.4.2' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
Load the local environment variables file (env/local.env) in your terminal session.
Windows - To load the env/local.env file in a PowerShell terminal session, use the command below:
Get-Content env/local.env | ForEach-Object { if ($_ -match '^(.*?)=(.*)$') { [System.Environment]::SetEnvironmentVariable($matches[1], $matches[2]) } }
Linux - To load the env/local.env file in a Bash terminal session, use the command below:
export $(grep -v '^#' env/local.env | xargs)
Generete .jar
mvn clean package
Run .jar
java -jar target/java-17-mysql-docker-compose-1.0-SNAPSHOT.jar
mvn clean compile exec:java
docker rm -f db-container
docker rmi db-img
docker rm -f store-system-app-1
docker rm -f store-system-db-1
docker rmi -f store-system-app
docker rmi -f store-system-db
This project is licensed under the MIT License - see the LICENSE file for details.