From 70a2f34b7c5bac3a970413579cb7a82ce348cd30 Mon Sep 17 00:00:00 2001 From: Yehor Potebenko <hitechnic.uu68@gmail.com> Date: Wed, 10 May 2023 21:50:44 +0200 Subject: [PATCH] feat: containerize front-end, back-end and database parts in different containers, apply declarative approach, provide the opportunity to connect to database with credentials, entered at the application runtime in the terminal --- .env | 2 ++ docker-compose.yml | 42 ++++++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- server/.env | 2 +- server/Dockerfile | 13 +++++++++++++ server/index.js | 6 +++--- src/Dockerfile | 15 +++++++++++++++ 7 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 .env create mode 100644 docker-compose.yml create mode 100644 server/Dockerfile create mode 100644 src/Dockerfile diff --git a/.env b/.env new file mode 100644 index 0000000..8c2600d --- /dev/null +++ b/.env @@ -0,0 +1,2 @@ +BACKEND_PORT=3000 +FRONTEND_PORT=8080 \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..275fcc9 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,42 @@ +version: '1.0.0' + +services: + frontend: + build: + dockerfile: src/Dockerfile + env_file: + - .env + container_name: frontend-side + ports: + - '${FRONTEND_PORT}:${FRONTEND_PORT}' + stdin_open: true + tty: true + depends_on: + - backend + networks: + - letsDo-network + + backend: + build: + context: ./server + env_file: + - ./server/.env + container_name: node-server + ports: + - '${BACKEND_PORT}:${BACKEND_PORT}' + restart: always + depends_on: + - database + environment: + - DB_USER + - DB_PASSWORD + networks: + - letsDo-network + + database: + image: mongo + container_name: mongo-db + +networks: + letsDo-network: + driver: bridge diff --git a/package.json b/package.json index 58e31a8..4bdd824 100644 --- a/package.json +++ b/package.json @@ -83,5 +83,6 @@ "dependencies": { "@babel/polyfill": "^7.12.1", "normalize.css": "^8.0.1" - } + }, + "proxy": "http://node-api:3000" } \ No newline at end of file diff --git a/server/.env b/server/.env index 11f8090..d73bc73 100644 --- a/server/.env +++ b/server/.env @@ -1,4 +1,4 @@ -PORT=3000 +BACKEND_PORT=3000 DB_USER=yehorTodo19 DB_PASSWORD=Ik04adTsdmJrXt6d DB_NAME=test diff --git a/server/Dockerfile b/server/Dockerfile new file mode 100644 index 0000000..c24a7e1 --- /dev/null +++ b/server/Dockerfile @@ -0,0 +1,13 @@ +FROM node:18-alpine + +WORKDIR /letsDo-app-root + +COPY package.json . + +RUN npm install + +COPY . . + +EXPOSE ${BACKEND_PORT} + +CMD [ "npm", "start" ] \ No newline at end of file diff --git a/server/index.js b/server/index.js index 92291ee..46acaee 100644 --- a/server/index.js +++ b/server/index.js @@ -10,7 +10,7 @@ const app = express(); dotenv.config(); // * Constants -const { PORT } = process.env || 3000; +const { BACKEND_PORT } = process.env || 3000; const { DB_USER, DB_PASSWORD, DB_NAME } = process.env; // * Middleware @@ -27,8 +27,8 @@ async function start() { `mongodb+srv://${DB_USER}:${DB_PASSWORD}@cluster0.ooena3i.mongodb.net/${DB_NAME}` ); - app.listen(PORT, () => { - console.log(`Server started on port: ${PORT}`); + app.listen(BACKEND_PORT, () => { + console.log(`Server started on port: ${BACKEND_PORT}`); }); return null; diff --git a/src/Dockerfile b/src/Dockerfile new file mode 100644 index 0000000..7fb0d13 --- /dev/null +++ b/src/Dockerfile @@ -0,0 +1,15 @@ +FROM node:18-alpine + +WORKDIR /letsDo-app-root + +ADD src src + +COPY ../webpack.config.mjs . + +COPY ../package.json . + +RUN npm install + +EXPOSE ${FRONTEND_PORT} + +CMD [ "npm", "start" ] \ No newline at end of file -- GitLab