From c3b24b1071af1153734f6aff6b29e21f22430ce1 Mon Sep 17 00:00:00 2001
From: strokh24 <Rokas.Stankunas@Student.Reutlingen-University.DE>
Date: Wed, 30 Oct 2024 12:22:18 +0100
Subject: [PATCH] Containerized application and updated README.md file

---
 Dockerfile         | 17 +++++++++++++++++
 README.md          | 27 ++++++++-------------------
 docker-compose.yml | 16 ++++++++++++++++
 mongodb.js         |  2 +-
 4 files changed, 42 insertions(+), 20 deletions(-)
 create mode 100644 Dockerfile
 create mode 100644 docker-compose.yml

diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..a6bf938
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,17 @@
+# We're taking the latest Alpine version, because it's the smallest
+# and has the least amount of dependencies
+FROM alpine:latest
+
+# Set working directory.
+WORKDIR /app
+
+# Copy the current directory contents (whole project) into the container at /app
+ADD . /app
+
+# Install dependencies
+RUN apk update && \
+    apk add --no-cache nodejs npm && \
+    npm install;
+
+# Run the app
+ENTRYPOINT ["node", "server"]
\ No newline at end of file
diff --git a/README.md b/README.md
index 0c89e3b..8400ba2 100644
--- a/README.md
+++ b/README.md
@@ -27,27 +27,16 @@ lists. **Use MongoDB as the database.**
 
 ---
 ## Deployment
-The deployment consists of 2 parts: deploying the database and the application.
-1. Create a docker compose file:
-```yml
-services:
-  mongodb:
-    image: mongo:latest
-    container_name: mongodb
-    ports:
-      - "27017:27017"
-```
-2. Start docker compose file
-`sudo docker compose up -d`
-3. Clone the repository:
+In exercise 1, the deployment consisted of 2 parts: deploying the database and the application. However, in exercise 2, the application got containerized, so you can deploy it by following these simple steps:
+1. Clone the repository:
 `git clone https://gitlab.reutlingen-university.de/gajesh24/cloudcomputing_act1.git`
-4. Enter the folder:
+2. Enter the folder:
 `cd cloudcomputing_act1`
-5. Install dependencies:
-`npm install`
-6. Start server
-`node server`
-7. Open up website on `http://localhost:3000/`
+3. Build the image:
+`sudo docker build -t todo-app .`
+4. Start docker compose file
+`sudo docker compose up -d`
+5. Open up website on `http://localhost/`
 
 ---
 ## Authors
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..1d21bd3
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,16 @@
+services:
+  mongodb:
+    image: mongo:latest
+    container_name: mongodb
+    ports:
+      - "27017:27017"
+
+  todo-app:
+    image: todo-app
+    container_name: todo-app
+    ports:
+      - "80:3000"
+    depends_on:
+      - mongodb
+    environment:
+      - MONGO_URI=mongodb://mongodb:27017/todo-app
diff --git a/mongodb.js b/mongodb.js
index 12594fe..8863f0e 100644
--- a/mongodb.js
+++ b/mongodb.js
@@ -3,7 +3,7 @@ require('dotenv').config();
 
 const connectDB = async () => {
   try {
-    const conn = await mongoose.connect(process.env.MONGO_URI);
+    const conn = await mongoose.connect(process.env.MONGO_URI || "mongodb://mongodb:27017/todo-app");
     console.log(`MongoDB connected: ${conn.connection.host}`);
   } catch (err) {
     console.error(`Error: ${err.message}`);
-- 
GitLab