I have a Java Spring Boot web application that I'd like to containerize using Docker. I'm having trouble getting the mvn install
command to work during the Docker build process because my project depends on some other Maven projects I've written that are installed in my local /.m2
folder but aren't available in the Maven central repository. I'd like to avoid adding these local projects to the public Maven central repository because they exist specifically to support this Spring Boot application and I'd like to keep them private.
If I wasn't using Docker, I could get around this problem by building a JAR with dependencies then deploying that .jar
file. Is there any way for me to include these local dependencies in my Docker build process?
Here's the simple Dockerfile I'm trying to run:
# Step 1: Build with Maven
FROM maven:3.5-jdk-8-alpine
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
RUN mvn clean install
# Step 2: Run jar file with Java
FROM openjdk:8-alpine
WORKDIR /usr/src/myapp
COPY --from=0 /usr/src/myapp/target/myapp-1.0-SNAPSHOT.jar ./myapp.jar
ENTRYPOINT ["java", "-jar", "myapp.jar"]
I run this build command:
docker build -t myspringapp .
And it errors with the following message:
[ERROR] Failed to execute goal on project server: Could not resolve dependencies for project
com.website:myapp:jar:1.0-SNAPSHOT: The following artifacts could not be resolved:
com.website:dependency1:jar:0.1.0, com.website:dependency2:jar:0.1.0,
com.website:dependency3:jar:0.1.0:
Could not find artifact com.website:dependency1:jar:0.1.0 in central
(https://repo.maven.apache.org/maven2) -> [Help 1]
As an alternative question, can I just run the mvn clean install
command on my development machine to produce the jar file then skip the whole Maven build part of the Docker image? Will my container still be able to replicate itself in an auto-scaling scenario? Do I lose anything by building the project separately from its Docker image/container?