diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..a6bf938ea1375b17bd2e8fe52002515a4fe76e53 --- /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 0c89e3b702423a07fc3a57cc5e0cbe377515855e..8400ba26dd979495e4c825f186de58892c6fdfcc 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 0000000000000000000000000000000000000000..1d21bd3b6dcce7c166aaa57241c893d7bace6a5e --- /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 12594fef07de8219932c75fd842ef0fd0d72753b..8863f0ea1abe9b6bed16d7de4a103d84f88428ff 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}`);