-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathDockerfile
147 lines (113 loc) · 4.14 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
## Stage 0 (builder)
# This stage installs all dependencies and builds the application if needed
FROM node:16.13.0-alpine AS builder
ARG PANDOC_VERSION=3.1.1
# Set the working directory in the Docker container
WORKDIR /usr/src/app
# Install build dependencies for npm packages
RUN apk add --no-cache --virtual .build-deps \
python3 \
make \
g++ \
autoconf \
automake \
libtool \
nasm \
libpng-dev \
git \
curl \
tar
# Install Pandoc
ARG TARGETPLATFORM
RUN ARCH=$(echo ${TARGETPLATFORM} | sed -nE 's/^linux\/(amd64|arm64)$/\1/p') \
&& if [ -z "$ARCH" ]; then echo "Unsupported architecture: $TARGETPLATFORM" && exit 1; fi \
&& curl -L https://github.com/jgm/pandoc/releases/download/${PANDOC_VERSION}/pandoc-${PANDOC_VERSION}-linux-${ARCH}.tar.gz | tar xvz \
&& mv pandoc-${PANDOC_VERSION}/bin/pandoc /usr/local/bin/pandoc \
&& chmod +x /usr/local/bin/pandoc \
&& rm -r pandoc-${PANDOC_VERSION}
# Set environment variables
ENV NODE_ENV=production
# Copy package files
COPY package.json .
COPY package-lock.json .
# Install all dependencies including devDependencies
RUN npm config list \
&& npm ci \
&& npm cache clean --force
# Remove build dependencies
RUN apk del .build-deps
## Stage 1 (production base)
# This stage prepares the production environment
FROM node:16.13.0-alpine AS base
EXPOSE 8080
# Set environment variables
ENV NODE_ENV=production
ENV NODE_PATH=/usr/src/app/app
# Set the working directory in the Docker container
WORKDIR /usr/src/app
# Copy the built application from the builder stage
COPY --from=builder /usr/src/app .
COPY --from=builder /usr/local/bin/pandoc /usr/local/bin/pandoc
# Install git for good since the git client requires it
RUN apk add --no-cache git
# Install curl for good since the health check requires it
RUN apk add --no-cache curl
# configure git
RUN git config --global user.email "[email protected]"
RUN git config --global user.name "Your Name"
# neccessary for the git client to work in the docker container
# the issue is possibly related to the ownership of the git data
# directory, and the root user running the git client
# todo: work out how to fix this
RUN git config --global --add safe.directory '*'
# Install necessary packages for Puppeteer
RUN apk add --no-cache \
chromium \
nss \
freetype \
harfbuzz \
ca-certificates \
ttf-freefont
# Set the Puppeteer executable path
ENV PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser
## Stage 2 (development)
# This stage is for development purposes
FROM base AS dev
ENV NODE_ENV=development
ENV PATH=/usr/src/app/node_modules/.bin:$PATH
RUN npm install
## Stage 3 (copy in source)
# This gets our source code into builder for use in next two stages
# It gets its own stage so we don't have to copy twice
# this stage starts from the first one and skips the last two
FROM base AS source
WORKDIR /usr/src/app
COPY ./app ./app
COPY ./scripts ./scripts
COPY ./config ./config
COPY ./notes ./notes
COPY ./todo.txt ./todo.txt
## Stage 4 (testing)
# This stage is used for running tests in CI
FROM source AS test
WORKDIR /usr/src/app
ENV NODE_ENV=test
# this copies all dependencies (prod+dev)
COPY --from=dev /usr/src/app/node_modules ./node_modules
# this copies the tests
COPY ./tests ./tests
# copy in the git repository so the news page can be generated
# inside the container, this is a bit of a hack and should be
# replaced with a better solution in the future
COPY .git .git
## Stage 5 (default, production)
# The final production stage
FROM source AS prod
# build the brochure static site and exit (i.e. dont watch for changes)
RUN node ./app/documentation/build/index.js --no-watch
HEALTHCHECK --interval=30s --timeout=5s --start-period=5s --retries=3 \
CMD curl --fail http://localhost:8080/health || exit 1
# Ensure the logfile directory exists with proper permissions
RUN mkdir -p /usr/src/app/data/logs/docker && chmod -R 0755 /usr/src/app/data/logs/docker
# 1.5gb max memory is 75% of the 2gb limit for the container
CMD ["sh", "-c", "node --max-old-space-size=1536 /usr/src/app/app/index.js >> /usr/src/app/data/logs/docker/app.log 2>&1"]