Skip to content

feat: add Dockerfile and .dockerignore for containerized builds#6

Open
felipementel wants to merge 1 commit intomainfrom
add-dockerfile
Open

feat: add Dockerfile and .dockerignore for containerized builds#6
felipementel wants to merge 1 commit intomainfrom
add-dockerfile

Conversation

@felipementel
Copy link
Copy Markdown
Owner

Why

This project had no container support, making it harder to run consistently across environments. Adding a Dockerfile and .dockerignore enables 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:

  • Stage 1 (build)eclipse-temurin:21-jdk-alpine: copies the Maven wrapper and pom.xml first to leverage Docker layer caching for dependencies (dependency:go-offline), then copies src/ and produces the fat JAR via mvnw clean package -DskipTests.
  • Stage 2 (runtime)eclipse-temurin:21-jre-alpine: copies only the built JAR, runs it as a non-root user (appuser) for security, exposes port 8080, and includes a HEALTHCHECK against /actuator/health.

The .dockerignore excludes build artifacts (target/), IDE files, docs, and VCS metadata to keep the build context small and avoid accidentally invalidating cache layers.

Notes

  • The HEALTHCHECK probes /actuator/health — add spring-boot-starter-actuator to pom.xml if not already present, or update the endpoint accordingly.
  • No packages are installed in the runtime stage, so apk --no-cache is not needed there.

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>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 .dockerignore to 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
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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

Copilot uses AI. Check for mistakes.

WORKDIR /app

COPY --from=build /app/target/usuarios-api-java-0.1.0.jar app.jar
Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
COPY --from=build /app/target/usuarios-api-java-0.1.0.jar app.jar
COPY --from=build /app/target/*.jar app.jar

Copilot uses AI. Check for mistakes.
Comment on lines +26 to +29
COPY --from=build /app/target/usuarios-api-java-0.1.0.jar app.jar

RUN chown appuser:appgroup app.jar

Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants