Improve Docker and CI/CD configuration with best practices

- Dockerfile: Add non-root user, healthcheck, optimized layer caching
- docker-compose.yml: Add healthchecks, data volumes, networking, better configuration
- CI/CD: Add multi-platform builds, image testing, multiple tags, metadata
- .dockerignore: Comprehensive exclusions for faster builds

These improvements enhance security, reliability, and maintainability
following Docker and Spring Boot best practices.

Signed-off-by: Bhavesh Khandelwal <bhaveshkhandelwal1232@gmail.com>
This commit is contained in:
Bhavesh Khandelwal 2025-12-14 10:49:10 +05:30
parent 0200c9fe61
commit bdbece8d3f
4 changed files with 164 additions and 15 deletions

View file

@ -6,15 +6,16 @@ on:
pull_request:
branches: [ main ]
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
@ -32,6 +33,19 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
- name: Log in to GitHub Container Registry
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
uses: docker/login-action@v3
@ -45,7 +59,24 @@ jobs:
with:
context: .
push: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }}
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64
- name: Test Docker image
if: github.event_name == 'pull_request'
run: |
docker build -t spring-petclinic-test .
docker run -d --name petclinic-test -p 8080:8080 \
-e SPRING_PROFILES_ACTIVE=h2 \
spring-petclinic-test
echo "Waiting for application to start..."
sleep 45
echo "Testing health endpoint..."
curl -f http://localhost:8080/actuator/health || exit 1
echo "Health check passed!"
docker stop petclinic-test
docker rm petclinic-test