Skip to content

Commit

Permalink
fix: update service Dockerfile and docker build command
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcossIC committed Nov 23, 2024
1 parent beb915a commit 636c065
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 140 deletions.
26 changes: 13 additions & 13 deletions .github/workflows/mainServer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,38 +83,38 @@ jobs:

# 8. Construir la imagen Docker para Courses Service
- name: Courses Service - Build Docker image
working-directory: ./server/apps/courses
run: docker build --build-arg NODE_ENV=${{ env.NODE_ENV }} --build-arg COURSES_SERVICE_HOST=${{ env.MICROSERVICE_HOST }} --build-arg COURSES_SERVICE_PORT=${{ env.COURSES_SERVICE_PORT }} --build-arg MONGO_URI=${{ secrets.MONGO_URI }} --build-arg JWT_SECRET=${{ secrets.JWT_SECRET }} -t $GCLOUD_REGION-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.COURSE_SERVICE_NAME }}:${{ github.sha }} .
working-directory: ./server
run: docker build --progress plain --build-arg MONGO_URI=${{ secrets.MONGO_URI }} --build-arg JWT_SECRET=${{ secrets.JWT_SECRET }} -f ./apps/courses/Dockerfile -t ${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.COURSE_SERVICE_NAME }}:${{ github.sha }} ./apps/courses

# 8. Construir la imagen Docker para Gateway Service
- name: Gateway Service - Build Docker image
working-directory: ./server/apps/gateway
run: docker build --build-arg NODE_ENV=${{ env.NODE_ENV }} --build-arg PORT=${{ env.GATEWAY_SERVICE_PORT }} --build-arg USERS_MICROSERVICE_HOST=${{ env.MICROSERVICE_HOST }} --build-arg USERS_SERVICE_PORT=${{ env.USERS_SERVICE_PORT }} --build-arg COURSES_SERVICE_HOST=${{ env.MICROSERVICE_HOST }} --build-arg COURSES_SERVICE_PORT=${{ env.COURSES_SERVICE_PORT }} --build-arg UPLOAD_MICROSERVICE_HOST=${{ env.MICROSERVICE_HOST }} --build-arg UPLOAD_SERVICE_PORT=${{ env.UPLOAD_SERVICE_PORT }} --build-arg FRONTEND_URL=${{ secrets.FRONTEND_URL }} --build-arg JWT_SECRET=${{ secrets.JWT_SECRET }} -t $GCLOUD_REGION-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.GATEWAY_SERVICE_NAME }}:${{ github.sha }} .
working-directory: ./server
run: docker build --progress plain --build-arg FRONTEND_URL=${{ secrets.FRONTEND_URL }} --build-arg JWT_SECRET=${{ secrets.JWT_SECRET }} -f ./apps/gateway/Dockerfile -t ${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.GATEWAY_SERVICE_NAME }}:${{ github.sha }} ./apps/gateway

# 8. Construir la imagen Docker para Upload Service
- name: Upload Service - Build Docker image
working-directory: ./server/apps/uploadGCould
run: docker build --build-arg NODE_ENV=${{ env.NODE_ENV }} --build-arg UPLOAD_MICROSERVICE_HOST=${{ env.MICROSERVICE_HOST }} --build-arg UPLOAD_SERVICE_PORT=${{ env.UPLOAD_SERVICE_PORT }} --build-arg PROJECT_ID=${{ secrets.PROJECT_ID }} --build-arg BUCKET_NAME=${{ secrets.BUCKET_NAME }} -t $GCLOUD_REGION-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.UPLOAD_SERVICE_NAME }}:${{ github.sha }} .
working-directory: ./server
run: docker build --progress plain --build-arg PROJECT_ID=${{ secrets.PROJECT_ID }} --build-arg BUCKET_NAME=${{ secrets.BUCKET_NAME }} -f ./apps/uploadGCould/Dockerfile -t ${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.UPLOAD_SERVICE_NAME }}:${{ github.sha }} ./apps/uploadGCould

# 9. Subir la imagen de User Service a Artifact Registry
- name: Users Service - Push Docker image
working-directory: ./server/apps/users
run: docker push $GCLOUD_REGION-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.USERS_SERVICE_NAME }}:${{ github.sha }}
working-directory: ./server
run: docker push ${{ secrets.GCLOUD_REGION }}-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.USERS_SERVICE_NAME }}:${{ github.sha }}

# 10. Subir la imagen de Courses Service a Artifact Registry
- name: Courses Service - Push Docker image
working-directory: ./server/apps/courses
working-directory: ./server
run: docker push $GCLOUD_REGION-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.COURSE_SERVICE_NAME }}:${{ github.sha }}

# 10. Subir la imagen de Upload Service a Artifact Registry
- name: Upload Service - Push Docker image
working-directory: ./server/apps/uploadGCould
run: docker push $GCLOUD_REGION-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.UPLOAD_SERVICE_NAME }}:${{ github.sha }}
working-directory: ./server
run: docker push ${{ secrets.GCLOUD_REGION }}.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.UPLOAD_SERVICE_NAME }}:${{ github.sha }}

# 11. Subir la imagen de Gateway Service a Artifact Registry
- name: Gateway Service - Push Docker image
working-directory: ./server/apps/gateway
run: docker push $GCLOUD_REGION-docker.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.GATEWAY_SERVICE_NAME }}:${{ github.sha }}
working-directory: ./server
run: docker push ${{ secrets.GCLOUD_REGION }}.pkg.dev/${{ secrets.GCLOUD_PROJECT_ID }}/klowhub-server/${{ env.GATEWAY_SERVICE_NAME }}:${{ github.sha }}

# 12. Setear un .env para la susticucion de variables
- name: Create env file for substitution
Expand Down
51 changes: 21 additions & 30 deletions server/apps/courses/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
# Fase Base
FROM node:20-alpine AS base
# Fase de dependencias
FROM node:20-alpine AS deps
RUN apk add --no-cache g++ make py3-pip libc6-compat
WORKDIR /app
# Instalar pnpm globalmente
RUN npm install -g typescript @nestjs/cli

COPY package.json package-lock.json tsconfig.json ./
RUN npm ci --only=production

# Fase de Construcción
FROM base AS builder
FROM deps AS builder
WORKDIR /app
COPY package.json package-lock.json tsconfig.json ./
RUN npm ci --legacy-peer-deps
COPY . ./
RUN npm ci

# Declaración de los ARGs (se pasarán desde GitHub Actions)
ARG NODE_ENV=production
ARG COURSES_SERVICE_HOST=0.0.0.0
ARG COURSES_SERVICE_PORT=3001
ARG JWT_SECRET
ARG MONGO_URI

# Variables de entorno
ENV NODE_ENV=$NODE_ENV
ENV COURSES_SERVICE_HOST=$COURSES_SERVICE_HOST
ENV COURSES_SERVICE_PORT=$COURSES_SERVICE_PORT
ENV JWT_SECRET=$JWT_SECRET
ENV MONGO_URI=$MONGO_URI
RUN npm run build
RUN npm prune --production

# Construir el proyecto con pnpm
RUN npm run build

# Fase de Producción
FROM base AS production
# Fase de producción
FROM node:20-alpine AS production
RUN apk add --no-cache libc6-compat
WORKDIR /app

# Configuración de usuario no-root
RUN addgroup -g 1001 -S nodejs && \
adduser -S nestjs -u 1001 && \
chown -R nestjs:nodejs /app
# Declaración de los ARGs (se pasarán desde GitHub Actions)
ARG NODE_ENV=production
ARG COURSES_SERVICE_HOST=0.0.0.0
Expand All @@ -47,14 +39,13 @@ ENV COURSES_SERVICE_PORT=$COURSES_SERVICE_PORT
ENV JWT_SECRET=$JWT_SECRET
ENV MONGO_URI=$MONGO_URI

# Configurar usuario seguro para correr la aplicación
RUN addgroup -g 1001 -S nodejs && adduser -S nestjs -u 1001
USER nestjs

# Copiar archivos relevantes de la fase de construcción
COPY --from=builder /app/package.json /app/package-lock.json /app/tsconfig.json ./
COPY --from=deps /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
COPY package.json ./

USER nestjs

EXPOSE $PORT
EXPOSE 3000

CMD ["npm", "run", "start:prod"]
62 changes: 22 additions & 40 deletions server/apps/gateway/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,48 +1,31 @@
# Fase Base
FROM node:20-alpine AS base
# Fase de dependencias
FROM node:20-alpine AS deps
RUN apk add --no-cache g++ make py3-pip libc6-compat
WORKDIR /app
# Instalar pnpm globalmente
RUN npm install -g typescript @nestjs/cli

COPY package.json package-lock.json tsconfig.json ./
RUN npm ci --only=production

# Fase de Construcción
FROM base AS builder
FROM deps AS builder
WORKDIR /app
COPY package.json package-lock.json tsconfig.json ./
RUN npm ci --legacy-peer-deps
COPY . ./
RUN npm ci
RUN npm run build
RUN npm prune --production

# Declaración de los ARGs (se pasarán desde GitHub Actions)
ARG NODE_ENV=production
ARG PORT=3000
ARG USERS_MICROSERVICE_HOST=0.0.0.0
ARG USERS_SERVICE_PORT=3001
ARG COURSES_SERVICE_HOST=0.0.0.0
ARG COURSES_SERVICE_PORT=3002
ARG UPLOAD_MICROSERVICE_HOST=0.0.0.0
ARG UPLOAD_SERVICE_PORT=3003
ARG JWT_SECRET
ARG FRONTEND_URL

# Variables de entorno
ENV NODE_ENV=$NODE_ENV
ENV PORT=$PORT
ENV USERS_MICROSERVICE_HOST=$USERS_MICROSERVICE_HOST
ENV USERS_SERVICE_PORT=$USERS_SERVICE_PORT
ENV COURSES_SERVICE_HOST=$COURSES_SERVICE_HOST
ENV COURSES_SERVICE_PORT=$COURSES_SERVICE_PORT
ENV UPLOAD_MICROSERVICE_HOST=$UPLOAD_MICROSERVICE_HOST
ENV UPLOAD_SERVICE_PORT=$UPLOAD_SERVICE_PORT
ENV JWT_SECRET=$JWT_SECRET
ENV FRONTEND_URL=$FRONTEND_URL

# Construir el proyecto con pnpm
RUN npm run build

# Fase de Producción
FROM base AS production
# Fase de producción
FROM node:20-alpine AS production
RUN apk add --no-cache libc6-compat
WORKDIR /app

# Configuración de usuario no-root
RUN addgroup -g 1001 -S nodejs && \
adduser -S nestjs -u 1001 && \
chown -R nestjs:nodejs /app

# Declaración de los ARGs (se pasarán desde GitHub Actions)
ARG NODE_ENV=production
ARG PORT=3000
Expand All @@ -67,14 +50,13 @@ ENV UPLOAD_SERVICE_PORT=$UPLOAD_SERVICE_PORT
ENV JWT_SECRET=$JWT_SECRET
ENV FRONTEND_URL=$FRONTEND_URL

# Configurar usuario seguro para correr la aplicación
RUN addgroup -g 1001 -S nodejs && adduser -S nestjs -u 1001
USER nestjs

# Copiar archivos relevantes de la fase de construcción
COPY --from=builder /app/package.json /app/package-lock.json /app/tsconfig.json ./
COPY --from=deps /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
COPY package.json ./

USER nestjs

EXPOSE $PORT
EXPOSE 3000

CMD ["npm", "run", "start:prod"]
23 changes: 0 additions & 23 deletions server/apps/gateway/Dockerfile.dev

This file was deleted.

54 changes: 23 additions & 31 deletions server/apps/uploadGCould/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
# Fase Base
FROM node:20-alpine AS base
# Fase de dependencias
FROM node:20-alpine AS deps
RUN apk add --no-cache g++ make py3-pip libc6-compat
WORKDIR /app
# Instalar pnpm globalmente
RUN npm install -g typescript @nestjs/cli
COPY gccKey.json package.json package-lock.json tsconfig.json ./

COPY package.json package-lock.json tsconfig.json ./
RUN npm ci --only=production

# Fase de Construcción
FROM base AS builder
FROM deps AS builder
WORKDIR /app
COPY package.json package-lock.json tsconfig.json ./
RUN npm ci --legacy-peer-deps
COPY . ./
RUN npm ci

# Declaración de los ARGs (se pasarán desde GitHub Actions)
ARG NODE_ENV=production
ARG UPLOAD_MICROSERVICE_HOST=0.0.0.0
ARG UPLOAD_SERVICE_PORT=3003
ARG PROJECT_ID
ARG BUCKET_NAME

# Variables de entorno
ENV NODE_ENV=$NODE_ENV
ENV UPLOAD_MICROSERVICE_HOST=$UPLOAD_MICROSERVICE_HOST
ENV UPLOAD_SERVICE_PORT=$UPLOAD_SERVICE_PORT
ENV PROJECT_ID=$PROJECT_ID
ENV BUCKET_NAME=$BUCKET_NAME
RUN npm run build
RUN npm prune --production

# Construir el proyecto con pnpm
RUN npm run build

# Fase de Producción
FROM base AS production
# Fase de producción
FROM node:20-alpine AS production
RUN apk add --no-cache libc6-compat
WORKDIR /app

# Configuración de usuario no-root
RUN addgroup -g 1001 -S nodejs && \
adduser -S nestjs -u 1001 && \
chown -R nestjs:nodejs /app

# Declaración de los ARGs (se pasarán desde GitHub Actions)
ARG NODE_ENV=production
ARG UPLOAD_MICROSERVICE_HOST=0.0.0.0
Expand All @@ -47,14 +40,13 @@ ENV UPLOAD_SERVICE_PORT=$UPLOAD_SERVICE_PORT
ENV PROJECT_ID=$PROJECT_ID
ENV BUCKET_NAME=$BUCKET_NAME

# Configurar usuario seguro para correr la aplicación
RUN addgroup -g 1001 -S nodejs && adduser -S nestjs -u 1001
USER nestjs

# Copiar archivos relevantes de la fase de construcción
COPY --from=builder /app/gccKey.json /app/package.json /app/package-lock.json /app/tsconfig.json ./
COPY --from=deps /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
COPY package.json ./

USER nestjs

EXPOSE $PORT
EXPOSE 3000

CMD ["npm", "run", "start:prod"]
3 changes: 0 additions & 3 deletions server/apps/users/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,4 @@ USER nestjs

EXPOSE 3001

HEALTHCHECK --interval=30s --timeout=3s --start-period=30s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:$PORT/health || exit 1

CMD ["npm", "run", "start:prod"]

0 comments on commit 636c065

Please sign in to comment.