2

I use docker-compose to launch different Spring Boot apps.

My docker images are defined with this kind of Dockerfile:

FROM openjdk:8-jdk-alpine

ADD app.jar app.jar

ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app.jar"]

However, I would like to benefit from debugging and hot-reload features using something like mvn spring-boot:run without being dependent of a particular IDE.

What is the best way to accomplish debugging and hot-reloading with Spring Boot in a Docker container without being dependent of a particular IDE?

Notes:

  • my source files are build into a jar (with Maven) which is copied to a different location containing the definition of my Docker images ; meaning my sources files are not in the docker image.
  • the reason I want to develop in the Docker container is that my apps depend on each other, and are configured in the docker-compose environment, so I cannot easily run one app alone outside the docker network and environment.

I thought of mounting a volume containing my spring boot projects in the docker containers, and then use mvn spring-boot:run in the container ; but I can't prevent maven to download all dependencies from the internet (I tried specifying a local repository containing all my dependencies without success). I would like to know if this a decent solution and how to make it work.

arthur.sw
  • 11,052
  • 9
  • 47
  • 104

2 Answers2

2

You have to follow the following steps to build and run spring boot application in docker.

Step-1 : Create a File called Dockerfile in your Project. Example

Step-2 : Write the Following Code on you Dockerfile

# Use the official maven/Java 8 image to create a build artifact.
# https://hub.docker.com/_/maven
FROM maven:3.6-jdk-11 as builder

# Copy local code to the container image.
WORKDIR /app
COPY pom.xml .
COPY src ./src

# Build a release artifact.
RUN mvn package -DskipTests

# Use AdoptOpenJDK for base image.
# It's important to use OpenJDK 8u191 or above that has container support enabled.
# https://hub.docker.com/r/adoptopenjdk/openjdk8
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM adoptopenjdk/openjdk11:alpine-slim

# Copy the jar to the production image from the builder stage.
COPY --from=builder /app/target/your-app-name*.jar /your-app-name.jar

# Run the web service on container startup.
CMD ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/your-app-name.jar"]

Step-3 : Start Your Docker Desktop Application

Step-4 : Open Your Terminal or Windows PowerShell. Then go to Project Directory.

Step-6 : Write the Following Command to create image for your application (You must have internet connection to download all dependencies).

docker build -f Dockerfile -t your-app-name .

Example

Step-7 : After image creation success. Write the following code to run the image in Docker container.

docker run -p docker-port:app-port image-name

Example

Peppermintology
  • 9,343
  • 3
  • 27
  • 51
1

Following your line of thinking you can try to copy your dependencies from a volume into the project container and then use the offline mode in something like this:

FROM openjdk:8-jdk-alpine

WORKDIR /app

# copy the Project Object Model file
COPY ./pom.xml ./pom.xml

# copy your dependencies
COPY app.jar app.jar

# copy your other files
COPY ./src ./src

# Set fetch mode to offline to avoid downloading them from the internet
RUN mvn dependency:go-offline

Apparently it's also possible to configure the offline mode globally by setting the offline property in the ~/.m2/settings.xml file, you can setup that and copy your m2 file and reference it when running the container

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      https://maven.apache.org/xsd/settings-1.0.0.xsd">
  <offline>true</offline>
</settings>
mvn -Dmaven.repo.local=~/.m2/settings.xml ...

You can find more information here:

Richard
  • 640
  • 7
  • 13