diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..38ca1e1 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +.github/ +.next/ +.vscode/ + +node_modules/ + diff --git a/.gitignore b/.gitignore index 9332b66..7105a88 100644 --- a/.gitignore +++ b/.gitignore @@ -29,6 +29,7 @@ yarn-error.log* .env*.local .env .env.local +.env.production # vercel .vercel diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3bc3cbb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +# Build +FROM node:18-alpine AS builder +WORKDIR /app + +COPY package*.json ./ +COPY . . + +RUN npm install +RUN cd src && npx prisma generate && cd .. +RUN npm run build + +# Production stage +FROM node:18-alpine AS runner +WORKDIR /app + +ARG NODE_ENV=production +ENV NODE_ENV=${NODE_ENV} + +COPY --from=builder /app/next.config.ts ./ +COPY --from=builder /app/package*.json ./ +COPY --from=builder /app/pnpm-lock.yaml ./ +COPY --from=builder /app/public ./public +COPY --from=builder /app/.next ./.next +COPY --from=builder /app/node_modules ./node_modules +COPY --from=builder /app/src ./src + +EXPOSE 3000 + +CMD ["sh", "-c", "env | grep DATABASE && npm start"] diff --git a/docker-compose-init.yml b/docker-compose-init.yml new file mode 100644 index 0000000..a8ef625 --- /dev/null +++ b/docker-compose-init.yml @@ -0,0 +1,24 @@ +version: '3' + +services: + nginx: + image: nginx:alpine + container_name: nginx-init + ports: + - '80:80' + volumes: + - certbot-www:/var/www/certbot + - ./nginx/init.conf:/etc/nginx/conf.d/default.conf + command: [nginx-debug, '-g', 'daemon off;'] + + certbot: + image: certbot/certbot + container_name: certbot + volumes: + - certbot-etc:/etc/letsencrypt + - certbot-www:/var/www/certbot + command: certonly --webroot -w /var/www/certbot --email minjing.chen.21@ucl.ac.uk --agree-tos --no-eff-email -d team3docker.uksouth.cloudapp.azure.com + +volumes: + certbot-etc: + certbot-www: diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..63cbbbe --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,59 @@ +services: + nextapp: + build: + context: . + dockerfile: Dockerfile + ports: + - '3000:3000' + container_name: nextapp + restart: always + env_file: + - .env.production + environment: + - NODE_ENV=production + depends_on: + - postgres + command: sh -c "cd src && npx prisma migrate deploy && npx prisma db seed && cd .. && npm start" + networks: + - app-network + volumes: + - ./src:/app/src + + postgres: + image: postgres:15 + container_name: postgres + restart: always + ports: + - '5432:5432' + env_file: + - .env.production + volumes: + - pgdata:/var/lib/postgresql/data + networks: + - app-network + + nginx: + build: ./nginx + container_name: nginx-main + ports: + - '80:80' + - '443:443' + volumes: + - certbot-etc:/etc/letsencrypt + - certbot-www:/var/www/certbot + environment: + DOMAIN_NAME: team3docker.uksouth.cloudapp.azure.com + depends_on: + - nextapp + networks: + - app-network + +networks: + app-network: + +volumes: + pgdata: + certbot-etc: + name: comp0067_2025_team3_certbot-etc + certbot-www: + name: comp0067_2025_team3_certbot-www diff --git a/nginx/Dockerfile b/nginx/Dockerfile new file mode 100644 index 0000000..c61cc6f --- /dev/null +++ b/nginx/Dockerfile @@ -0,0 +1,5 @@ +FROM nginx:alpine + +COPY default.conf /etc/nginx/conf.d/default.conf + +CMD ["nginx", "-g", "daemon off;"] diff --git a/nginx/default.conf b/nginx/default.conf new file mode 100644 index 0000000..aa72669 --- /dev/null +++ b/nginx/default.conf @@ -0,0 +1,32 @@ +server { + listen 80; + server_name team3docker.uksouth.cloudapp.azure.com; + + location /.well-known/acme-challenge/ { + root /var/www/certbot; + } + + location / { + return 301 https://$host$request_uri; # Corrected $request_url to $request_uri + } +} + +server { + listen 443 ssl; + server_name team3docker.uksouth.cloudapp.azure.com; + + ssl_certificate /etc/letsencrypt/live/team3docker.uksouth.cloudapp.azure.com/fullchain.pem; + ssl_certificate_key /etc/letsencrypt/live/team3docker.uksouth.cloudapp.azure.com/privkey.pem; + + location / { + proxy_pass http://nextapp:3000; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection 'upgrade'; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_cache_bypass $http_upgrade; + } +} diff --git a/nginx/init.conf b/nginx/init.conf new file mode 100644 index 0000000..21f0d42 --- /dev/null +++ b/nginx/init.conf @@ -0,0 +1,13 @@ +server { + listen 80; + server_name team3docker.uksouth.cloudapp.azure.com; + + location /.well-known/acme-challenge/ { + root /var/www/certbot; + } + + location / { + return 200 'Init server is running!'; + add_header Content-Type text/plain; + } +} diff --git a/src/prisma/seed.ts b/src/prisma/seed.ts index 09a95b1..88ede67 100644 --- a/src/prisma/seed.ts +++ b/src/prisma/seed.ts @@ -312,7 +312,10 @@ async function initialiseQuestions() { async function main() { await initialiseQuestions() - await initialiseUsersAndResponses() + + if (process.env.NODE_ENV === 'development') { + await initialiseUsersAndResponses() + } } main()