Why can’t I build my Spring Boot app using this Dockerfile on my Mac?

0
3
Asked By CuriousCat42 On

I'm having trouble building a Docker image for my Spring Boot application on my Mac. Here's the Dockerfile I'm using:

```dockerfile
FROM maven:3.9.9-eclipse-temurin-21-jammy AS builder

WORKDIR /app

COPY pom.xml .

RUN mvn dependency:go-offline -B

COPY src ./src

RUN mvn clean package

FROM openjdk:21-jdk AS runner

WORKDIR /app

COPY --from=builder ./app/target/patient-service-0.0.1-SNAPSHOT.jar ./app.jar

EXPOSE 4000

ENTRYPOINT ["java", "-jar", "app.jar"]
```

Could someone help me understand what might be going wrong?

4 Answers

Answered By DevNinja99 On

Running `mvn package` inside the Dockerfile can be useful since it streamlines the process by pulling in dependencies automatically. But, you might prefer building locally and just copying the JAR or WAR file for a lighter image. That way, you won't have to rebuild every time you change your code.

CodeWizard88 -

Totally get that, but I think that way you also miss out on some caching optimizations you can get from building in Docker.

Answered By TechWhiz101 On

You might run into issues if your Mac uses ARM architecture versus x64. It's worth checking if the images you’re using are compatible with your CPU type.

SwiftCoder77 -

That's right! I had a similar problem, and it turned out to be related to the CPU architecture.

Answered By SpringStyle On

I got an error during the Docker build process:

```plaintext
Dockerfile:7
--------------------
5 | COPY pom.xml .
6 |
7 | >>> RUN mvn dependency:go-offline -B
8 |
9 | COPY src ./src
--------------------
ERROR: failed to solve: process "/bin/sh -c mvn dependency:go-offline -B" did not complete successfully: exit code: 134
```
What do you think the issue might be?

MavenMaster -

It seems like a tool issue. But check the Maven logs to see if they provide more context!

Answered By ContainerGuru On

You could try changing the order of operations in your Dockerfile. Set your work directory to `/app` after copying your files there. I had a similar issue and rearranging commands helped.

CuriousCat42 -

Thanks for the tip! I made that change and realized it was also a compatibility issue with the Maven image and OpenJDK version.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.