How to set up a dockerfile properly for Spring Boot with Maven?

Viewed 34

Based on the Spring Boot Docker guide, I set up the dockerfile as the following

FROM adoptopenjdk:8-jdk-hotspot
ARG JAR_FILE=*.jar
COPY ${JAR_FILE} application.jar
ENTRYPOINT ["java", "-jar", "application.jar"]

where the jar file is in the same directory as the dockerfile.

And I have a docker compose file as

version: '2'

services:
  app:
    image: 'my-app:latest'
    build:
      context: .
     container_name: app
     depends_on:
      - my-postgres
     environment:
       - SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/mydb
       - SPRING_DATASOURCE_USERNAME=user1
       - SPRING_DATASOURCE_PASSWORD=user.1
       - SPRING_JPA_HIBERNATE_DDL_AUTO=update

  my-postgres:
    image: 'postgres:13.1-alpine'
    container_name: db
    environment:
      - POSTGRES_USER=user1
      - POSTGRES_PASSWORD=user.1
      - POSTGRES_DB=mydb
    volumes:
      - ./postgres-data:/var/lib/postgresql/data
      # copy the sql script to create tables
      - ./sql/baseline.sql:/dshocker-entrypoint-initdb.d/baseline.sql

After building the docker image, I run the docker compose. The app isn't up though. It doesn't show up with "docker ps", but "docker ps -all".

The command shown with its docker entry,

java -jar application.jar

won't start up the app because the app requests one or two configuration files, but

mvn spring-boot:run

does.

I, however, haven't seen the line

 ENTRYPOINT ["mvn", "spring-boot:run"]

in a dockerfile yet.

How to set it up properly then?

I also run

mvn spring-boot:build-image

after upgrade the Spring Boot version. And although the docker compose command can be run, but the application container exited soon. The command is

/cnb/process/web

I haven't seen it before.

0 Answers
Related