Skip to content

Instantly share code, notes, and snippets.

@gornostal
Last active May 22, 2023 19:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gornostal/3d46df685fe093a1c98e39156a286262 to your computer and use it in GitHub Desktop.
Save gornostal/3d46df685fe093a1c98e39156a286262 to your computer and use it in GitHub Desktop.
React JS app in Docker with Nginx
FROM node:12-alpine as build
WORKDIR /www
ENV REACT_APP_KEYCLOAK_URL %REACT_APP_KEYCLOAK_URL%
ENV REACT_APP_KEYCLOAK_REALM %REACT_APP_KEYCLOAK_REALM%
COPY package*json ./
RUN npm install
COPY . .
RUN npm run build
FROM nginx:1.19.9-alpine
WORKDIR /www
COPY --from=build /www/build .
COPY entrypoint.sh .
COPY nginx.conf /etc/nginx/conf.d/default.conf
CMD ./entrypoint.sh
#!/bin/sh
set -ex
replace_var() {
# $1 var name
# $2 file name
eval "value=\$$1"
if [ -z "$value" ]; then
echo "Undefined variable $1"
exit 1
fi
sed -i "s,%$1%,$value,g" $2
}
# go through all JS files and replace %VAR_NAME% with VAR_NAME value from env variables
find . -type f -name "*.js" | while read filename; do
replace_var REACT_APP_KEYCLOAK_URL $filename
replace_var REACT_APP_KEYCLOAK_REALM $filename
done
nginx -g "daemon off;"
map $sent_http_content_type $expires {
default off;
text/html epoch;
text/css max;
application/javascript max;
~image/ max;
}
server {
listen 80 default_server;
server_name webapp;
root /www;
expires $expires;
##
# Gzip Settings
##
gzip on;
gzip_http_version 1.1;
gzip_disable "MSIE [1-6]\.";
gzip_min_length 1100;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_comp_level 9;
# Routes without file extension e.g. /user/1
location / {
try_files $uri /index.html;
}
# 404 if a file is requested (so the main app isn't served)
# make sure this regex doesn't catch paths that end with .\d+, because we're using versions (xxx.yyy.zzz) in URL path params
location ~ ^.+\.[a-zA-Z].+$ {
try_files $uri =404;
}
}
map $sent_http_content_type $expires {
default off;
text/html epoch;
text/css max;
application/javascript max;
~image/ max;
}
server {
listen 80 default_server;
server_name webapp;
root /www;
expires $expires;
##
# Gzip Settings
##
gzip on;
gzip_http_version 1.1;
gzip_disable "MSIE [1-6]\.";
gzip_min_length 1100;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_comp_level 9;
# Routes without file extension e.g. /user/1
location / {
try_files $uri /index.html;
}
# 404 if a file is requested (so the main app isn't served)
# make sure this regex doesn't catch paths that end with .\d+, because we're using versions (xxx.yyy.zzz) in URL path params
location ~ ^.+\.[a-zA-Z].+$ {
try_files $uri =404;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment