1
0
mirror of https://github.com/stonith404/pingvin-share.git synced 2024-07-02 07:20:38 +02:00

Improved docker image (size & speed)

This commit is contained in:
Jean-Michel Carrel 2022-12-10 15:34:01 +01:00
parent b1bfb09dfd
commit 8e38c5fed7
3 changed files with 33 additions and 19 deletions

View File

@ -1,27 +1,42 @@
FROM node:18-slim AS frontend-builder # Stage 1: on frontend dependency change
FROM node:18-alpine AS frontend-dependencies
WORKDIR /opt/app WORKDIR /opt/app
COPY frontend/package.json frontend/package-lock.json ./ COPY frontend/package.json frontend/package-lock.json ./
RUN npm ci RUN npm ci
# Stage 2: on frontend change
FROM node:18-alpine AS frontend-builder
WORKDIR /opt/app
COPY ./frontend . COPY ./frontend .
COPY --from=frontend-dependencies /opt/app/node_modules ./node_modules
RUN npm run build RUN npm run build
FROM node:18-slim AS backend-builder # Stage 3: on backend dependency change
RUN apt-get update && apt-get install -y openssl FROM node:18-alpine AS backend-dependencies
WORKDIR /opt/app WORKDIR /opt/app
COPY backend/package.json backend/package-lock.json ./ COPY backend/package.json backend/package-lock.json ./
RUN npm ci RUN npm ci
COPY ./backend .
RUN npx prisma generate
RUN npm run build
FROM node:18-slim AS runner # Stage 4:on backend change
FROM node:18-alpine AS backend-builder
RUN apk add --update openssl
WORKDIR /opt/app
COPY ./backend .
COPY --from=backend-dependencies /opt/app/node_modules ./node_modules
RUN npx prisma generate
RUN npm run build && npm prune --production
# Stage 5: Final image
FROM node:18-alpine AS runner
ENV NODE_ENV=production ENV NODE_ENV=production
RUN apt-get update && apt-get install -y openssl RUN apk add --update openssl
WORKDIR /opt/app/frontend WORKDIR /opt/app/frontend
COPY --from=frontend-builder /opt/app/next.config.js .
COPY --from=frontend-builder /opt/app/public ./public COPY --from=frontend-builder /opt/app/public ./public
COPY --from=frontend-builder /opt/app/.next ./.next # Automatically leverage output traces to reduce image size
COPY --from=frontend-builder /opt/app/node_modules ./node_modules # https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=frontend-builder /opt/app/.next/standalone ./
COPY --from=frontend-builder /opt/app/.next/static ./.next/static
WORKDIR /opt/app/backend WORKDIR /opt/app/backend
COPY --from=backend-builder /opt/app/node_modules ./node_modules COPY --from=backend-builder /opt/app/node_modules ./node_modules
@ -31,4 +46,5 @@ COPY --from=backend-builder /opt/app/package.json ./
WORKDIR /opt/app WORKDIR /opt/app
EXPOSE 3000 EXPOSE 3000
CMD cd frontend && node_modules/.bin/next start & cd backend && npm run prod EXPOSE 8080
CMD node frontend/server.js & cd backend && npm run prod

View File

@ -22,6 +22,7 @@
"@nestjs/platform-express": "^9.2.1", "@nestjs/platform-express": "^9.2.1",
"@nestjs/schedule": "^2.1.0", "@nestjs/schedule": "^2.1.0",
"@nestjs/throttler": "^3.1.0", "@nestjs/throttler": "^3.1.0",
"@prisma/client": "^4.7.1",
"archiver": "^5.3.1", "archiver": "^5.3.1",
"argon2": "^0.30.2", "argon2": "^0.30.2",
"class-transformer": "^0.5.1", "class-transformer": "^0.5.1",
@ -36,13 +37,13 @@
"passport-local": "^1.0.0", "passport-local": "^1.0.0",
"reflect-metadata": "^0.1.13", "reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"rxjs": "^7.6.0" "rxjs": "^7.6.0",
"ts-node": "^10.9.1"
}, },
"devDependencies": { "devDependencies": {
"@nestjs/cli": "^9.1.5", "@nestjs/cli": "^9.1.5",
"@nestjs/schematics": "^9.0.3", "@nestjs/schematics": "^9.0.3",
"@nestjs/testing": "^9.2.1", "@nestjs/testing": "^9.2.1",
"@prisma/client": "^4.7.1",
"@types/archiver": "^5.3.1", "@types/archiver": "^5.3.1",
"@types/cron": "^2.0.0", "@types/cron": "^2.0.0",
"@types/express": "^4.17.14", "@types/express": "^4.17.14",
@ -63,7 +64,6 @@
"prisma": "^4.7.1", "prisma": "^4.7.1",
"source-map-support": "^0.5.21", "source-map-support": "^0.5.21",
"ts-loader": "^9.4.2", "ts-loader": "^9.4.2",
"ts-node": "^10.9.1",
"tsconfig-paths": "4.1.1", "tsconfig-paths": "4.1.1",
"typescript": "^4.9.3", "typescript": "^4.9.3",
"wait-on": "^6.0.1" "wait-on": "^6.0.1"

View File

@ -1,10 +1,8 @@
/** @type {import('next').NextConfig} */ /** @type {import('next').NextConfig} */
const withPWA = require("next-pwa")({ const withPWA = require("next-pwa")({
dest: "public", dest: "public",
disable: process.env.NODE_ENV == "development" disable: process.env.NODE_ENV == "development",
}); });
module.exports = withPWA({ output: "standalone" });
module.exports = withPWA();