diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..fb764bc --- /dev/null +++ b/.dockerignore @@ -0,0 +1,22 @@ +# Dependencies +node_modules +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* + +# Build output +build +dist + +# Local env files +.env* + +# VCS +.git +.gitignore + +# Editor/OS +.DS_Store +.vscode +Thumbs.db \ No newline at end of file diff --git a/.env.example b/.env.example index e67d4e5..50d6a99 100644 --- a/.env.example +++ b/.env.example @@ -1 +1,27 @@ -REACT_APP_API_URL="http://localhost:4000" \ No newline at end of file +## Setup: copy this file to .env then run: docker compose up --build -d +## Replace API_TOKEN & JWT_SIGNING_KEY with secure random keys (>64 bytes for HS512). +## Generate command: openssl rand -hex 96 + +POSTGRES_USER=appuser +POSTGRES_PASSWORD=apppassword +POSTGRES_DB=appdb +DB_PORT=5434 + +# Server project expected in sibling folder to this client repo +API_BUILD_CONTEXT=../csharp-team-dev-server-team-4/exercise.wwwapi +API_DOCKERFILE=Dockerfile + +API_IMAGE_NAME=team-dev-api +API_PORT=3001 +ASPNETCORE_ENVIRONMENT=Development +ASPNETCORE_URLS=http://+:8080 +# HS512 signing keys must be >64 bytes. Replace these dev values. +JWT_SIGNING_KEY=change-me-jwt-signing-key-min-65-bytes +API_TOKEN=change-me-api-token-min-65-bytes + +WEB_IMAGE_NAME=team-dev-client:web +WEB_PORT=3000 + +REACT_APP_API_URL=http://localhost:3001 + +COMPOSE_PROJECT_NAME=team-dev \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..921d5a6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ + +FROM node:20-alpine AS builder + +WORKDIR /app + + +COPY package*.json ./ + +RUN npm ci --no-audit --no-fund || npm install --no-audit --no-fund + + +COPY . . + +ARG REACT_APP_API_URL + +ENV REACT_APP_API_URL=$REACT_APP_API_URL + +RUN npm run build + + +FROM nginx:alpine + +COPY nginx.conf /etc/nginx/conf.d/default.conf + +COPY --from=builder /app/build /usr/share/nginx/html + +EXPOSE 8080 + +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/README.md b/README.md index 60494ec..e10cd9e 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ Client repository for team dev project. 2. Make sure the `REACT_APP_API_URL` environment variable in the `.env` file contains the URL of the server app on your machine 3. `npm ci` to install dependencies 4. `npm start` to run the app. The server must also be running on your machine +5. `docker compose up --build` to start up the docker containers for use +6. `docker compose down` to stop running docker ### Project Management diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..f812d5e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,42 @@ +services: + db: + image: postgres:16-alpine + environment: + - POSTGRES_USER=${POSTGRES_USER:-appuser} + - POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-apppassword} + - POSTGRES_DB=${POSTGRES_DB:-appdb} + ports: + - "${DB_PORT:-5434}:5432" + volumes: + - pgdata:/var/lib/postgresql/data + + api: + build: + context: ${API_BUILD_CONTEXT:-../csharp-team-dev-server-team-4/exercise.wwwapi} + dockerfile: ${API_DOCKERFILE:-Dockerfile} + image: ${API_IMAGE_NAME:-team-dev-api} + environment: + - ConnectionStrings__DefaultConnection=Host=db;Port=5432;Database=${POSTGRES_DB:-appdb};Username=${POSTGRES_USER:-appuser};Password=${POSTGRES_PASSWORD:-apppassword};SSL Mode=Disable + - Token=${API_TOKEN:-dev-token-please-change} + - JWT__SigningKey=${JWT_SIGNING_KEY:-dev-super-secret-signing-key} + - ASPNETCORE_ENVIRONMENT=${ASPNETCORE_ENVIRONMENT:-Development} + - ASPNETCORE_URLS=${ASPNETCORE_URLS:-http://+:8080} + ports: + - "${API_PORT:-3001}:8080" + depends_on: + - db + + web: + build: + context: . + dockerfile: Dockerfile + args: + REACT_APP_API_URL: ${REACT_APP_API_URL:-http://localhost:${API_PORT:-3001}} + image: ${WEB_IMAGE_NAME:-team-dev-client:web} + ports: + - "${WEB_PORT:-3000}:80" + depends_on: + - api + +volumes: + pgdata: \ No newline at end of file diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..d0de181 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,15 @@ +server { + listen 8080; + server_name _; + + root /usr/share/nginx/html; + index index.html; + + location / { + try_files $uri /index.html; + } + + gzip on; + gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css application/json image/svg+xml; + gzip_min_length 256; +}