reduce Docker image size and use alpine as base

This commit is contained in:
Fishcake 2023-12-22 21:42:06 +09:00
parent 07008012de
commit 1652419cd3
No known key found for this signature in database
GPG Key ID: 70C6001B0A49776E

View File

@ -1,24 +1,42 @@
FROM node:19-buster-slim as build
FROM node:20-alpine AS build
WORKDIR /app
RUN apt-get update -y && apt-get install -y openssl python3 build-essential make gcc
# Install dependencies
RUN apk update && \
apk add --no-cache openssl python3 make g++ \
&& ln -sf python3 /usr/bin/python
# Copy package files and install dependencies
COPY package.json package-lock.json ./
RUN npm i
RUN apt-get purge -y gcc make build-essential && apt-get autoremove -y
RUN npm install
# Copy application files
COPY src/ src/
COPY scripts scripts/
COPY scripts/ scripts/
COPY prisma/schema.prisma prisma/
COPY tsconfig.json ./
# Generate prisma client and build the application
RUN npx prisma generate
RUN npm run build
RUN npm i
# Runtime stage
FROM node:20-alpine
WORKDIR /app
RUN apk update && \
apk add --no-cache openssl
# Copy built files from the build stage
COPY --from=build /app .
# Install only runtime dependencies
RUN npm install --only=production
# Copy and run migrations
COPY --from=build /app/prisma ./prisma
RUN npx prisma migrate deploy
# Set entrypoint
ENTRYPOINT [ "node", "scripts/start.js" ]
CMD ["start"]