generated from Louis3797/express-ts-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Dockerfile
64 lines (46 loc) · 1.47 KB
/
Dockerfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Build stage
FROM node:18-alpine3.16 AS build
# Set the working directory to /app
WORKDIR /app
# Copy package.json and yarn.lock to the container
COPY package.json yarn.lock ./
# Install development dependencies
RUN apk add --no-cache --virtual .build-deps \
gcc \
g++ \
python3 \
&& yarn install --frozen-lockfile --production=false \
&& apk del .build-deps \
&& yarn cache clean
# Copy the rest of the application code to the container
COPY . .
# Build the application
RUN yarn build
# Run stage
FROM node:18-alpine3.16 AS run
# Set the working directory to /app
WORKDIR /app
# Copy package.json and yarn.lock to the container
COPY package.json yarn.lock ./
# Install dumb-init
RUN apk add --no-cache dumb-init
# Add a non-root user to run the application
RUN addgroup -g 1001 -S nodejs \
&& adduser -S nodejs -u 1001 \
&& chown -R nodejs:nodejs /app
# Switch to the non-root user
USER nodejs
# Install production dependencies
RUN yarn install --frozen-lockfile --production=true && yarn cache clean
# Copy the built application code from the build stage
COPY --chown=nodejs:nodejs --from=build /app/dist ./dist
# Copy the prisma directory from the build stage
COPY --chown=nodejs:nodejs --from=build /app/prisma ./prisma
ENV NODE_ENV=production
ENV PORT=3000
# Generate prisma client
RUN yarn prisma generate
# Expose port for the application to listen on
EXPOSE 4040
# Start the application with dumb-init
CMD ["dumb-init", "node", "dist/index.js"]