Skip to content

Commit b7b5ccc

Browse files
feat: Add Docker support for the application (#3)
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. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 6217b7c commit b7b5ccc

4 files changed

Lines changed: 111 additions & 0 deletions

File tree

‎.dockerignore‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Ignore dependencies, git, and sensitive files
2+
node_modules
3+
npm-debug.log
4+
.git
5+
.gitignore
6+
7+
# Ignore session and upload data
8+
sessions/
9+
uploads/

‎Dockerfile‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use the official Node.js 20 Alpine image as a base
2+
FROM node:20-alpine
3+
4+
# Set the working directory in the container
5+
WORKDIR /usr/src/app
6+
7+
# Copy package.json and package-lock.json to the working directory
8+
COPY package*.json ./
9+
10+
# Install dependencies
11+
RUN npm install
12+
13+
# Copy the rest of the application source code
14+
COPY . .
15+
16+
# Expose the port the app runs on
17+
EXPOSE 3000
18+
19+
# Command to run the application
20+
CMD [ "npm", "start" ]

‎README.md‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,71 @@ npm start
5151

5252
---
5353

54+
## 🐳 Running with Docker
55+
56+
You can also run this application using Docker and Docker Compose for a more isolated and reproducible setup.
57+
58+
### **1. Using Docker Compose (Recommended)**
59+
60+
This is the easiest way to get started.
61+
62+
1. **Create an Environment File**:
63+
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.
64+
65+
```
66+
# .env
67+
68+
# The master key to protect the server
69+
MASTER_API_KEY=yoursecretkey
70+
71+
# The port to run the server on (optional, defaults to 3000)
72+
PORT=3000
73+
```
74+
75+
2. **Build and Run the Container**:
76+
Run the following command to build the Docker image and start the container in the background:
77+
78+
```bash
79+
docker compose up --build -d
80+
```
81+
82+
The server will now be running on the port you specified (or the default, 3000).
83+
84+
3. **To Stop the Server**:
85+
```bash
86+
docker compose down
87+
```
88+
89+
### **2. Using Docker (Manual)**
90+
91+
If you prefer not to use Docker Compose, you can build and run the container manually.
92+
93+
1. **Build the Docker Image**:
94+
```bash
95+
docker build -t whatsapp-api .
96+
```
97+
98+
2. **Run the Docker Container**:
99+
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.
100+
101+
```bash
102+
docker run -d \
103+
-p 3000:3000 \
104+
-e MASTER_API_KEY="yoursecretkey" \
105+
-e PORT="3000" \
106+
--name whatsapp-api-container \
107+
-v whatsapp_sessions:/usr/src/app/sessions \
108+
-v whatsapp_uploads:/usr/src/app/uploads \
109+
whatsapp-api
110+
```
111+
- `-d`: Run in detached mode.
112+
- `-p`: Map port 3000 on your host to port 3000 in the container.
113+
- `-e`: Set environment variables.
114+
- `--name`: Assign a name to the container.
115+
- `-v`: Mount named volumes to persist data.
116+
117+
---
118+
54119
## 📖 API Documentation
55120
56121
All endpoints are prefixed with `/api`.

‎docker-compose.yml‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
services:
2+
whatsapp-api:
3+
build: .
4+
container_name: whatsapp_api
5+
restart: unless-stopped
6+
ports:
7+
- "${PORT:-3000}:3000"
8+
environment:
9+
- MASTER_API_KEY=${MASTER_API_KEY:-yoursecretkey}
10+
- PORT=${PORT:-3000}
11+
volumes:
12+
- whatsapp_sessions:/usr/src/app/sessions
13+
- whatsapp_uploads:/usr/src/app/uploads
14+
15+
volumes:
16+
whatsapp_sessions:
17+
whatsapp_uploads:

0 commit comments

Comments
 (0)