skip to Main Content

console-springbootI am learing Docker and encountered an error is that I have created a basic CRUD springboot application which create,getById and getAll Users then configure the Dockerfile and then docker-compose.yml. once I run docker-compose up, its starts creating images and containers and I can see that they are running perfectly ( the status is running ) but they are not creating user tables in MySQL or in simple it is not running the Springboot application just running dockerfile and docker-compose.yml..

Dockerfile
FROM openjdk:21
LABEL authors="MohammedKhadeer"
WORKDIR /usr/src/myapp
COPY . /usr/src/myapp/
#ENTRYPOINT ["java", "-jar","./target/docker-0.0.1-SNAPSHOT.jar"]
CMD ["java", "-jar","./target/docker-0.0.1-SNAPSHOT.jar"]

docker-compose.yml

services:
  mydb:
    image: mysql:latest
    container_name: mysql_database_latest
    environment:
      - MYSQL_DATABASE = docker-testing
      - MYSQL_USER =  admin
      - MYSQL_PASSWORD =  root
      - MYSQL_ROOT_PASSWORD =  root
    restart: unless-stopped
    volumes:
      - ./data:/var/lib/mysql
    ports:
      - 3333:3306
    networks:
      - myapplication
  myapp:
    build: ./docker
    container_name: docker_boot_app
    depends_on:
      - mydb
    environment:
      - spring.datasource.url=jdbc:mysql://mydb:3306/docker-testing?createDatabaseIfNotExist=true
      - spring.datasource.username=admin
      - spring.datasource.password=root
    ports:
      - 8081:8080
    networks:
      - myapplication
networks:
  myapplication:

application.property
spring.application.name=docker

#spring.datasource.url=jdbc:mysql://localhost:3306/docker-testing
#spring.datasource.username=admin
#spring.datasource.password=root

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

enter image description here
enter image description here

2

Answers


  1. Your app is not creating the tables because spring.jpa.hibernate.ddl-auto is set to update, which only updates the existing schema.
    If you want to create the tables you need to set the spring.jpa.hibernate.ddl-auto property to create or create-drop.

    create – will create the tables if they don’t exists.

    create-drop – will create tables when app is up and drop then when app is shutdown.

    Login or Signup to reply.
  2. In the Docker file, you are telling docker to run the jar file from the target
    folder. Each time you make a change to your Java files you have to rebuild this file with Maven. Try running mvn package and give it another chance.

    To avoid doing this every time, you can tell docker to run mvn package as well.

    HOPE THIS HELPS

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search