diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c9c1aad..88a53e3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,6 +34,17 @@ jobs: - name: Build and run tests run: ./mvnw --batch-mode --no-transfer-progress verify + docker: + name: Backend Docker image (build) + runs-on: ubuntu-latest + timeout-minutes: 15 + steps: + - uses: actions/checkout@v7 + + # Makes sure the image used for deployment still builds (nothing is pushed). + - name: Build image + run: docker build --tag worklog-backend ./backend + frontend: name: Frontend (build and tests) runs-on: ubuntu-latest diff --git a/backend/.dockerignore b/backend/.dockerignore new file mode 100644 index 0000000..be19c40 --- /dev/null +++ b/backend/.dockerignore @@ -0,0 +1,22 @@ +# Build output and tooling +target/ +.mvn/wrapper/maven-wrapper.jar + +# Local config and secrets +.env +.env.* +application-dev.* +application-local.* + +# Editors, VCS and docs +.git +.gitignore +.gitattributes +.idea/ +.vscode/ +*.iml +HELP.md + +# Docker files themselves +Dockerfile +.dockerignore diff --git a/backend/Dockerfile b/backend/Dockerfile new file mode 100644 index 0000000..476e231 --- /dev/null +++ b/backend/Dockerfile @@ -0,0 +1,32 @@ +# syntax=docker/dockerfile:1 + +# ---- Build stage: compile and package the application ---- +FROM eclipse-temurin:21-jdk AS build +WORKDIR /workspace + +# Download dependencies first so Docker can cache this layer until the pom changes. +COPY .mvn .mvn +COPY mvnw pom.xml ./ +RUN chmod +x mvnw && ./mvnw --batch-mode --no-transfer-progress dependency:go-offline + +COPY src src +RUN ./mvnw --batch-mode --no-transfer-progress package -Dmaven.test.skip=true \ + && mv target/backend-*.jar target/app.jar + +# ---- Runtime stage: small JRE image, runs as a non-root user ---- +FROM eclipse-temurin:21-jre-alpine +RUN addgroup -S app && adduser -S app -G app +USER app +WORKDIR /app + +COPY --from=build /workspace/target/app.jar app.jar + +# Platforms like Render set PORT; the application reads it (server.port=${PORT:8080}). +ENV PORT=8080 +EXPOSE 8080 + +HEALTHCHECK --interval=30s --timeout=5s --start-period=60s --retries=3 \ + CMD wget -q -O /dev/null "http://localhost:${PORT}/health" || exit 1 + +# Use at most 75% of the container memory for the heap (the free tiers are small). +ENTRYPOINT ["java", "-XX:MaxRAMPercentage=75", "-jar", "app.jar"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f43ee95 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,50 @@ +# Local development helpers. +# +# docker compose up -d -> PostgreSQL only (run backend and frontend on your machine) +# docker compose --profile full up -d --build -> PostgreSQL + backend in containers +# docker compose down -> stop (data is kept); add -v to delete the data too +# +# Credentials below are for local development only. + +name: worklog + +services: + postgres: + image: postgres:16 + container_name: worklog-postgres + restart: unless-stopped + environment: + POSTGRES_DB: worklog + POSTGRES_USER: worklog + POSTGRES_PASSWORD: worklog_dev_password + ports: + # 5433 on the host avoids a clash with a PostgreSQL installed on your machine (usually 5432). + - "${DB_PORT:-5433}:5432" + volumes: + - worklog-postgres-data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U worklog -d worklog"] + interval: 5s + timeout: 5s + retries: 5 + + backend: + profiles: ["full"] + build: + context: ./backend + dockerfile: Dockerfile + container_name: worklog-backend + restart: unless-stopped + depends_on: + postgres: + condition: service_healthy + environment: + DB_URL: jdbc:postgresql://postgres:5432/worklog + DB_USER: worklog + DB_PASSWORD: worklog_dev_password + CORS_ORIGINS: http://localhost:4200 + ports: + - "${BACKEND_PORT:-8080}:8080" + +volumes: + worklog-postgres-data: