From cc5a0f32be2abe75a4a3716bd3aab7d06547fd24 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kratochv=C3=ADl?= <xkrato10@fi.muni.cz>
Date: Fri, 10 Jun 2022 00:52:56 +0200
Subject: [PATCH] feat: Docker - added frontend to docker-compose

---
 .env-example           |  3 +++
 docker-compose.yml     | 31 ++++++++++++++++++++++++++-----
 frontend/.dockerignore |  9 +++++++++
 frontend/Dockerfile    | 13 +++++++++++++
 frontend/package.json  |  2 +-
 5 files changed, 52 insertions(+), 6 deletions(-)
 create mode 100644 frontend/.dockerignore
 create mode 100644 frontend/Dockerfile

diff --git a/.env-example b/.env-example
index 935a3de..f8209f4 100644
--- a/.env-example
+++ b/.env-example
@@ -8,3 +8,6 @@ DB_NAME=film-db
 
 # ADMINER
 ADMINER_PORT=8080
+
+# CLIENT
+CLIENT_PORT=3000
diff --git a/docker-compose.yml b/docker-compose.yml
index f2c34d8..16ea757 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -15,10 +15,12 @@ services:
       # changes in host's ./backend directory will be propagated to the container
       - ./backend:/usr/src/app
       # this prevents our host files from overriding container's node_modules
-      - exclude:/usr/src/app/node_modules/
+      - exclude-server:/usr/src/app/node_modules/
     networks:
-      - app-network
+      - backend-network
     restart: always
+    depends_on:
+      - postgres-db
 
   postgres:
     container_name: postgres
@@ -27,7 +29,7 @@ services:
     volumes:
       - ./backend/database-storage:/var/lib/postgresql/data
     networks:
-      - app-network
+      - backend-network
     environment:
       POSTGRES_USER: ${DB_USER}
       POSTGRES_PASSWORD: ${DB_PASSWORD}
@@ -46,9 +48,28 @@ services:
     depends_on:
       - postgres-db
 
+  client:
+    container_name: client
+    image: client:1.0.0
+    build:
+      context: ./frontend
+      dockerfile: Dockerfile
+    env_file: .env
+    ports:
+      - 127.0.0.1:${CLIENT_PORT}:3000
+    volumes:
+      # changes in host's ./frontend directory will be propagated to the container
+      - ./frontend:/usr/src/app
+      # this prevents our host files from overriding container's node_modules
+      - exclude-client:/usr/src/app/node_modules/
+    restart: always
+    depends_on:
+      - server
+
 volumes:
-  exclude:
   database-storage:
+  exclude-server:
+  exclude-client:
 
 networks:
-  app-network:
+  backend-network:
diff --git a/frontend/.dockerignore b/frontend/.dockerignore
new file mode 100644
index 0000000..ca39985
--- /dev/null
+++ b/frontend/.dockerignore
@@ -0,0 +1,9 @@
+# Node modules folder
+node_modules
+
+# Git
+.gitignore
+
+# Docker files
+Dockerfile
+.dockerignore
diff --git a/frontend/Dockerfile b/frontend/Dockerfile
new file mode 100644
index 0000000..3776f62
--- /dev/null
+++ b/frontend/Dockerfile
@@ -0,0 +1,13 @@
+FROM node:16-alpine
+
+WORKDIR /usr/src
+
+COPY ["package.json", "package-lock.json*", "./"]
+RUN npm install
+ENV PATH=/usr/src/node_modules/.bin:$PATH
+
+WORKDIR /usr/src/app
+
+COPY . .
+
+CMD ["npm", "run", "dev"]
diff --git a/frontend/package.json b/frontend/package.json
index d465e2c..fb6322e 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -3,7 +3,7 @@
   "private": true,
   "version": "0.0.0",
   "scripts": {
-    "dev": "vite",
+    "dev": "vite --host",
     "build": "tsc && vite build",
     "preview": "vite preview"
   },
-- 
GitLab