-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add multi-stage Dockerfile for building and running app #195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| .github | ||
| .mvn | ||
| .vscode | ||
| target | ||
| .codacy.yml | ||
| .gitignore | ||
| azure-pipelines.yml | ||
| CODE_OF_CONDUCT.md | ||
| codecov.yml | ||
| CONTRIBUTING.md | ||
| LICENSE | ||
| mvnw | ||
| mvnw.cmd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # ------------------------------------------------------------------------------ | ||
| # Stage 1: Builder | ||
| # This stage builds the application and its dependencies. | ||
| # ------------------------------------------------------------------------------ | ||
| FROM maven:3.9-eclipse-temurin-21-alpine AS builder | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| # Copy pom.xml and download dependencies. This will be cached until pom.xml | ||
| # changes, speeding up the build process. | ||
| COPY pom.xml . | ||
| RUN mvn dependency:go-offline -B | ||
|
|
||
| # Copy source code and build the application, skipping tests for faster builds. | ||
| COPY src ./src | ||
| RUN mvn clean package -DskipTests | ||
|
|
||
| # ------------------------------------------------------------------------------ | ||
| # Stage 2: Runtime | ||
| # This stage creates the final, minimal image to run the application. | ||
| # ------------------------------------------------------------------------------ | ||
| FROM eclipse-temurin:21-jdk-alpine AS runtime | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| # Install curl for health check | ||
| RUN apk add --no-cache curl | ||
|
|
||
| # Metadata labels for the image. These are useful for registries and inspection. | ||
| LABEL org.opencontainers.image.title="🧪 RESTful Web Service with Spring Boot" | ||
| LABEL org.opencontainers.image.description="Proof of Concept for a RESTful Web Service made with JDK 21 (LTS) and Spring Boot 3" | ||
| LABEL org.opencontainers.image.licenses="MIT" | ||
| LABEL org.opencontainers.image.source="https://github.com/nanotaboada/java.samples.spring.boot" | ||
|
|
||
| # https://rules.sonarsource.com/docker/RSPEC-6504/ | ||
|
|
||
| # Copy application JAR file from the builder stage | ||
| COPY --from=builder /app/target/*.jar ./app.jar | ||
|
|
||
| # Copy metadata docs for container registries (e.g.: GitHub Container Registry) | ||
| COPY --chmod=444 README.md ./ | ||
| COPY --chmod=555 assets/ ./assets/ | ||
|
|
||
| # Copy entrypoint and healthcheck scripts | ||
| COPY --chmod=555 scripts/entrypoint.sh ./entrypoint.sh | ||
| COPY --chmod=555 scripts/healthcheck.sh ./healthcheck.sh | ||
|
|
||
| # Add system user | ||
| RUN addgroup -S spring && \ | ||
| adduser -S -G spring spring | ||
|
|
||
| USER spring | ||
|
|
||
| EXPOSE 9000 | ||
| EXPOSE 9001 | ||
|
|
||
| HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \ | ||
| CMD ["./healthcheck.sh"] | ||
|
|
||
| ENTRYPOINT ["./entrypoint.sh"] | ||
| CMD ["java", "-jar", "./app.jar"] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| # Sample RESTful Web Service with Spring Boot | ||
| # 🧪 RESTful Web Service with Spring Boot | ||
|
|
||
| ## Status | ||
|
|
||
|
|
@@ -27,6 +27,30 @@ http://localhost:9000/swagger/index.html | |
|
|
||
|  | ||
|
|
||
| ## Container | ||
|
|
||
| ### Docker Compose | ||
|
|
||
| This setup uses [Docker Compose](https://docs.docker.com/compose/) to build and run the app | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line length |
||
|
|
||
| #### Build the image | ||
|
|
||
| ```bash | ||
| docker compose build | ||
| ``` | ||
|
|
||
| #### Start the app | ||
|
|
||
| ```bash | ||
| docker compose up | ||
| ``` | ||
|
|
||
| #### Stop the app | ||
|
|
||
| ```bash | ||
| docker compose down | ||
| ``` | ||
|
|
||
| ## Credits | ||
|
|
||
| The solution has been coded using [Visual Studio Code](https://code.visualstudio.com/) with the [Extension Pack for Java](https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| name: java-samples-spring-boot | ||
|
|
||
| services: | ||
| api: | ||
| image: java-samples-spring-boot | ||
| container_name: spring-app | ||
| build: | ||
| context: . | ||
| dockerfile: Dockerfile | ||
| ports: | ||
| - "9000:9000" | ||
| - "9001:9001" | ||
| environment: | ||
| - SPRING_PROFILES_ACTIVE=production | ||
| restart: unless-stopped |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| #!/bin/sh | ||
| set -e | ||
|
|
||
| echo "✔ Starting Spring Boot container..." | ||
|
|
||
| echo "✔ Server Port: ${SERVER_PORT:-9000}" | ||
| echo "✔ Management Port: ${MANAGEMENT_PORT:-9001}" | ||
| echo "✔ Active Profile(s): ${SPRING_PROFILES_ACTIVE:-default}" | ||
|
|
||
| echo "🚀 Launching Spring Boot app..." | ||
| exec "$@" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| #!/bin/sh | ||
| set -e | ||
|
|
||
| # Use Actuator endpoint for health check | ||
| ACTUATOR_HEALTH_URL="http://localhost:9001/actuator/health" | ||
|
|
||
| # Curl with fail-fast behavior and silent output | ||
| curl --fail --silent --show-error --connect-timeout 1 --max-time 2 "$ACTUATOR_HEALTH_URL" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.