From 6de661bf606321e7862a4f7f30a402c7ee0b7cfc Mon Sep 17 00:00:00 2001 From: silvadias Date: Sun, 29 Dec 2024 09:48:02 -0300 Subject: [PATCH 1/4] chore: configure Docker Compose for Node.js eCommerce app with MongoDB - Changed the server port from 8000 to 3000 to align with common Node.js development practices. - Added MongoDB service to create non-production test records, helping to simulate a realistic environment without affecting real data. - Configured the Node.js service to use the MongoDB connection URI (mongodb://mongo:27017/ecomerce) to connect to the database. - Added environment variables to set the Node.js environment to 'development' and MongoDB credentials for secure initialization. - Configured volumes for persistent data storage and database initialization scripts. - Ensured that the Node.js service waits for the MongoDB service to be ready before starting. - Highlighted that initial records for the database should be created in the by the eCommerce creator or later as needed. This setup enables easy testing of the eCommerce application in a local environment with a development database. --- docker-compose.yml | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100755 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100755 index 0000000..256d9fa --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,44 @@ +version: '3' + +services: + nodeService: + image: node:20-alpine + container_name: node + user: "node" + working_dir: /home/node/app + environment: + - NODE_ENV=development + - DB_URI=mongodb://mongo:27017/ecomerce + volumes: + - ./:/home/node/app + ports: + - 3000:3000 + command: > + sh -c " + npm install && + npm run start:dev + " + depends_on: + - db + networks: + - ecomerce + + db: + image: mongo:3 + container_name: mongo + environment: + - MONGO_INITDB_DATABASE=ecomerce + - MONGO_INITDB_ROOT_USERNAME=root + - MONGO_INITDB_ROOT_PASSWORD=root + + volumes: + - ./data:/data/db + - ./mongo-init:/docker-entrypoint-initdb.d + ports: + - "27017:27017" + networks: + - ecomerce + +networks: + ecomerce: + driver: bridge From 9a0e880caffd8f550043e559d683079ffc488378 Mon Sep 17 00:00:00 2001 From: silvadias Date: Sun, 29 Dec 2024 10:09:59 -0300 Subject: [PATCH 2/4] fix: changed the port from 8000 to 3000 and configured the development environment - Changed the server port from 8000 to 3000, which is more common in Node.js development environments. - Added the website URL for Stripe registration, enabling users to register and run the development environment. --- .env | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100755 .env diff --git a/.env b/.env new file mode 100755 index 0000000..9a79325 --- /dev/null +++ b/.env @@ -0,0 +1,17 @@ +## PORT +PORT=3000 + +## MongoDB URI +DB_URI= mongodb://mongo:27017/ecommerce + +## JWT access token +JWT_SECRET_KEY=chaveSecreta +JWT_EXPIRE_TIME=1h + +## GMAIL +MAILER_APP_EMAIL=SENDEREMAIL +MAILER_APP_PASSWORD=SENDERPASSWORD + +## STRIPE +STRIPE_SECRET=Create: https://dashboard.stripe.com/register +STRIPE_WEBHOOK_SECRET=Create: https://dashboard.stripe.com/register \ No newline at end of file From 65062d1da549f05d3b4c67f83d31a855413cdafb Mon Sep 17 00:00:00 2001 From: silvadias Date: Sun, 29 Dec 2024 10:18:31 -0300 Subject: [PATCH 3/4] fix: change server port from 8000 to 3000 - Changed the server port from 8000 to 3000 to align with common Node.js development practices. - This change ensures consistency with other Node.js applications, making it easier for developers to set up and configure the environment. --- server.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 server.js diff --git a/server.js b/server.js old mode 100644 new mode 100755 From d7c45d91c9f2274ca8c255989da675fde4f59f76 Mon Sep 17 00:00:00 2001 From: silvadias Date: Sun, 29 Dec 2024 11:29:59 -0300 Subject: [PATCH 4/4] Add installation and usage instructions for Docker Compose --- README.md | 114 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 86 insertions(+), 28 deletions(-) mode change 100644 => 100755 README.md diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 85f3a08..192945f --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Node.js-Full-E-Commerce-RESTFul-API-with- Cash and Online Payment ![dotenv](https://img.shields.io/badge/dotenv-3982CE?style=for-the-badge&logo=dotenv&logoColor=white1) ![swagger](https://img.shields.io/badge/swagger-00FF00?style=for-the-badge&logo=swagger&logoColor=white) ![eslint](https://img.shields.io/badge/eslint-8A118C?style=for-the-badge&logo=eslint&logoColor=white) - ![prettier](https://img.shields.io/badge/prettier-8A118C?style=for-the-badge&logo=prettier&logoColor=white) + ![prettier](https://img.shields.io/badge/prettier-8A118C?style=for-the-badge&logo=prettier&logoColor=white) ![docker-compose](https://img.shields.io/badge/docker-compose-%232496ED.svg?style=for-the-badge&logo=docker&logoColor=white)
@@ -51,46 +51,104 @@ Node.js-Full-E-Commerce-RESTFul-API-with- Cash and Online Payment ## Installation -1. **Clone the Repository:** - Use the `git clone` command to clone the GitHub repository to your local machine. + + +## Docker Support - "Simplify Your Development with Docker and Docker Compose!" + +E-shtery-app comes with Docker Compose configuration, so you can run the project easily in a containerized environment. No need to worry about setting up dependencies, as Docker Compose takes care of that for you. + +### Prerequisites + +Before you begin, make sure you have **Docker** and **Docker Compose** installed on your machine. + +1. **Install Docker on Linux (Ubuntu):** + + Run the following commands in your terminal: + ```bash - git clone https://github.com/alin00r/Node.js-Full-E-Commerce-RESTFul-App-with-Payment + sudo apt-get update + sudo apt-get install -y docker.io ``` -2. **Initialize a Package.json File (if not already done):** - If your project doesn't already have a `package.json` file, you can create one by running: + +2. **Install Docker Compose:** + + Run the following commands to install Docker Compose: + ```bash - npm init - # or - yarn init + sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose + sudo chmod +x /usr/local/bin/docker-compose ``` -3. **Install depends** + +### Running the Project with Docker Compose + +1. **Clone the Repository:** + Clone the GitHub repository to your local machine: + ```bash - npm install + git clone https://github.com/alin00r/Node.js-Full-E-Commerce-RESTFul-Api-with-Payment.git ``` -4. **Setting up env variables**
- - **Please first specifiy your database engine** +2. **Navigate to the Project Directory:** + Change to the project directory: - ```properties - ## PORT - PORT=YOUR PORT HERE + ```bash + cd Node.js-Full-E-Commerce-RESTFul-App-with-Payment + ``` - ## MongoDB URI - DB_URI= YOUR DATABASE URI +3. **Run the Docker Compose Command:** + + The project already includes the `docker-compose.yml` file. To start the application and all its services (including the MongoDB container), simply run: - ## JWT access token - JWT_SECRET_KEY=YOUR JWT ACCESS TOKEN SECRET - JWT_EXPIRE_TIME=YOUR JWT EXPIRE TIME + ## Docker Compose Workflow - ## GMAIL - MAILER_APP_EMAIL=SENDER EMAIL - MAILER_APP_PASSWORD=SENDER PASSWORD +To start the services in detached mode, where the containers are running in the background, use the following command: + +```bash +docker-compose up -d +``` +This will build the images and start the containers. + +## Bash with Logs +If you want to view the logs of the running services, you can execute the following command: + +```bash +docker-compose up +``` +This command will start the services and continuously display the logs in the terminal. To run commands inside the containers while monitoring logs, open another bash session for your commands. + +## Accessing Containers via Docker Exec + +To execute commands inside the Node container, use the following command: + +```bash +docker exec -it node sh +``` +This will open a bash shell inside the node container. + +For accessing the MongoDB container, use: + +```bash +docker exec -it mongo sh +``` +This command will open a bash shell inside the mongo container, allowing you to interact with the database directly. + + + +4. **Access the Project:** + + After running the command above, the project will be available at `http://localhost:3000`. You can also check your MongoDB container on `localhost:27017` if needed. + +5. **Stop the Docker Containers:** + + To stop the running containers, use: + +```bash +docker-compose down +``` +This setup will allow you to run the project seamlessly using Docker and Docker Compose, simplifying your development workflow. Happy coding! +![docker-compose](https://img.shields.io/badge/docker-compose-%232496ED.svg?style=for-the-badge&logo=docker&logoColor=white) - ## STRIPE - STRIPE_SECRET=Your STRIPE SECRET KEY - STRIPE_WEBHOOK_SECRET=Your STRIPE WEBHOOK SECRET KEY - ``` ## Routes