-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
96 lines (83 loc) · 2.94 KB
/
Dockerfile
File metadata and controls
96 lines (83 loc) · 2.94 KB
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
ARG NODE_VERSION=20
FROM node:${NODE_VERSION}-alpine AS builder
WORKDIR /app
# Enable Yarn Berry
RUN corepack enable && corepack prepare yarn@stable --activate
# Install deps
COPY package.json yarn.lock .yarnrc.yml ./
COPY .yarn ./.yarn
RUN yarn install --immutable --frozen-lockfile
# Build app (inject build‑time args if you like)
COPY . .
ARG REACT_APP_NAME
ARG REACT_APP_DEFAULT_INSTANCE
ARG REACT_APP_LOCK_TO_DEFAULT_INSTANCE
ARG REACT_APP_INSTANCE_SELECTION_MODE
ENV \
REACT_APP_NAME=$REACT_APP_NAME \
REACT_APP_DEFAULT_INSTANCE=$REACT_APP_DEFAULT_INSTANCE \
REACT_APP_LOCK_TO_DEFAULT_INSTANCE=$REACT_APP_LOCK_TO_DEFAULT_INSTANCE \
REACT_APP_INSTANCE_SELECTION_MODE=$REACT_APP_INSTANCE_SELECTION_MODE
RUN yarn build \
&& rm -rf dist/*.map
# ─── Runtime stage ───────────────────────────
FROM nginx:alpine AS runtime
# install envsubst
RUN apk add --no-cache gettext
# copy built files
COPY --from=builder /app/dist /usr/share/nginx/html
COPY --from=builder /app/package.json /usr/share/nginx/html/package.json
# rename the real index.html so we can template it
RUN mv /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.tmpl
# write our custom nginx config (all routes → index.html)
RUN printf '%s\n' \
'server {' \
' listen 80;' \
' server_name _;' \
'' \
' root /usr/share/nginx/html;' \
' index index.html;' \
'' \
' # React Router support: serve index.html for any path' \
' location / {' \
' try_files $uri $uri/ /index.html;' \
' }' \
'' \
' # long‑term caching for static assets' \
' location ~* \.(?:js|css|png|jpg|jpeg|gif|svg|ico)$ {' \
' expires 1y;' \
' add_header Cache-Control "public, immutable";' \
' }' \
'' \
' error_page 500 502 503 504 /50x.html;' \
' location = /50x.html {' \
' root /usr/share/nginx/html;' \
' }' \
'}' \
> /etc/nginx/conf.d/default.conf
# embed entrypoint script via heredoc
RUN cat << 'EOF' > /usr/local/bin/docker-entrypoint.sh
#!/bin/sh
set -e
# assemble a small JS snippet from runtime ENV
cat << JS > /tmp/env-block.js
window.REACT_APP_DEFAULT_INSTANCE = "${REACT_APP_DEFAULT_INSTANCE}";
window.REACT_APP_NAME = "${REACT_APP_NAME}";
window.REACT_APP_LOCK_TO_DEFAULT_INSTANCE = "${REACT_APP_LOCK_TO_DEFAULT_INSTANCE}";
window.REACT_APP_INSTANCE_SELECTION_MODE = "${REACT_APP_INSTANCE_SELECTION_MODE}";
JS
# merge template → final index.html, splicing in our JS snippet
envsubst '$REACT_APP_DEFAULT_INSTANCE $REACT_APP_NAME $REACT_APP_LOCK_TO_DEFAULT_INSTANCE $REACT_APP_INSTANCE_SELECTION_MODE' \
< /usr/share/nginx/html/index.html.tmpl \
| sed -e '/REPLACE_ENV_VARS/ {
r /tmp/env-block.js
d
}' \
> /usr/share/nginx/html/index.html
# hand off to nginx
exec "$@"
EOF
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
EXPOSE 80
ENTRYPOINT ["docker-entrypoint.sh"]
CMD ["nginx", "-g", "daemon off;"]