-
Notifications
You must be signed in to change notification settings - Fork 23
/
Dockerfile
145 lines (114 loc) · 4.78 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
# Copyright Red Hat
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Install dependencies only when needed
FROM registry.access.redhat.com/ubi9/nodejs-18-minimal AS deps
USER root
# Install yarn & node-gyp dependency
RUN \
curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | tee /etc/yum.repos.d/yarn.repo && \
microdnf install -y yarn python3 gcc-c++ make && \
npm install --build-from-resource node-gyp
# Project non-specific args
ARG PROJECT_NAME
ARG SITE_URL
ARG NEXT_PUBLIC_BASE_PATH
ARG NEXT_PUBLIC_ANALYTICS_WRITE_KEY
# Landing page specific args
ARG NEXT_PUBLIC_DOCSEARCH_APP_ID
ARG NEXT_PUBLIC_DOCSEARCH_API_KEY
ARG NEXT_PUBLIC_DOCSEARCH_INDEX_NAME
# Building different architectures via emulation is slow with Yarn
# This increases the timeout period so it can properly download dependencies
# Value is in milliseconds and is set to 60 minutes
# To increase/decrease you can override via --build-arg YARN_TIMEOUT=x in your build command
ARG YARN_TIMEOUT=3600000
# Check if the PROJECT_NAME build argument is set
RUN \
if [ "$PROJECT_NAME" == "landing-page" ] || [ "$PROJECT_NAME" == "registry-viewer" ]; then echo "Building project \"${PROJECT_NAME}\"."; \
else echo "Build argument \"PROJECT_NAME\" needs to be set to either \"landing-page\" or \"registry-viewer\"." && exit 1; \
fi
WORKDIR /app
# Install dependencies
COPY package.json yarn.lock* ./
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile --network-timeout $YARN_TIMEOUT; \
else echo "Lockfile not found." && exit 1; \
fi
# Rebuild the source code only when needed
FROM registry.access.redhat.com/ubi9/nodejs-18-minimal AS builder
USER root
# Install yarn
RUN \
curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | tee /etc/yum.repos.d/yarn.repo && \
microdnf install -y yarn
# Project non-specific args
ARG PROJECT_NAME
ARG SITE_URL
ARG NEXT_PUBLIC_BASE_PATH
ARG NEXT_PUBLIC_ANALYTICS_WRITE_KEY
# Landing page specific args
ARG NEXT_PUBLIC_DOCSEARCH_APP_ID
ARG NEXT_PUBLIC_DOCSEARCH_API_KEY
ARG NEXT_PUBLIC_DOCSEARCH_INDEX_NAME
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
ENV NEXT_TELEMETRY_DISABLED 1
RUN yarn nx build ${PROJECT_NAME} --skip-nx-cache
# Production image, copy all the files and run next
FROM registry.access.redhat.com/ubi9/nodejs-18-minimal AS runner
USER root
# Install shadow-utils to use groupadd and useradd
RUN microdnf install shadow-utils -y
# Install react-env to update environment variables during runtime
RUN npm install -g @beam-australia/react-env
# Project non-specific args
ARG PROJECT_NAME
ARG SITE_URL
ARG NEXT_PUBLIC_BASE_PATH
ARG NEXT_PUBLIC_ANALYTICS_WRITE_KEY
# Landing page specific args
ARG NEXT_PUBLIC_DOCSEARCH_APP_ID
ARG NEXT_PUBLIC_DOCSEARCH_API_KEY
ARG NEXT_PUBLIC_DOCSEARCH_INDEX_NAME
WORKDIR /app
ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
ENV NEXT_TELEMETRY_DISABLED 1
RUN useradd nextjs -u 1001
COPY --from=builder --chown=nextjs:0 /app/apps/${PROJECT_NAME}/dist/public ./apps/${PROJECT_NAME}/public
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:0 /app/apps/${PROJECT_NAME}/dist/.next/standalone ./
COPY --from=builder --chown=nextjs:0 /app/apps/${PROJECT_NAME}/dist/.next/static ./apps/${PROJECT_NAME}/dist/.next/static
RUN chown -R 1001:0 ./apps/${PROJECT_NAME} && chmod -R g=u ./apps/${PROJECT_NAME}
# Add license
RUN mkdir -p /licenses
COPY LICENSE /licenses
USER 1001
EXPOSE 3000
ENV PORT 3000
# Convert args to env vars to remain persistent after build
ENV PROJECT_NAME=$PROJECT_NAME
ENV SITE_URL=$SITE_URL
ENV NEXT_PUBLIC_BASE_PATH=$NEXT_PUBLIC_BASE_PATH
ENV NEXT_PUBLIC_ANALYTICS_WRITE_KEY=$NEXT_PUBLIC_ANALYTICS_WRITE_KEY
ENV NEXT_PUBLIC_DOCSEARCH_APP_ID=$NEXT_PUBLIC_DOCSEARCH_APP_ID
ENV NEXT_PUBLIC_DOCSEARCH_API_KEY=$NEXT_PUBLIC_DOCSEARCH_API_KEY
ENV NEXT_PUBLIC_DOCSEARCH_INDEX_NAME=$NEXT_PUBLIC_DOCSEARCH_INDEX_NAME
ENTRYPOINT react-env --prefix NEXT_PUBLIC --dest apps/${PROJECT_NAME}/public --path .env.production && \
node apps/${PROJECT_NAME}/server.js