-
Notifications
You must be signed in to change notification settings - Fork 4
/
Dockerfile
76 lines (67 loc) · 2.72 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
65
66
67
68
69
70
71
72
73
74
75
76
# syntax=docker/dockerfile:1
# See https://www.digitalocean.com/community/tutorials/how-to-build-a-node-js-application-with-docker
# See https://depot.dev/docs/languages/node-pnpm-dockerfile
# See https://github.com/nodejs/docker-node/blob/main/docs/BestPractices.md
#===============================================================================
# Development
#===============================================================================
# Set up the dev environment
FROM node:20 AS dev-setup
WORKDIR /app
RUN corepack enable && yarn set version stable
COPY . .
RUN yarn install
# Install nano as text editor to be used with Git
RUN --mount=type=cache,target=/var/cache/apt apt-get update && apt-get install nano
# Install browsers and tools for running end-to-end test
RUN --mount=type=cache,target=/var/cache/apt yarn playwright install --with-deps
RUN yarn gulp installDev
EXPOSE 3000
#===============================================================================
# Production
#===============================================================================
# Use Alpine, a lighter weight OS
FROM node:20-alpine AS base-prod
ENV PROD_NODE_DIR=/home/node
ENV PROD_APP_DIR=$PROD_NODE_DIR/app
# Install dependencies
FROM base-prod AS deps-prod
# Install Yarn
RUN corepack enable && yarn set version stable
# Create directory for app and switch to it
WORKDIR $PROD_APP_DIR
# Copying these files before running `yarn install` allows us to take advantage
# of Docker’s caching mechanism.
COPY package.json yarn.lock .yarnrc.yml ./
# Install production dependencies while using BuildKit the create cache.
# BuildKit is included in Docker 23.0+
# See https://stackoverflow.com/a/66165135
# See https://docs.docker.com/build/buildkit
RUN --mount=type=cache,target=$PROD_NODE_DIR/.yarn YARN_CACHE_FOLDER=$PROD_NODE_DIR/.yarn yarn workspaces focus --all --production
# Build the standalone
FROM base-prod AS build-prod
# Install Yarn
RUN corepack enable && yarn set version stable
# Create directory for app and switch to it
WORKDIR $PROD_APP_DIR
# Copy installed dependencies from "deps-prod" stage
COPY --from=deps-prod $PROD_APP_DIR/node_modules ./node_modules
# Copy source files
COPY . .
# Build
RUN yarn build:standalone
# NOTE: The last stage is the stage used as the default
# Run standalone output
FROM base-prod AS prod
# Create directory with restricted file permissions
RUN mkdir -p $PROD_APP_DIR && chown -R node:node $PROD_APP_DIR
# Switch to new directory
WORKDIR $PROD_APP_DIR
# Switch user to ensure that all of the application files are owned by this
# non-root user
USER node
# Copy the built standalone app
COPY --from=build-prod --chown=node:node $PROD_APP_DIR/.next/standalone .
# Start server at localhost:3000
EXPOSE 3000
CMD ["node", "server.js"]