简体   繁体   中英

How can I serve a multi-module spring boot application in a single Docker Container?

I have a multi-module spring boot application that looks similar to this project structure:

hello-world
│   
├── hello-world-core
│   └── HelloWorldApplication.java
│   └── pom.xml <--- hello-world-core POM
│   └── src     <--- hello-world-core src
│   └── target  <--- hello-world-core target with jar
├── hello-world-users
│   └── pom.xml <--- hello-world-users POM
│   └── src     <--- hello-world-users src
│   └── target  <--- hello-world-users target with jar
├── hello-world-website
│   └── pom.xml <--- hello-world-website POM
│   └── src     <--- hello-world-website src
│   └── target  <--- hello-world-website target with jar
└── pom.xml

Now I want to serve the application in a single Docker Container with an Image like this one:

# jdk 11 image
FROM maven:3.6-jdk-11 as builder

# Copy local code to the container image.
WORKDIR /app

# copy the project files
COPY pom.xml .

# copy your other files
COPY src ./src

# Build a release artifact.
RUN mvn package -DskipTests

# Use AdoptOpenJDK for base image.
FROM adoptopenjdk/openjdk11:alpine-slim

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

EXPOSE 8080

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

In order to serve it with this Dockerfile, I need to create a single jar out of this project.

Now I was wondering:

  1. Is there a better way to serve this kind of application in a single Docker container (without using docker-compose)?
  2. How can I create a single jar out of this structure with maven?

If you want to do this in one container, there are some ways to do that,

  1. You can have 3 different spring modules built separately as you have already and run those 3 jars in different ports in the same image. So for this you will have to change the ports which are bound in the jar using maven as we are trying to run all 3 processes in 1 container so using 8080 ( default ) will result errors if you try to do so.

server.port=9090

Something like above having in application.properties will help. So after that you will have to run them manually inside the container or attach them all to the container startup script.

Ex:

RUN java -jar hello-world-core.jar & java -jar hello-world-users.jar

So when you do this, the different jars will be serving on different ports which you mentioned in the pom files separately. So in-order to access them from outside, you will have to EXPOSE those ports as well.

  1. If you really want these to be in one container, why did you make them separate in the first place? and having 3 separate tomcats on them to execute? I mean all 3 applications are having separate tomcats inside and that's why we are struggling to merge it to 1. So why not we escape those tomcats and build war files and then install a single tomcat inside the container and put all the wars into webapps folder? Meaning you will have to put a tomcat to the docker and use it so that all the applications will be using the same tomcat and will be accessible via 8080.

  2. I believe in micro-services perspective, its good to have these separately and yes there are pros and cons doing that. And I also agree to the point which @khmarbaise made in his comment, skipping tests is not a good idea. :D

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