From 3db4a0ec005f384c1e9b01486103351c36493234 Mon Sep 17 00:00:00 2001 From: minjnlgc Date: Thu, 10 Apr 2025 20:48:10 +0100 Subject: [PATCH 1/3] feat: local build version docker set-up --- .dockerignore | 6 ++++++ .gitignore | 1 + Dockerfile | 29 ++++++++++++++++++++++++++ docker-compose.yml | 51 ++++++++++++++++++++++++++++++++++++++++++++++ nginx/Dockerfile | 3 +++ nginx/default.conf | 16 +++++++++++++++ 6 files changed, 106 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 nginx/Dockerfile create mode 100644 nginx/default.conf 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.yml b/docker-compose.yml new file mode 100644 index 0000000..62a1c4c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,51 @@ +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: + image: nginx:latest + container_name: nginx + ports: + - '80:80' + volumes: + - ./nginx/default.conf:/etc/nginx/conf.d/default.conf + depends_on: + - nextapp + networks: + - app-network + +networks: + app-network: + +volumes: + pgdata: diff --git a/nginx/Dockerfile b/nginx/Dockerfile new file mode 100644 index 0000000..9e620af --- /dev/null +++ b/nginx/Dockerfile @@ -0,0 +1,3 @@ +FROM nginx:alpine + +COPY nginx.conf /etc/nginx/nginx.conf diff --git a/nginx/default.conf b/nginx/default.conf new file mode 100644 index 0000000..b4db104 --- /dev/null +++ b/nginx/default.conf @@ -0,0 +1,16 @@ +server { + listen 80; + server_name localhost; + + 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; + } +} From 0a053454faec4f12b5cb0bfaf75787153211bae3 Mon Sep 17 00:00:00 2001 From: minjnlgc Date: Mon, 14 Apr 2025 16:34:24 +0100 Subject: [PATCH 2/3] feat: update the deployed version, but not sure if it works --- docker-compose-init.yml | 23 +++++++++++++++++++++++ docker-compose.yml | 12 ++++++++++-- nginx/Dockerfile | 4 +++- nginx/default.conf | 18 +++++++++++++++++- src/prisma/seed.ts | 5 ++++- 5 files changed, 57 insertions(+), 5 deletions(-) create mode 100644 docker-compose-init.yml diff --git a/docker-compose-init.yml b/docker-compose-init.yml new file mode 100644 index 0000000..4e1481c --- /dev/null +++ b/docker-compose-init.yml @@ -0,0 +1,23 @@ +version: '3' + +services: + nginx: + image: nginx:alpine + container_name: nginx-init + ports: + - '80:80' + volumes: + - certbot-www:/var/www/certbot + 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 index 62a1c4c..9fc671f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -33,12 +33,16 @@ services: - app-network nginx: - image: nginx:latest + build: ./nginx container_name: nginx ports: - '80:80' + - '443:443' volumes: - - ./nginx/default.conf:/etc/nginx/conf.d/default.conf + - certbot-etc:/etc/letsencrypt + - certbot-www:/var/www/certbot + environment: + DOMAIN_NAME: team3docker.uksouth.cloudapp.azure.com depends_on: - nextapp networks: @@ -49,3 +53,7 @@ networks: volumes: pgdata: + certbot-etc: + external: true + certbot-www: + external: true diff --git a/nginx/Dockerfile b/nginx/Dockerfile index 9e620af..c61cc6f 100644 --- a/nginx/Dockerfile +++ b/nginx/Dockerfile @@ -1,3 +1,5 @@ FROM nginx:alpine -COPY nginx.conf /etc/nginx/nginx.conf +COPY default.conf /etc/nginx/conf.d/default.conf + +CMD ["nginx", "-g", "daemon off;"] diff --git a/nginx/default.conf b/nginx/default.conf index b4db104..aa72669 100644 --- a/nginx/default.conf +++ b/nginx/default.conf @@ -1,6 +1,22 @@ server { listen 80; - server_name localhost; + 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; 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() From f235b3ea5b264af5c850c84825c8b1beab0a48bd Mon Sep 17 00:00:00 2001 From: minjnlgc Date: Mon, 14 Apr 2025 17:30:14 +0000 Subject: [PATCH 3/3] feat: the working deployed docker setup --- docker-compose-init.yml | 1 + docker-compose.yml | 6 +++--- nginx/init.conf | 13 +++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 nginx/init.conf diff --git a/docker-compose-init.yml b/docker-compose-init.yml index 4e1481c..a8ef625 100644 --- a/docker-compose-init.yml +++ b/docker-compose-init.yml @@ -8,6 +8,7 @@ services: - '80:80' volumes: - certbot-www:/var/www/certbot + - ./nginx/init.conf:/etc/nginx/conf.d/default.conf command: [nginx-debug, '-g', 'daemon off;'] certbot: diff --git a/docker-compose.yml b/docker-compose.yml index 9fc671f..63cbbbe 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -34,7 +34,7 @@ services: nginx: build: ./nginx - container_name: nginx + container_name: nginx-main ports: - '80:80' - '443:443' @@ -54,6 +54,6 @@ networks: volumes: pgdata: certbot-etc: - external: true + name: comp0067_2025_team3_certbot-etc certbot-www: - external: true + name: comp0067_2025_team3_certbot-www 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; + } +}