feat: add Dockerfile and .dockerignore for containerized builds#6
feat: add Dockerfile and .dockerignore for containerized builds#6felipementel wants to merge 1 commit intomainfrom
Conversation
Multi-stage build with eclipse-temurin:21 (JDK for build, JRE for runtime). Includes .dockerignore to optimize build context. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds initial containerization support for the Spring Boot API so it can be built and run reproducibly via Docker using a multi-stage build.
Changes:
- Introduces a multi-stage
Dockerfile(build with Maven wrapper; runtime with non-root user and healthcheck). - Adds a
.dockerignoreto reduce build context size and improve caching.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| Dockerfile | Multi-stage build/runtime image definition for the API container. |
| .dockerignore | Excludes common build artifacts/IDE/VCS files from Docker build context. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| EXPOSE 8080 | ||
|
|
||
| HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \ | ||
| CMD wget --no-verbose --tries=1 --spider http://localhost:8080/actuator/health || exit 1 |
There was a problem hiding this comment.
The HEALTHCHECK targets /actuator/health, but this repo currently doesn't include spring-boot-starter-actuator (and the codebase already exposes /health/live and /health/ready). As written, containers will be marked unhealthy due to a 404. Update the HEALTHCHECK to hit an existing endpoint (e.g. /health/ready) or add Actuator and ensure the endpoint is enabled in config.
| CMD wget --no-verbose --tries=1 --spider http://localhost:8080/actuator/health || exit 1 | |
| CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health/ready || exit 1 |
|
|
||
| WORKDIR /app | ||
|
|
||
| COPY --from=build /app/target/usuarios-api-java-0.1.0.jar app.jar |
There was a problem hiding this comment.
The runtime stage copies a version-pinned JAR name (usuarios-api-java-0.1.0.jar). This will break the Docker build whenever the project version changes or if the artifact name is customized. Consider producing a stable output name during the build (e.g., configure Maven finalName to app) and copy that, or otherwise avoid hard-coding the version in the Dockerfile.
| COPY --from=build /app/target/usuarios-api-java-0.1.0.jar app.jar | |
| COPY --from=build /app/target/*.jar app.jar |
| COPY --from=build /app/target/usuarios-api-java-0.1.0.jar app.jar | ||
|
|
||
| RUN chown appuser:appgroup app.jar | ||
|
|
There was a problem hiding this comment.
You can avoid the extra layer from RUN chown ... by using COPY --chown=appuser:appgroup --from=build ... app.jar when copying the JAR into the runtime image. This keeps the image slightly smaller and the Dockerfile simpler.
| COPY --from=build /app/target/usuarios-api-java-0.1.0.jar app.jar | |
| RUN chown appuser:appgroup app.jar | |
| COPY --chown=appuser:appgroup --from=build /app/target/usuarios-api-java-0.1.0.jar app.jar |
Why
This project had no container support, making it harder to run consistently across environments. Adding a
Dockerfileand.dockerignoreenables building and running the API as a Docker image with a reproducible, production-ready setup.What changed
A multi-stage build approach is used to keep the final image lean:
build) —eclipse-temurin:21-jdk-alpine: copies the Maven wrapper andpom.xmlfirst to leverage Docker layer caching for dependencies (dependency:go-offline), then copiessrc/and produces the fat JAR viamvnw clean package -DskipTests.runtime) —eclipse-temurin:21-jre-alpine: copies only the built JAR, runs it as a non-root user (appuser) for security, exposes port8080, and includes aHEALTHCHECKagainst/actuator/health.The
.dockerignoreexcludes build artifacts (target/), IDE files, docs, and VCS metadata to keep the build context small and avoid accidentally invalidating cache layers.Notes
HEALTHCHECKprobes/actuator/health— addspring-boot-starter-actuatortopom.xmlif not already present, or update the endpoint accordingly.apk --no-cacheis not needed there.