From 2db9ff5635c3cec3b1554058019f3056a9b1c5cd Mon Sep 17 00:00:00 2001 From: Wysogota Date: Thu, 25 Aug 2022 21:18:39 +0300 Subject: [PATCH] Set production docker config --- client/.dockerignore | 1 + client/.env | 2 +- client/Dockerfile | 25 +++++++ client/Dockerfile-dev | 2 +- client/nginx.conf | 11 ++++ docker-compose.yaml | 64 ++++++++++++++++++ server/.adminjs/bundle.js | 1 + server/.env | 4 +- server/.gitignore | 1 - server/Dockerfile | 23 +++++++ server/Dockerfile-dev | 4 +- server/config/mongoConfig.json | 2 +- server/config/postgresConfig.json | 8 +-- server/package.json | 7 +- server/src/server.js | 2 +- server/start-dev.sh | 15 +++++ server/start.sh | 2 +- server/yarn.lock | 104 ++++++++++++++++++++++++++++-- start.sh | 3 + 19 files changed, 260 insertions(+), 21 deletions(-) create mode 100644 client/Dockerfile create mode 100644 client/nginx.conf create mode 100644 docker-compose.yaml create mode 100644 server/.adminjs/bundle.js create mode 100644 server/Dockerfile create mode 100755 server/start-dev.sh create mode 100755 start.sh diff --git a/client/.dockerignore b/client/.dockerignore index 4b90444..c4562a6 100644 --- a/client/.dockerignore +++ b/client/.dockerignore @@ -1 +1,2 @@ ./node_modules +yarn.lock diff --git a/client/.env b/client/.env index 82583e9..78155b4 100644 --- a/client/.env +++ b/client/.env @@ -1,3 +1,3 @@ REACT_APP_DOMAIN=localhost -REACT_APP_SERVER_PORT=3000 +REACT_APP_SERVER_PORT=80 ESLINT_NO_DEV_ERRORS=true \ No newline at end of file diff --git a/client/Dockerfile b/client/Dockerfile new file mode 100644 index 0000000..0112afc --- /dev/null +++ b/client/Dockerfile @@ -0,0 +1,25 @@ +FROM node:16.15.1-alpine3.15 as build-stage + +ARG NODE_ENV="production" + +RUN mkdir -p ./client + +WORKDIR /client + +COPY package.json ./ + +# COPY yarn.lock ./ + +RUN yarn install + +COPY . . + +RUN npm rebuild node-sass + +RUN yarn run build + +FROM nginx:1.23.1-alpine + +COPY --from=build-stage /client/build /usr/share/nginx/html + +COPY ./nginx.conf /etc/nginx/conf.d/default.conf diff --git a/client/Dockerfile-dev b/client/Dockerfile-dev index 7ea1ab7..17e8490 100644 --- a/client/Dockerfile-dev +++ b/client/Dockerfile-dev @@ -18,4 +18,4 @@ RUN yarn install RUN mkdir -p node_modules/.cache && chmod -R 777 node_modules/.cache -CMD npm start +CMD yarn start diff --git a/client/nginx.conf b/client/nginx.conf new file mode 100644 index 0000000..3eacd96 --- /dev/null +++ b/client/nginx.conf @@ -0,0 +1,11 @@ +server { + listen 80; + + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ /index.html =404; + } + + include /etc/nginx/extra-conf.d/*.conf; +} \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..e3f1caa --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,64 @@ +version: '3' +services: + + front-prod: + build: + context: ./client + dockerfile: Dockerfile + environment: + NODE_ENV: production + depends_on: + - server-prod + volumes: + - ./client:/client + - /client/node_modules + networks: + - frontend + ports: + - "3000:80" + + server-prod: + build: + context: ./server + dockerfile: Dockerfile + environment: + NODE_ENV: production + ports: + - "80:80" + depends_on: + - db-prod + - mongo-prod + networks: + - frontend + - backend + volumes: + - ./server:/server + - /server/node_modules + + mongo-prod: + image: mongo:latest + networks: + - backend + ports: + - "27017:27017" + + db-prod: + image: postgres:14.3-alpine + environment: + - POSTGRES_PASSWORD=password + - POSTGRES_USER=postgres + - POSTGRES_DB=mangoose-prod + networks: + - backend + ports: + - "5432:5432" + +volumes: + server-prod: + + +networks: + frontend: + driver: bridge + backend: + driver: bridge diff --git a/server/.adminjs/bundle.js b/server/.adminjs/bundle.js new file mode 100644 index 0000000..1c79749 --- /dev/null +++ b/server/.adminjs/bundle.js @@ -0,0 +1 @@ +!function(){"use strict";AdminJS.UserComponents={}}(); diff --git a/server/.env b/server/.env index aaf5ae0..62d7f7f 100644 --- a/server/.env +++ b/server/.env @@ -1,6 +1,6 @@ DOMAIN=localhost -PORT=3000 -CLIENT_PORT=5000 +PORT=80 +CLIENT_PORT=3000 SALT_ROUNDS=4 ACCESS_TOKEN_SECRET=eyJpc3MiOiJhc2RhZGEiLCJpYXQiOjE2NTgzOTk3MTUsImV4cCI6MTY4OTkzNTcx ACCESS_TOKEN_TIME=1h diff --git a/server/.gitignore b/server/.gitignore index be0c3be..d3ef110 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -24,4 +24,3 @@ yarn-debug.log* yarn-error.log* # .env -/uploads diff --git a/server/Dockerfile b/server/Dockerfile new file mode 100644 index 0000000..589cc78 --- /dev/null +++ b/server/Dockerfile @@ -0,0 +1,23 @@ +FROM node:16.15.1-alpine3.15 + +ARG NODE_ENV="production" + +RUN mkdir /server + +WORKDIR /server + +COPY package.json ./ + +COPY yarn.lock ./ + +RUN yarn global add sequelize-cli + +RUN yarn global add @babel/core @babel/cli + +RUN yarn install + +COPY . . + +RUN yarn run build + +CMD ./start.sh diff --git a/server/Dockerfile-dev b/server/Dockerfile-dev index a55999e..7deac64 100644 --- a/server/Dockerfile-dev +++ b/server/Dockerfile-dev @@ -18,10 +18,8 @@ RUN yarn global add sequelize-cli RUN yarn install -RUN mkdir -p node_modules/.cache && chmod -R 777 node_modules/.cache - COPY . . EXPOSE 3000 -CMD ./start.sh +CMD ./start-dev.sh diff --git a/server/config/mongoConfig.json b/server/config/mongoConfig.json index 58c70b5..13b5cd8 100644 --- a/server/config/mongoConfig.json +++ b/server/config/mongoConfig.json @@ -6,7 +6,7 @@ }, "production": { "database": "mangoose-prod", - "host": "mongo-dev", + "host": "mongo-prod", "port": 27017 } } \ No newline at end of file diff --git a/server/config/postgresConfig.json b/server/config/postgresConfig.json index 2ed523d..2ada2b0 100644 --- a/server/config/postgresConfig.json +++ b/server/config/postgresConfig.json @@ -11,17 +11,17 @@ }, "test": { "username": "postgres", - "password": "admin", + "password": "password", "database": "mangoose-test", - "host": "db-dev", + "host": "db-test", "dialect": "postgres", "operatorsAliases": "Op" }, "production": { "username": "postgres", - "password": "admin", + "password": "password", "database": "mangoose-prod", - "host": "db-dev", + "host": "db-prod", "dialect": "postgres", "operatorsAliases": "Op" } diff --git a/server/package.json b/server/package.json index c6c908e..83926a2 100644 --- a/server/package.json +++ b/server/package.json @@ -5,13 +5,15 @@ "description": "", "main": "src/server.js", "scripts": { - "start": "nodemon server.js" + "start": "nodemon server.js", + "build": "babel src -d build" }, "keywords": [], "author": "Wysogota", "license": "ISC", "devDependencies": { - "cors": "^2.8.5", + "@babel/cli": "^7.18.10", + "@babel/core": "^7.18.13", "nodemon": "^2.0.16", "sequelize-cli": "^6.4.1" }, @@ -23,6 +25,7 @@ "axios": "^0.27.2", "bcrypt": "^5.0.1", "cookie-parser": "^1.4.6", + "cors": "^2.8.5", "dotenv": "^16.0.1", "express": "^4.18.1", "express-formidable": "^1.2.0", diff --git a/server/src/server.js b/server/src/server.js index e0dad5d..ca7d41e 100644 --- a/server/src/server.js +++ b/server/src/server.js @@ -3,6 +3,6 @@ const http = require('http'); require('./mongoose'); const app = require('./app'); const server = http.createServer(app); -const PORT = process.env.PORT || 3000; +const PORT = process.env.PORT || 80; server.listen(PORT, () => console.log('Server started on port = ' + PORT)); diff --git a/server/start-dev.sh b/server/start-dev.sh new file mode 100755 index 0000000..635a834 --- /dev/null +++ b/server/start-dev.sh @@ -0,0 +1,15 @@ +#!/bin/sh + +# Run Sequalize's migrations. +echo "-----> Running application migrations" +npx sequelize db:migrate +echo "" + +# Run Sequalize's seeds. +echo "-----> Running application seeds" +npx sequelize db:seed:all +echo "<----- Seeds created" + +echo "-----> Starting server" +yarn start +echo "" \ No newline at end of file diff --git a/server/start.sh b/server/start.sh index 635a834..3d7f742 100755 --- a/server/start.sh +++ b/server/start.sh @@ -11,5 +11,5 @@ npx sequelize db:seed:all echo "<----- Seeds created" echo "-----> Starting server" -yarn start +node ./build/server.js echo "" \ No newline at end of file diff --git a/server/yarn.lock b/server/yarn.lock index 0973574..998c15b 100644 --- a/server/yarn.lock +++ b/server/yarn.lock @@ -79,6 +79,22 @@ "@jridgewell/gen-mapping" "^0.1.0" "@jridgewell/trace-mapping" "^0.3.9" +"@babel/cli@^7.18.10": + version "7.18.10" + resolved "https://registry.yarnpkg.com/@babel/cli/-/cli-7.18.10.tgz#4211adfc45ffa7d4f3cee6b60bb92e9fe68fe56a" + integrity sha512-dLvWH+ZDFAkd2jPBSghrsFBuXrREvFwjpDycXbmUoeochqKYe4zNSLEJYErpLg8dvxvZYe79/MkN461XCwpnGw== + dependencies: + "@jridgewell/trace-mapping" "^0.3.8" + commander "^4.0.1" + convert-source-map "^1.1.0" + fs-readdir-recursive "^1.1.0" + glob "^7.2.0" + make-dir "^2.1.0" + slash "^2.0.0" + optionalDependencies: + "@nicolo-ribaudo/chokidar-2" "2.1.8-no-fsevents.3" + chokidar "^3.4.0" + "@babel/code-frame@^7.0.0", "@babel/code-frame@^7.10.4", "@babel/code-frame@^7.18.6": version "7.18.6" resolved "https://registry.yarnpkg.com/@babel/code-frame/-/code-frame-7.18.6.tgz#3b25d38c89600baa2dcc219edfa88a74eb2c427a" @@ -112,6 +128,27 @@ json5 "^2.2.1" semver "^6.3.0" +"@babel/core@^7.18.13": + version "7.18.13" + resolved "https://registry.yarnpkg.com/@babel/core/-/core-7.18.13.tgz#9be8c44512751b05094a4d3ab05fc53a47ce00ac" + integrity sha512-ZisbOvRRusFktksHSG6pjj1CSvkPkcZq/KHD45LAkVP/oiHJkNBZWfpvlLmX8OtHDG8IuzsFlVRWo08w7Qxn0A== + dependencies: + "@ampproject/remapping" "^2.1.0" + "@babel/code-frame" "^7.18.6" + "@babel/generator" "^7.18.13" + "@babel/helper-compilation-targets" "^7.18.9" + "@babel/helper-module-transforms" "^7.18.9" + "@babel/helpers" "^7.18.9" + "@babel/parser" "^7.18.13" + "@babel/template" "^7.18.10" + "@babel/traverse" "^7.18.13" + "@babel/types" "^7.18.13" + convert-source-map "^1.7.0" + debug "^4.1.0" + gensync "^1.0.0-beta.2" + json5 "^2.2.1" + semver "^6.3.0" + "@babel/generator@^7.18.10": version "7.18.12" resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.18.12.tgz#fa58daa303757bd6f5e4bbca91b342040463d9f4" @@ -121,6 +158,15 @@ "@jridgewell/gen-mapping" "^0.3.2" jsesc "^2.5.1" +"@babel/generator@^7.18.13": + version "7.18.13" + resolved "https://registry.yarnpkg.com/@babel/generator/-/generator-7.18.13.tgz#59550cbb9ae79b8def15587bdfbaa388c4abf212" + integrity sha512-CkPg8ySSPuHTYPJYo7IRALdqyjM9HCbt/3uOBEFbzyGVP6Mn8bwFPB0jX6982JVNBlYzM1nnPkfjuXSOPtQeEQ== + dependencies: + "@babel/types" "^7.18.13" + "@jridgewell/gen-mapping" "^0.3.2" + jsesc "^2.5.1" + "@babel/helper-annotate-as-pure@^7.15.4", "@babel/helper-annotate-as-pure@^7.16.0", "@babel/helper-annotate-as-pure@^7.18.6": version "7.18.6" resolved "https://registry.yarnpkg.com/@babel/helper-annotate-as-pure/-/helper-annotate-as-pure-7.18.6.tgz#eaa49f6f80d5a33f9a5dd2276e6d6e451be0a6bb" @@ -336,6 +382,11 @@ resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.18.11.tgz#68bb07ab3d380affa9a3f96728df07969645d2d9" integrity sha512-9JKn5vN+hDt0Hdqn1PiJ2guflwP+B6Ga8qbDuoF0PzzVhrzsKIJo8yGqVk6CmMHiMei9w1C1Bp9IMJSIK+HPIQ== +"@babel/parser@^7.18.13": + version "7.18.13" + resolved "https://registry.yarnpkg.com/@babel/parser/-/parser-7.18.13.tgz#5b2dd21cae4a2c5145f1fbd8ca103f9313d3b7e4" + integrity sha512-dgXcIfMuQ0kgzLB2b9tRZs7TTFFaGM2AbtA4fJgUUYukzGH4jwsS7hzQHEGs67jdehpm22vkgKwvbU+aEflgwg== + "@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression@^7.18.6": version "7.18.6" resolved "https://registry.yarnpkg.com/@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression/-/plugin-bugfix-safari-id-destructuring-collision-in-function-expression-7.18.6.tgz#da5b8f9a580acdfbe53494dba45ea389fb09a4d2" @@ -1070,6 +1121,22 @@ debug "^4.1.0" globals "^11.1.0" +"@babel/traverse@^7.18.13": + version "7.18.13" + resolved "https://registry.yarnpkg.com/@babel/traverse/-/traverse-7.18.13.tgz#5ab59ef51a997b3f10c4587d648b9696b6cb1a68" + integrity sha512-N6kt9X1jRMLPxxxPYWi7tgvJRH/rtoU+dbKAPDM44RFHiMH8igdsaSBgFeskhSl/kLWLDUvIh1RXCrTmg0/zvA== + dependencies: + "@babel/code-frame" "^7.18.6" + "@babel/generator" "^7.18.13" + "@babel/helper-environment-visitor" "^7.18.9" + "@babel/helper-function-name" "^7.18.9" + "@babel/helper-hoist-variables" "^7.18.6" + "@babel/helper-split-export-declaration" "^7.18.6" + "@babel/parser" "^7.18.13" + "@babel/types" "^7.18.13" + debug "^4.1.0" + globals "^11.1.0" + "@babel/types@^7.18.10", "@babel/types@^7.18.6", "@babel/types@^7.18.9", "@babel/types@^7.4.4": version "7.18.10" resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.18.10.tgz#4908e81b6b339ca7c6b7a555a5fc29446f26dde6" @@ -1079,6 +1146,15 @@ "@babel/helper-validator-identifier" "^7.18.6" to-fast-properties "^2.0.0" +"@babel/types@^7.18.13": + version "7.18.13" + resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.18.13.tgz#30aeb9e514f4100f7c1cb6e5ba472b30e48f519a" + integrity sha512-ePqfTihzW0W6XAU+aMw2ykilisStJfDnsejDCXRchCcMJ4O0+8DhPXf2YUbZ6wjBlsEmZwLK/sPweWtu8hcJYQ== + dependencies: + "@babel/helper-string-parser" "^7.18.10" + "@babel/helper-validator-identifier" "^7.18.6" + to-fast-properties "^2.0.0" + "@carbon/icon-helpers@^10.28.0": version "10.32.0" resolved "https://registry.yarnpkg.com/@carbon/icon-helpers/-/icon-helpers-10.32.0.tgz#700c3ffbe6350cf481f174c0fb06c9a4269a5248" @@ -1246,7 +1322,7 @@ resolved "https://registry.yarnpkg.com/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.14.tgz#add4c98d341472a289190b424efbdb096991bb24" integrity sha512-XPSJHWmi394fuUuzDnGz1wiKqWfo1yXecHQMRf2l6hztTO+nPru658AyDngaBe7isIxEkRsPR3FZh+s7iVa4Uw== -"@jridgewell/trace-mapping@^0.3.9": +"@jridgewell/trace-mapping@^0.3.8", "@jridgewell/trace-mapping@^0.3.9": version "0.3.15" resolved "https://registry.yarnpkg.com/@jridgewell/trace-mapping/-/trace-mapping-0.3.15.tgz#aba35c48a38d3fd84b37e66c9c0423f9744f9774" integrity sha512-oWZNOULl+UbhsgB51uuZzglikfIKSUBO/M9W2OfEjn7cmqoAiCgmv9lyACTUacZwBz0ITnJ2NqjU8Tx0DHL88g== @@ -1269,6 +1345,11 @@ semver "^7.3.5" tar "^6.1.11" +"@nicolo-ribaudo/chokidar-2@2.1.8-no-fsevents.3": + version "2.1.8-no-fsevents.3" + resolved "https://registry.yarnpkg.com/@nicolo-ribaudo/chokidar-2/-/chokidar-2-2.1.8-no-fsevents.3.tgz#323d72dd25103d0c4fbdce89dadf574a787b1f9b" + integrity sha512-s88O1aVtXftvp5bCPB7WnmXc5IwOZZ7YPuwNPt+GtOOXpPvad1LfbmjYv+qII7zP6RU2QGnqve27dnLycEnyEQ== + "@popperjs/core@^2.9.0": version "2.11.6" resolved "https://registry.yarnpkg.com/@popperjs/core/-/core-2.11.6.tgz#cee20bd55e68a1720bdab363ecf0c821ded4cd45" @@ -2192,7 +2273,7 @@ chalk@^4.1.0: ansi-styles "^4.1.0" supports-color "^7.1.0" -chokidar@^3.5.2: +chokidar@^3.4.0, chokidar@^3.5.2: version "3.5.3" resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-3.5.3.tgz#1cf37c8707b932bd1af1ae22c0432e2acd1903bd" integrity sha512-Dr3sfKRP6oTcjf2JmUmFJfeVMvXBdegxB0iVQ5eb2V10uFJUCAS8OByZdVAyVb8xXNz3GjjTgj9kLWsZTqE6kw== @@ -2304,6 +2385,11 @@ commander@^2.19.0, commander@^2.20.0, commander@^2.20.3: resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33" integrity sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ== +commander@^4.0.1: + version "4.1.1" + resolved "https://registry.yarnpkg.com/commander/-/commander-4.1.1.tgz#9fd602bd936294e9e9ef46a3f4d6964044b18068" + integrity sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA== + commander@^5.1.0: version "5.1.0" resolved "https://registry.yarnpkg.com/commander/-/commander-5.1.0.tgz#46abbd1652f8e059bddaef99bbdcb2ad9cf179ae" @@ -2354,7 +2440,7 @@ content-type@~1.0.4: resolved "https://registry.yarnpkg.com/content-type/-/content-type-1.0.4.tgz#e138cc75e040c727b1966fe5e5f8c9aee256fe3b" integrity sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA== -convert-source-map@^1.5.0, convert-source-map@^1.7.0: +convert-source-map@^1.1.0, convert-source-map@^1.5.0, convert-source-map@^1.7.0: version "1.8.0" resolved "https://registry.yarnpkg.com/convert-source-map/-/convert-source-map-1.8.0.tgz#f3373c32d21b4d780dd8004514684fb791ca4369" integrity sha512-+OQdjP49zViI/6i7nIJpA8rAl4sV/JdPfU9nZs3VqOwGIgizICvuN2ru6fMd+4llL0tar18UYJXfZ/TWtmhUjA== @@ -2890,6 +2976,11 @@ fs-minipass@^2.0.0: dependencies: minipass "^3.0.0" +fs-readdir-recursive@^1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/fs-readdir-recursive/-/fs-readdir-recursive-1.1.0.tgz#e32fc030a2ccee44a6b5371308da54be0b397d27" + integrity sha512-GNanXlVr2pf02+sPN40XN8HG+ePaNcvM0q5mZBd668Obwb0yD5GiUbZOFgwn8kGMY6I3mdyDJzieUy3PTYyTRA== + fs.realpath@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" @@ -2951,7 +3042,7 @@ glob-parent@~5.1.2: dependencies: is-glob "^4.0.1" -glob@^7.1.3, glob@^7.1.6: +glob@^7.1.3, glob@^7.1.6, glob@^7.2.0: version "7.2.3" resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.3.tgz#b8df0fb802bbfa8e89bd1d938b4e16578ed44f2b" integrity sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q== @@ -4729,6 +4820,11 @@ simple-update-notifier@^1.0.7: dependencies: semver "~7.0.0" +slash@^2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/slash/-/slash-2.0.0.tgz#de552851a1759df3a8f206535442f5ec4ddeab44" + integrity sha512-ZYKh3Wh2z1PpEXWr0MpSBZ0V6mZHAQfYevttO11c51CaWjGTaadiKZ+wVt1PbMlDV5qhMFslpZCemhwOK7C89A== + slash@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634" diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..bb113e7 --- /dev/null +++ b/start.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +docker-compose --file docker-compose.yaml up --build