Commit f918b6c3 by Sathish A

Initial setup of Docker configuration for Spin The Wheel application, including…

Initial setup of Docker configuration for Spin The Wheel application, including Dockerfiles for backend and frontend, docker-compose file, and CI/CD pipeline configuration in .gitlab-ci.yml. Added .dockerignore files for backend and frontend to exclude unnecessary files from Docker builds.
parent 34f956fc
Pipeline #61314 failed with stages
in 46 seconds
# Dependencies
node_modules
npm-debug.log
yarn-error.log
# Build outputs
dist
build
*.tsbuildinfo
# Environment files
.env
.env.local
.env.*.local
# IDE
.vscode
.idea
*.swp
*.swo
*~
# OS
.DS_Store
Thumbs.db
# Git
.git
.gitignore
.gitlab-ci.yml
# Documentation
README.md
*.md
# Test files
coverage
.nyc_output
*.test.ts
*.test.tsx
*.spec.ts
*.spec.tsx
# Logs
logs
*.log
# Misc
.cache
.temp
stages:
- build
- deploy
variables:
app_name: spin-the-wheel
app_image_tag: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME
image_tag: $CI_BUILD_REF_NAME
image: $CI_REGISTRY_IMAGE
registry_pass: $CI_BUILD_TOKEN
registry_user: gitlab-ci-token
registry: $CI_REGISTRY
# Docker build for backend - staging
docker_build_backend_staging:
tags:
- docker
stage: build
variables:
app_env: staging
script:
- env
- docker login -u $registry_user -p $registry_pass $registry
- cd backend
- docker build -t $CI_REGISTRY_IMAGE/backend:$CI_COMMIT_REF_NAME -t $CI_REGISTRY_IMAGE/backend:staging -f Dockerfile .
- docker push $CI_REGISTRY_IMAGE/backend:$CI_COMMIT_REF_NAME
- docker push $CI_REGISTRY_IMAGE/backend:staging
only:
- master
# Docker build for frontend - staging
docker_build_frontend_staging:
tags:
- docker
stage: build
variables:
app_env: staging
vite_app_url: https://spin-wheel-backend-staging.kacdn.net
backend_url: https://spin-wheel-backend-staging.kacdn.net
script:
- env
- docker login -u $registry_user -p $registry_pass $registry
- cd frontend
- docker build -t $CI_REGISTRY_IMAGE/frontend:$CI_COMMIT_REF_NAME -t $CI_REGISTRY_IMAGE/frontend:staging
--build-arg VITE_APP_URL=$vite_app_url
--build-arg APP_ENV=$app_env
--build-arg BACKEND_URL=$backend_url
-f Dockerfile .
- docker push $CI_REGISTRY_IMAGE/frontend:$CI_COMMIT_REF_NAME
- docker push $CI_REGISTRY_IMAGE/frontend:staging
only:
- master
# Deploy backend to staging
deploy_backend_staging:
image: registry.git.int.krds.com/tools/deploy:edge
tags:
- deploy
stage: deploy
variables:
app_env: staging
app_url: https://spin-wheel-backend-staging.kacdn.net
script:
- deploy-ecs staging
only:
- master
# Deploy frontend to staging
deploy_frontend_staging:
image: registry.git.int.krds.com/tools/deploy:edge
tags:
- deploy
stage: deploy
variables:
app_env: staging
app_url: https://spin-wheel-frontend-staging.kacdn.net
script:
- deploy-ecs staging
only:
- master
# Multi-stage Dockerfile for both frontend and backend
# This is useful for docker-compose or single container deployment
FROM node:18-alpine AS base
# Backend stage
FROM base AS backend-deps
WORKDIR /app/backend
COPY backend/package*.json ./
RUN npm ci
FROM backend-deps AS backend-prisma
COPY backend/prisma ./prisma
RUN npx prisma generate
FROM base AS backend-builder
WORKDIR /app/backend
COPY --from=backend-deps /app/backend/node_modules ./node_modules
COPY --from=backend-prisma /app/backend/node_modules/.prisma ./node_modules/.prisma
COPY backend/tsconfig.json ./
COPY backend/src ./src
COPY backend/prisma ./prisma
RUN npm run build
# Frontend stage
FROM base AS frontend-deps
WORKDIR /app/frontend
COPY frontend/package*.json ./
RUN npm ci
FROM base AS frontend-builder
WORKDIR /app/frontend
COPY --from=frontend-deps /app/frontend/node_modules ./node_modules
COPY frontend/ ./
ARG VITE_APP_URL
ENV VITE_APP_URL=${VITE_APP_URL}
RUN npm run build
# Production runner
FROM base AS runner
WORKDIR /app
# Install nginx for frontend
RUN apk add --no-cache nginx
# Copy backend
COPY --from=backend-builder /app/backend/dist ./backend/dist
COPY --from=backend-builder /app/backend/node_modules ./backend/node_modules
COPY --from=backend-builder /app/backend/package*.json ./backend/
COPY --from=backend-builder /app/backend/prisma ./backend/prisma
# Copy frontend
COPY --from=frontend-builder /app/frontend/dist /usr/share/nginx/html
# Create nginx config
RUN echo 'server { \
listen 80; \
server_name _; \
root /usr/share/nginx/html; \
index index.html; \
\
location / { \
try_files $uri $uri/ /index.html; \
} \
\
location /api { \
proxy_pass http://localhost:5000; \
proxy_http_version 1.1; \
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; \
} \
}' > /etc/nginx/conf.d/default.conf
# Create startup script
RUN echo '#!/bin/sh\n\
nginx &\n\
cd /app/backend\n\
npx prisma migrate deploy\n\
node dist/index.js\n\
' > /app/start.sh && chmod +x /app/start.sh
EXPOSE 80 5000
CMD ["/app/start.sh"]
node_modules
npm-debug.log
dist
*.tsbuildinfo
.env
.env.local
.env.*.local
.vscode
.idea
*.swp
*.swo
.DS_Store
coverage
.nyc_output
*.log
.cache
.temp
# Backend Dockerfile
FROM node:18-alpine AS base
# Install dependencies only when needed
FROM base AS deps
WORKDIR /app/backend
# Copy package files
COPY package*.json ./
RUN npm ci
# Generate Prisma Client
FROM deps AS prisma
COPY prisma ./prisma
RUN npx prisma generate
# Build the application
FROM base AS builder
WORKDIR /app/backend
COPY --from=deps /app/backend/node_modules ./node_modules
COPY --from=prisma /app/backend/node_modules/.prisma ./node_modules/.prisma
COPY tsconfig.json ./
COPY src ./src
COPY prisma ./prisma
RUN npm run build
# Production image
FROM base AS runner
WORKDIR /app/backend
ENV NODE_ENV=production
# Create non-root user
RUN addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nodejs
# Copy necessary files
COPY --from=builder /app/backend/dist ./dist
COPY --from=builder /app/backend/node_modules ./node_modules
COPY --from=builder /app/backend/package*.json ./
COPY --from=builder /app/backend/prisma ./prisma
# Change ownership
RUN chown -R nodejs:nodejs /app/backend
USER nodejs
EXPOSE 5000
ENV PORT=5000
# Run migrations and start server
CMD ["sh", "-c", "npx prisma migrate deploy && node dist/index.js"]
version: '3.8'
services:
backend:
build:
context: ./backend
dockerfile: Dockerfile
container_name: spin-wheel-backend
ports:
- "5000:5000"
environment:
- NODE_ENV=production
- PORT=5000
- DATABASE_URL=${DATABASE_URL}
- ALLOWED_ORIGINS=${ALLOWED_ORIGINS:-*}
restart: unless-stopped
networks:
- spin-wheel-network
healthcheck:
test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost:5000/api/health"]
interval: 30s
timeout: 10s
retries: 3
start_period: 40s
frontend:
build:
context: ./frontend
dockerfile: Dockerfile
args:
- VITE_APP_URL=${VITE_APP_URL:-http://localhost:5000}
- BACKEND_URL=http://backend:5000
container_name: spin-wheel-frontend
ports:
- "80:80"
depends_on:
- backend
restart: unless-stopped
networks:
- spin-wheel-network
healthcheck:
test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost:80"]
interval: 30s
timeout: 10s
retries: 3
networks:
spin-wheel-network:
driver: bridge
node_modules
npm-debug.log
dist
build
.env
.env.local
.env.*.local
.vscode
.idea
*.swp
*.swo
.DS_Store
coverage
.nyc_output
*.log
.cache
.temp
# Stage 1: Base image for building the app
FROM node:lts-alpine AS base
ARG VITE_APP_URL
ARG APP_ENV
ARG BACKEND_URL
# Stage 2: Install dependencies only when needed
FROM base AS deps
WORKDIR /app
COPY package*.json ./
RUN npm ci
# Stage 3: Build the app
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Build arguments for environment variables
ARG VITE_APP_URL
ARG APP_ENV
ENV VITE_APP_URL=${VITE_APP_URL}
RUN npm run build
# Stage 4: Production image with nginx
FROM nginx:alpine AS runner
WORKDIR /app
# Copy built files
COPY --from=builder /app/dist /usr/share/nginx/html
# Create nginx configuration
ARG BACKEND_URL=http://backend:5000
RUN echo "server { \
listen 80; \
server_name _; \
root /usr/share/nginx/html; \
index index.html; \
\
location / { \
try_files \$uri \$uri/ /index.html; \
} \
\
location /api { \
proxy_pass ${BACKEND_URL}; \
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; \
} \
}" > /etc/nginx/conf.d/default.conf
EXPOSE 80
ENV PORT 80
CMD ["nginx", "-g", "daemon off;"]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment