From 726246778a40d010d0f9e0f257bfd1ef8502ac2a Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Sun, 15 Jan 2023 17:07:02 -0800 Subject: [PATCH] Working to let react app work in Docker Signed-off-by: James Ketrenos --- .dockerignore | 2 ++ Dockerfile | 24 +++++++++++------ docker-compose.yml | 4 +-- server/development.location | 14 ++++++++++ server/nginx.conf | 54 +++++++++++++++++++++++++++++++++++++ server/production.location | 6 +++++ 6 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 server/development.location create mode 100644 server/nginx.conf create mode 100644 server/production.location diff --git a/.dockerignore b/.dockerignore index cf068f7..5f671e1 100644 --- a/.dockerignore +++ b/.dockerignore @@ -17,3 +17,5 @@ !src !util !ketrface +!client +!scripts diff --git a/Dockerfile b/Dockerfile index 1735373..26595e0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -45,33 +45,41 @@ WORKDIR /website RUN npm upgrade && npm install WORKDIR /website/client -COPY /website/client/package*json /website/client +COPY /client/package*json /website/client/ RUN npm upgrade && npm install COPY /config/default.json /website/config/default.json COPY /*js /website/ COPY /src /website/src -COPY /client /website/client +COPY /client/public /website/client/public +COPY /client/src /website/client/src COPY /scanner /website/scanner COPY /server /website/server COPY /frontend /website/frontend COPY /ketrface /website/ketrface +# Switch to bash instead of sh +SHELL [ "/bin/bash", "-c" ] + +ARG DEVELOPMENT= + # If not DEVELOPMENT mode, copy production config, else development RUN \ if [[ -z "${DEVELOPMENT}" ]]; then \ - echo "Building PRODUCTION" ; \ - cp /website/server/production.location /etc/nginx/snippets/active.location ; \ + echo "Building PRODUCTION" ; \ + cp /website/server/production.location \ + /etc/nginx/snippets/active.location ; \ else \ - echo "Building DEVELOPMENT" ; \ - cp /website/server/development.location /etc/nginx/snippets/active.location ; \ + echo "Building DEVELOPMENT" ; \ + cp /website/server/development.location \ + /etc/nginx/snippets/active.location ; \ fi # If not DEVELOPMENT mode, then locally build project RUN \ if [[ -z "${DEVELOPMENT}" ]]; then \ - cd /website/client ; \ - npm run build ; \ + cd /website/client ; \ + npm run build ; \ fi COPY /scripts /opt/scripts diff --git a/docker-compose.yml b/docker-compose.yml index 09ffacf..e537a26 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,8 +17,8 @@ services: # - db restart: always ports: - - ${PORT}:${TARGET_PORT:-8123} - - 127.0.0.1:14200:4200 # shellinabox + - ${PORT}:80 # nginx -> server/app.js express app + - 127.0.0.1:14200:4200 # shellinabox volumes: - ${PICTURES}:/pictures - ${PWD}/db:/website/db diff --git a/server/development.location b/server/development.location new file mode 100644 index 0000000..bef2e9b --- /dev/null +++ b/server/development.location @@ -0,0 +1,14 @@ +# DEVELOPMENT -- use npm development server on port 3000 (entrypoint.sh) +location / { + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header Host $http_host; + proxy_set_header X-NginX-Proxy true; + proxy_pass_header Set-Cookie; + proxy_pass_header P3P; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_pass https://localhost:3000/; +} \ No newline at end of file diff --git a/server/nginx.conf b/server/nginx.conf new file mode 100644 index 0000000..65f2e19 --- /dev/null +++ b/server/nginx.conf @@ -0,0 +1,54 @@ +server { + root /website; + index index.html; + + access_log /var/log/nginx/access.log; + error_log stderr; + + autoindex on; + + # 'active.conf' is copied during docker build based on whether + # DEVELOPMENT is set (development.conf) or not (production.conf) + include snippets/active.location; + + # proxy_pass has automatic redirect from v1 -> v1/ + location /api/v1/ { + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header Host $http_host; + proxy_set_header X-NginX-Proxy true; + proxy_pass_header Set-Cookie; + proxy_pass_header P3P; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_pass http://localhost:8123/api/v1/; + } + + # This routes all traffic to the Node Express server + # + # Eventually, a SSO layer in nginx can be used so the Express + # server does not need to be part of serving static content. + # + # See https://gist.github.com/mjbnz/b402edf819a69e517b0c59710f291da9 + # for example. + location / { + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + proxy_set_header Host $http_host; + proxy_set_header X-NginX-Proxy true; + proxy_pass_header Set-Cookie; + proxy_pass_header P3P; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_pass https://localhost:8123/; + } + + location ~ /(db|conf) { + deny all; + return 404; + } +} diff --git a/server/production.location b/server/production.location new file mode 100644 index 0000000..3a654ce --- /dev/null +++ b/server/production.location @@ -0,0 +1,6 @@ +# PRODUCTION -- pre-built source +location / { + try_files $uri $uri/ =404; + alias /website/client/build/; + index index.html; +} \ No newline at end of file