From 3bf5441c054cce3e4a25ad323f5b6577d0853939 Mon Sep 17 00:00:00 2001 From: drsky Date: Sun, 12 Nov 2023 14:19:21 +0100 Subject: [PATCH 1/6] feature: Add Dockerfile and .dockerignore to build app s Docker image --- .dockerignore | 4 ++++ Dockerfile | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 000000000..34c8c132a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +./mode_modules +Dockerfile +.dockerignore +docker-compose.yml \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..bc1b04ff9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM node:12.22.1 + +# Dockerize is required to wait for the MongoDB container to be ready.ENV DOCKERIZE_VERSION v0.6.1 +RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \ + && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \ + && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz + +WORKDIR /usr/src/app +COPY package*.json ./ +RUN npm install +COPY . . + +# This script starts 'npm run dev:server' and, once the app is running, +# it executes the script to populate the database. +COPY startServerAndPopulate.sh . +RUN chmod +x startServerAndPopulate.sh + +CMD dockerize -wait tcp://mongo_db:27017 -timeout 30s ./startServerAndPopulate.sh From e9b4b4491a85c32a6d949b5016cc853ef4d3cbf8 Mon Sep 17 00:00:00 2001 From: drsky Date: Sun, 12 Nov 2023 14:21:00 +0100 Subject: [PATCH 2/6] feature: Implement script to start server and populate database --- startServerAndPopulate.sh | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 startServerAndPopulate.sh diff --git a/startServerAndPopulate.sh b/startServerAndPopulate.sh new file mode 100644 index 000000000..aec1fbe91 --- /dev/null +++ b/startServerAndPopulate.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +( + while ! curl --output /dev/null --silent --head --fail http://localhost:3001/api-docs/; do + echo 'waiting for localhost:3001/api-docs to be available' + sleep 1 + done + echo 'API documentation is available, starting to populate the database...' + npm run populate-db + echo 'Database population script has been executed. App is ready.' +) & + +echo 'Starting the development server...' +npm run dev:server From e34c739841ed42223fb7c2711335ed50f8d64eee Mon Sep 17 00:00:00 2001 From: drsky Date: Sun, 12 Nov 2023 15:01:32 +0100 Subject: [PATCH 3/6] bugfix: add docker version for dockerize --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index bc1b04ff9..c94a88a91 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,7 @@ FROM node:12.22.1 # Dockerize is required to wait for the MongoDB container to be ready.ENV DOCKERIZE_VERSION v0.6.1 +ENV DOCKERIZE_VERSION v0.6.1 RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \ && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \ && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz From 7f9a8ffa787df9c53c0833726682f1285add7fb1 Mon Sep 17 00:00:00 2001 From: drsky Date: Sun, 12 Nov 2023 15:03:09 +0100 Subject: [PATCH 4/6] feature: Add docker-compose for MongoDB and app services --- docker-compose.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..d138152fa --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,22 @@ +version: '3.9' + +services: + # Mongodb service + mongo_db: + image: mongo:latest + restart: always + ports: + - 27017:27017 + + # Node api service + argentbank_api: + depends_on: + - mongo_db + container_name: argentbank_api + build: . + ports: + # local->container + - 3001:3001 + environment: + PORT: 3001 + DATABASE_URL: 'mongodb://mongo_db/argentBankDB' From 034f4582fd85c429dc978990b2b9b3f6865a5135 Mon Sep 17 00:00:00 2001 From: drsky Date: Sun, 12 Nov 2023 15:27:45 +0100 Subject: [PATCH 5/6] chore: downgrade docker-compose file version from 3.9 to 3 --- docker-compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index d138152fa..8013cd913 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -version: '3.9' +version: '3' services: # Mongodb service From da8f27f844da5ef058618ab67a0a86c1374af412 Mon Sep 17 00:00:00 2001 From: drsky Date: Sun, 12 Nov 2023 15:36:49 +0100 Subject: [PATCH 6/6] docs: update instructions for dockerized setup --- README.md | 40 ++++++++++++++-------------------------- 1 file changed, 14 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 973bdefd7..1a4239943 100644 --- a/README.md +++ b/README.md @@ -4,46 +4,34 @@ This codebase contains the code needed to run the backend for Argent Bank. ## Getting Started -### Prerequisites +## Prerequisites -Argent Bank uses the following tech stack: +For the dockerized version of Argent Bank, you only need to have Docker installed: -- [Node.js v12](https://nodejs.org/en/) -- [MongoDB Community Server](https://www.mongodb.com/try/download/community) +- [Docker](https://www.docker.com/get-started) -Please make sure you have the right versions and download both packages. You can verify this by using the following commands in your terminal: +Please make sure you have Docker installed and running on your machine. You can verify this by using the following command in your terminal: ```bash -# Check Node.js version -node --version - -# Check Mongo version -mongo --version +# Check Docker version +docker --version ``` ### Instructions -1. Fork this repo -1. Clone the repo onto your computer -1. Open a terminal window in the cloned project -1. Run the following commands: +1. Fork this repo. +2. Clone the repo onto your computer. +3. Open a terminal window in the cloned project directory. +4. Run the following commands: ```bash -# Install dependencies -npm install - -# Start local dev server -npm run dev:server - -# Populate database with two users -npm run populate-db +# Build and start the application and database using Docker +docker-compose up --build -d ``` -Your server should now be running at http://locahost:3001 and you will now have two users in your MongoDB database! - -## Populated Database Data +The application will be available on http://localhost:3001 after the build completes. -Once you run the `populate-db` script, you should have two users in your database: +The database will be populated automatically. ### Tony Stark