Docker Container with Java and MySql
We need now a container that should be able to execute a spring boot application (jar) and a linked mysql db.
For this we need a docker compose file and a Dockerfile
Dockerfile for Java
FROM openjdk:11
COPY ./app ./app
CMD java -Djava.net.preferIPv4Stack=true -jar /app/JavApp.war
In this example we use the java version 11 (openjdk)
Then we copy all file (actually only the jar file) from our local folder (./app) to the container folder (/app)
Finally we execute the command line to execute the jar file, adding also some java parameter
Docker compose for java and mysql
version: "3.8"
services:
mysqldb:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: thisIsTheRootPwd
MYSQL_DATABASE: fconsulting
MYSQL_USER: fconsuser
MYSQL_PASSWORD: fconspassword
ports:
- "3306:3306"
volumes:
- ./dbdata:/var/lib/mysql
app:
depends_on:
- mysqldb
build:
context: .
dockerfile: Dockerfile
ports:
- 8081:8080
environment:
JDBC_CONNECTION_STRING: 'jdbc:mysql://mysqldb:3306/fconsulting?allowPublicKeyRetrieval=true&useSSL=false'
JDBC_PWD: 'fconspassword'
JDBC_USER: 'fconsuser'
ClientId: '...'
ClientSecret: '...'
mongourl: 'mongodb+srv://...'
stdin_open: true
tty: true
In the same folder of Dockerfile we have our docker compose yaml file. The most intersting part is the environment field in which we specify all the system props needed by our java application