From 0690c7c3830a67c26e8013c9ad8836b53ee13b86 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 30 Sep 2025 02:03:34 +0000 Subject: [PATCH] feat: Add Docker support for the application This commit introduces comprehensive Docker support to streamline the development and deployment process. Key changes include: - A `Dockerfile` that containerizes the application using the efficient `node:20-alpine` base image. It sets up the working directory, installs dependencies, and configures the application to run on container start. - A `.dockerignore` file to exclude unnecessary files like `node_modules` and `.git` from the build context, resulting in a smaller and more secure image. - A `docker-compose.yml` file to simplify running the application. It defines the service, manages environment variables with sensible defaults, maps ports, and configures named volumes for `sessions` and `uploads` to ensure data persistence across container restarts. - The `README.md` has been updated with a new "Running with Docker" section, providing clear, step-by-step instructions for getting the application running with both Docker Compose (recommended) and manual Docker commands. The commands have been updated to use the modern `docker compose` syntax. --- .dockerignore | 9 +++++++ Dockerfile | 20 ++++++++++++++ README.md | 65 ++++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 17 ++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3437787 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,9 @@ +# Ignore dependencies, git, and sensitive files +node_modules +npm-debug.log +.git +.gitignore + +# Ignore session and upload data +sessions/ +uploads/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a65d077 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,20 @@ +# Use the official Node.js 20 Alpine image as a base +FROM node:20-alpine + +# Set the working directory in the container +WORKDIR /usr/src/app + +# Copy package.json and package-lock.json to the working directory +COPY package*.json ./ + +# Install dependencies +RUN npm install + +# Copy the rest of the application source code +COPY . . + +# Expose the port the app runs on +EXPOSE 3000 + +# Command to run the application +CMD [ "npm", "start" ] \ No newline at end of file diff --git a/README.md b/README.md index 0fc8101..a6da630 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,71 @@ npm start --- +## 🐳 Running with Docker + +You can also run this application using Docker and Docker Compose for a more isolated and reproducible setup. + +### **1. Using Docker Compose (Recommended)** + +This is the easiest way to get started. + +1. **Create an Environment File**: + Create a `.env` file in the root of the project. This file will be used by Docker Compose to set the environment variables inside the container. + + ``` + # .env + + # The master key to protect the server + MASTER_API_KEY=yoursecretkey + + # The port to run the server on (optional, defaults to 3000) + PORT=3000 + ``` + +2. **Build and Run the Container**: + Run the following command to build the Docker image and start the container in the background: + + ```bash + docker compose up --build -d + ``` + + The server will now be running on the port you specified (or the default, 3000). + +3. **To Stop the Server**: + ```bash + docker compose down + ``` + +### **2. Using Docker (Manual)** + +If you prefer not to use Docker Compose, you can build and run the container manually. + +1. **Build the Docker Image**: + ```bash + docker build -t whatsapp-api . + ``` + +2. **Run the Docker Container**: + You must pass the `MASTER_API_KEY` and map the port. You also need to create and mount volumes to persist the `sessions` and `uploads` data. + + ```bash + docker run -d \ + -p 3000:3000 \ + -e MASTER_API_KEY="yoursecretkey" \ + -e PORT="3000" \ + --name whatsapp-api-container \ + -v whatsapp_sessions:/usr/src/app/sessions \ + -v whatsapp_uploads:/usr/src/app/uploads \ + whatsapp-api + ``` + - `-d`: Run in detached mode. + - `-p`: Map port 3000 on your host to port 3000 in the container. + - `-e`: Set environment variables. + - `--name`: Assign a name to the container. + - `-v`: Mount named volumes to persist data. + +--- + ## 📖 API Documentation All endpoints are prefixed with `/api`. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..009af11 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,17 @@ +services: + whatsapp-api: + build: . + container_name: whatsapp_api + restart: unless-stopped + ports: + - "${PORT:-3000}:3000" + environment: + - MASTER_API_KEY=${MASTER_API_KEY:-yoursecretkey} + - PORT=${PORT:-3000} + volumes: + - whatsapp_sessions:/usr/src/app/sessions + - whatsapp_uploads:/usr/src/app/uploads + +volumes: + whatsapp_sessions: + whatsapp_uploads: \ No newline at end of file