Kun-Yao Lin
83 words
1 minutes
[SpringBoot] #3 Building with docker

ENVIRONMENT: zshrc, macos, 32G RAM

Compose#

  1. Create a docker file named docker-compose.yml
services:
  db:
    image: mysql
    restart: always
    ports:
      - "3306:3306"
    expose:
      - "3306"
    environment:
      - MYSQL_USER=springuser
      - MYSQL_PASSWORD=ThePassword
      - MYSQL_DATABASE=db_example
      - MYSQL_ROOT_PASSWORD=root
    volumes:
      - "./conf.d:/etc/mysql/conf.d:ro"
  1. Add dependency
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-docker-compose</artifactId>
</dependency>
  1. Set up application.properties
spring.jpa.generate-ddl=true

Build dockerfile#

First need to package the project, can use mvn clean package

FROM openjdk:21-oracle
COPY ["./target/accessing-data-mysql-complete-0.0.1-SNAPSHOT.jar", "/app/"]
COPY ["./env-config/prod", "/app/config/"]
EXPOSE 3306
WORKDIR /app
CMD ["java", "-Dspring.config.location=config/", "-jar", "backend-app.jar", "--spring.config.name=application"]

Check#

  • application.properties of production
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://db:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.generate-ddl=true
logging.file.path=./log
#spring.jpa.show-sql: true
  • port
    • docker-compose ports need compare to Dockerfile EXPOSE