Damien
Damien

Reputation: 4121

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

Upvotes: 0

Views: 4892

Answers (2)

Kim T
Kim T

Reputation: 6436

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.

Upvotes: 7

Sunil Kumar
Sunil Kumar

Reputation: 320

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.

Upvotes: 1

Related Questions