简体   繁体   中英

Docker-Compose for local Spring Boot Development

I have the following docker-compose file setup along with an associated Dockerfile.

docker-compose.yml

version: '3'
services:
   vault:   
   springboot-app:
    build:
      dockerfile: ./Dockerfile
    restart: always
    ports:
      - "8080:8080"
    environment:
       - "SPRING_PROFILES_ACTIVE=local"

Dockerfile

FROM maven:3.5.2-jdk-8-alpine AS MAVEN_BUILD

COPY pom.xml /build/
COPY src /build/src/

WORKDIR /build/

RUN mvn -B -U -e clean verify 

FROM openjdk:8-jre-alpine

WORKDIR /

COPY --from=MAVEN_BUILD /build/target/MyApp-0.0.1-SNAPSHOT.jar /

ENTRYPOINT ["java", "-jar", "MyApp-0.0.1-SNAPSHOT.jar"]

My application is now running fine when I run docker-compose up. I was wondering what change do I need to make in order to have the spring boot app rebuild upon code changes? In my pom file, I have the spring-boot-maven-plugin setup as follows and I have specified the spring-boot-devtools dependency

       <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludeDevtools>false</excludeDevtools>
            </configuration>
        </plugin>

Any help is greatly appreciated on this

Thanks Damien

For production applications you can use a Java Docker image along with the compiled .jar file.

For local development use a Maven Docker image instead. It can run the source code and compile whenever you make a change to the application. You can use a docker-compose.yml file such as:

version: '3.1'
services:
  backend:
    image: maven:3.6.3-jdk-8
    command: mvn spring-boot:run
    ports:
      - 8080:8080
    volumes:
      - .:/usr/src/mymaven:rw
    working_dir: /usr/src/mymaven

Be sure to set the volumes to map your source code to the Docker container, and to set the work directory for Maven to find your pom.xml.

might not be answer but

do you have any specific use case to use 2 docker images within DockerFile.? If, You can move out the build steps from dockerfile and build(invoke build) from script and invoke docker-compose to copy the build to docker container and start the container. You may hit issue while COPY, docker takes docker folder rather than your project folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM