Home > front end >  Run Java application (Spring Boot MySQL) by Docker compose
Run Java application (Spring Boot MySQL) by Docker compose

Time:02-04

I'm trying to run an application on my local machine using Docker. I also want to run the application on a remote server. Without Docker in Intelij Idea everything works fine.

On the local machine I installed Docker. Created a Dockerfile and docker-compose.yml in the root folder of the project. Some errors I made in these files. I think, the communication as I understand it between database and my application is not working.

Maybe something wrong with MYSQL_ROOT_PASSWORD, because i really don't know what the password from MYSQL_ROOT_PASSWORD. I just know username and password. I run mysql by MySQLWorkbanch, i create here commection named "my_connection", there is user named "bestuser" and login "bestuser".

enter image description here

This is my structure of project

enter image description here

I tried combine ports, 8080:8080, 8081:8080, 3306:3306, 3307:3306.. But nothing.

MySQL image running well, but app image get alot of errors.

enter image description here

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/crm?useSSL=false
spring.datasource.username=bestuser
spring.datasource.password=bestuser
#spring.datasource.password=T#3;jbY7L@m6
spring.sql.init.mode=always
#spring.datasource.initialization-mode=always
#server.port=8080




#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jms.listener.acknowledge-mode=auto





info.name=CRM system. Accounting for sales, merchandise, payroll
info.description=CRM system. Accounting for sales, merchandise, payroll
info.author=Vitaliy Shvetsov


#spring.security.user.name=vitaliy
#spring.security.user.password=vitaliy



spring.view.prefix:/WEB-INF/
spring.view.suffix:.jsp
spring.view.view-names:views/*
#spring.thymeleaf.prefix=/webapp/WEB-INF/
#spring.thymeleaf.view-names:views/*





#management.endpoints.web.exposure.include=*


#spring.mvc.view.prefix: /WEB-INF/views/
#spring.mvc.view.suffix: .jsp
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.prefix=classpath:/templates/

# ==============================================================
# = Keep the connection alive if idle for a long time (needed in production)
# ==============================================================
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

# ==============================================================
# = Show or not log for each sql query
# ==============================================================
#spring.jpa.show-sql = true

# ==============================================================
# = Hibernate ddl auto (create, create-drop, update)
# ==============================================================
spring.jpa.hibernate.ddl-auto = update

# ==============================================================
# = The SQL dialect makes Hibernate generate better SQL for the chosen database
# ==============================================================
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

spring.main.allow-circular-references=true


docker-compose.yml

version: '3.7'

# Define services
services:
  # App backend service
  app-server:
    # Configuration for building the docker image for the backend service
    build:
      context: . # Use an image built from the specified dockerfile in the `springboot-app-server` directory.
      dockerfile: Dockerfile
    ports:
      - "8081:8080" # Forward the exposed port 4000 on the container to port 4000 on the host machine
    restart: always
    depends_on:
      - db # This service depends on mysql. Start that first.
    environment: # Pass environment variables to the service
      SPRING_DATASOURCE_URL: jdbc:mysql://localhost:3306/crm?useSSL=false
      SPRING_DATASOURCE_USERNAME: bestuser
      SPRING_DATASOURCE_PASSWORD: bestuser


  # Database Service (Mysql)
  db:
    image: mysql:8.0
    ports:
      - "3307:3306"
    restart: always
    environment:
      MYSQL_DATABASE: crm
      MYSQL_USER: bestuser
      MYSQL_PASSWORD: bestuser
      MYSQL_ROOT_PASSWORD: bestuser

Dockerfile

# Use a Java base image
FROM openjdk:11

# Set the working directory
WORKDIR /

# Copy the application jar to the container
COPY target/crm-0.0.1-SNAPSHOT.war .
EXPOSE 8080
# Set the environment variables
ENV JAVA_OPTS=""

# Define the command to run the application
CMD ["java", "-jar", "crm-0.0.1-SNAPSHOT.war"]

Please help me figure out what I did wrong, thank you in advance!

CodePudding user response:

Is the application referring to the right database ?

No. Check SPRING_DATASOURCE_URL in docker-compose file, localhost in the url points to your local db. Change it to db (service name you have given in the docker-compose file for mysql).

  • Related