How to convert a Spring-Boot web service into a Docker image?

前端 未结 2 1858
日久生厌
日久生厌 2021-01-07 01:32

I want to access my website from a Docker container but I can\'t. The steps I am trying to implement are as follows. After all the steps I can\'t access the http://loc

2条回答
  •  生来不讨喜
    2021-01-07 01:53

    To fully address your question (dockerize a Spring Boot project and browse the corresponding webapp in the local browser during, say, development phase), three independent tasks have to be done:

    1. Leverage the build of the Docker image by using a Dockerfile that benefits from Docker's cache mechanism (to avoid re-downloading Maven dependencies from scratch every time, and thereby speed-up the build)

    2. Make sure the Spring Boot app listens to the specified port for the 0.0.0.0 special IP, not localhost;

    3. And finally publish the given port so that you can run for example:

      $ xdg-open http://localhost:8080/index
      

    Step 3 is well explained in @Poger's answer, so I'll elaborate a bit more on the steps 1 and 2 only:

    1. I have proposed a Dockerfile in this SO thread: How to cache maven dependencies in Docker, inspired by this blog article, that is applicable for Java/Maven projects in general (not only Spring Boot projects):

      # our base build image
      FROM maven:3-jdk-8 as maven
      
      WORKDIR /app
      
      # copy the Project Object Model file
      COPY ./pom.xml ./pom.xml
      
      # fetch all dependencies
      RUN mvn dependency:go-offline -B
      
      # copy your other files
      COPY ./src ./src
      
      # build for release
      # NOTE: my-project-* should be replaced with the proper prefix
      RUN mvn package && cp target/my-project-*.jar app.jar
      
      
      # smaller, final base image
      FROM openjdk:8-jre-alpine
      # OPTIONAL: copy dependencies so the thin jar won't need to re-download them
      # COPY --from=maven /root/.m2 /root/.m2
      
      # set deployment directory
      WORKDIR /app
      
      # copy over the built artifact from the maven image
      COPY --from=maven /app/app.jar ./app.jar
      
      # set the startup command to run your binary
      CMD ["java", "-jar", "/app/app.jar"]
      

      but to refine it further, note that it's recommended to pass an extra system property java.security.egd:

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

      or if you prefer ENTRYPOINT over CMD:

      ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app/app.jar"]
      
    2. As mentioned in the SO thread How do I access a spring app running in a docker container?, in the context of a containerized application, localhost should be avoided and replaced with 0.0.0.0 most of the time (namely, if the app should act as a web service replying to incoming requests from outside the container).

      To sum up, you should just try adding the following line in your application.properties file:

      server.address=0.0.0.0
      

提交回复
热议问题