Skip to content

Commit acdb72b

Browse files
committed
Add Docker Compose configurations for CouchDB and MongoDB with supporting documentation and test script
- Introduced `docker-compose.couchdb.yml` and `docker-compose.mongodb.yml` for streamlined setup with database backends. - Updated `README.md` with detailed instructions on using Docker Compose environments. - Added `.env` template file and required environment variable configurations for security. - Included a `test-docker-setups.sh` script for validating Docker Compose setups and running automated API tests. - Updated `notes.Dockerfile` to add `wget` for health checks. - Ensured robust health checks and service dependencies in both configurations.
1 parent 2649769 commit acdb72b

6 files changed

Lines changed: 435 additions & 1 deletion

File tree

README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,110 @@ Stable work on lower versions is not guaranteed.
7272
```
7373
Adjust the values according to your environment. Set `DB_VENDOR` to either `couchdb` or `mongodb` to select the database vendor.
7474

75+
## Docker Compose Setup
76+
77+
The application includes separate Docker Compose configurations for easy deployment with different database backends. This is the recommended way to run the application locally for development and testing.
78+
79+
### Available Configurations
80+
81+
- **docker-compose.couchdb.yml**: Runs the application with CouchDB
82+
- **docker-compose.mongodb.yml**: Runs the application with MongoDB
83+
84+
### ⚠️ Required: Database Credentials Configuration
85+
86+
**IMPORTANT**: For security reasons, this application requires explicit database credentials. No default passwords are provided.
87+
88+
You **must** create a `.env` file with your own secure credentials before running any Docker Compose setup:
89+
90+
```bash
91+
# Copy the template file
92+
cp env.template .env
93+
94+
# Edit the .env file with your own secure credentials
95+
# The .env file is automatically ignored by git for security
96+
```
97+
98+
Required `.env` file content:
99+
```env
100+
# MongoDB Configuration (all required)
101+
MONGODB_USERNAME=your_mongodb_user
102+
MONGODB_PASSWORD=your_secure_mongodb_password
103+
MONGODB_DATABASE=your_notes_database
104+
105+
# CouchDB Configuration (all required)
106+
COUCHDB_USERNAME=your_couchdb_user
107+
COUCHDB_PASSWORD=your_secure_couchdb_password
108+
COUCHDB_DATABASE=your_notes_database
109+
```
110+
111+
**Security Notes:**
112+
- Use strong, unique passwords
113+
- Never use default credentials like `admin/password`
114+
- Keep your `.env` file secure and never commit it to version control
115+
- The application will fail to start if any credentials are missing
116+
117+
### Running with CouchDB
118+
119+
```bash
120+
# Start the application with CouchDB
121+
docker compose -f docker-compose.couchdb.yml up -d
122+
123+
# Stop the application
124+
docker compose -f docker-compose.couchdb.yml down
125+
```
126+
127+
### Running with MongoDB
128+
129+
```bash
130+
# Start the application with MongoDB
131+
docker compose -f docker-compose.mongodb.yml up -d
132+
133+
# Stop the application
134+
docker compose -f docker-compose.mongodb.yml down
135+
```
136+
137+
### Services Included
138+
139+
Each Docker Compose setup includes:
140+
141+
- **Database service**: Either CouchDB (port 5984) or MongoDB (port 27017)
142+
- **Notes application**: The Node.js API server (port 3000)
143+
- **Health checks**: Automatic health monitoring for both services
144+
- **Data persistence**: Named volumes for database data
145+
- **Network isolation**: Services communicate through a private network
146+
147+
### Automated Testing
148+
149+
A test script is provided to validate both setups:
150+
151+
```bash
152+
# Make the script executable (first time only)
153+
chmod +x test-docker-setups.sh
154+
155+
# Test both database setups
156+
./test-docker-setups.sh
157+
158+
# Test only CouchDB setup
159+
./test-docker-setups.sh couchdb
160+
161+
# Test only MongoDB setup
162+
./test-docker-setups.sh mongodb
163+
```
164+
165+
The test script will:
166+
- Start the specified database setup
167+
- Wait for services to be healthy
168+
- Test all API endpoints (health, CRUD operations)
169+
- Verify the web UI is accessible
170+
- Clean up by stopping the services
171+
172+
### Accessing the Application
173+
174+
Once started with either setup, the application will be available at:
175+
- **Web UI**: http://localhost:3000
176+
- **API**: http://localhost:3000/api/notes
177+
- **Health Check**: http://localhost:3000/health
178+
75179
## Running the Application
76180

77181
Start the server:

docker-compose.couchdb.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
version: '3.8'
2+
3+
# Define common environment variables (explicit configuration required)
4+
x-couchdb-variables: &couchdb-variables
5+
COUCHDB_USERNAME: ${COUCHDB_USERNAME:?COUCHDB_USERNAME is required. Please set it in .env file or environment}
6+
COUCHDB_PASSWORD: ${COUCHDB_PASSWORD:?COUCHDB_PASSWORD is required. Please set it in .env file or environment}
7+
COUCHDB_DATABASE: ${COUCHDB_DATABASE:?COUCHDB_DATABASE is required. Please set it in .env file or environment}
8+
9+
services:
10+
couchdb:
11+
build:
12+
context: .
13+
dockerfile: couchdb.Dockerfile
14+
container_name: notes-couchdb
15+
environment:
16+
<<: *couchdb-variables
17+
COUCHDB_USER: ${COUCHDB_USERNAME}
18+
COUCHDB_PASSWORD: ${COUCHDB_PASSWORD}
19+
ports:
20+
- "5984:5984"
21+
volumes:
22+
- couchdb_data:/opt/couchdb/data
23+
networks:
24+
- notes-network
25+
healthcheck:
26+
test: ["CMD", "curl", "-f", "http://${COUCHDB_USERNAME}:${COUCHDB_PASSWORD}@localhost:5984/_up"]
27+
interval: 30s
28+
timeout: 10s
29+
retries: 5
30+
start_period: 30s
31+
32+
notes-app:
33+
build:
34+
context: .
35+
dockerfile: notes.Dockerfile
36+
container_name: notes-app-couchdb
37+
environment:
38+
<<: *couchdb-variables
39+
NODE_ENV: production
40+
HOST: 0.0.0.0
41+
PORT: 3000
42+
DB_VENDOR: couchdb
43+
COUCHDB_URL: http://${COUCHDB_USERNAME}:${COUCHDB_PASSWORD}@couchdb:5984
44+
COUCHDB_DB_NAME: ${COUCHDB_DATABASE}
45+
ports:
46+
- "3000:3000"
47+
depends_on:
48+
couchdb:
49+
condition: service_healthy
50+
networks:
51+
- notes-network
52+
healthcheck:
53+
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3000/health"]
54+
interval: 30s
55+
timeout: 10s
56+
retries: 3
57+
start_period: 40s
58+
59+
volumes:
60+
couchdb_data:
61+
driver: local
62+
63+
networks:
64+
notes-network:
65+
driver: bridge

docker-compose.mongodb.yml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
version: '3.8'
2+
3+
# Define common environment variables (explicit configuration required)
4+
x-mongo-variables: &mongo-variables
5+
MONGODB_USERNAME: ${MONGODB_USERNAME:?MONGODB_USERNAME is required. Please set it in .env file or environment}
6+
MONGODB_PASSWORD: ${MONGODB_PASSWORD:?MONGODB_PASSWORD is required. Please set it in .env file or environment}
7+
MONGODB_DATABASE: ${MONGODB_DATABASE:?MONGODB_DATABASE is required. Please set it in .env file or environment}
8+
9+
services:
10+
mongodb:
11+
build:
12+
context: .
13+
dockerfile: mongodb.Dockerfile
14+
container_name: notes-mongodb
15+
environment:
16+
<<: *mongo-variables
17+
MONGO_INITDB_ROOT_USERNAME: ${MONGODB_USERNAME}
18+
MONGO_INITDB_ROOT_PASSWORD: ${MONGODB_PASSWORD}
19+
MONGO_INITDB_DATABASE: ${MONGODB_DATABASE}
20+
ports:
21+
- "27017:27017"
22+
volumes:
23+
- mongodb_data:/data/db
24+
networks:
25+
- notes-network
26+
healthcheck:
27+
test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
28+
interval: 30s
29+
timeout: 10s
30+
retries: 5
31+
start_period: 30s
32+
33+
notes-app:
34+
build:
35+
context: .
36+
dockerfile: notes.Dockerfile
37+
container_name: notes-app-mongodb
38+
environment:
39+
<<: *mongo-variables
40+
NODE_ENV: production
41+
HOST: 0.0.0.0
42+
PORT: 3000
43+
DB_VENDOR: mongodb
44+
MONGODB_URL: mongodb://${MONGODB_USERNAME}:${MONGODB_PASSWORD}@mongodb:27017?authSource=admin
45+
MONGODB_DB_NAME: ${MONGODB_DATABASE}
46+
ports:
47+
- "3000:3000"
48+
depends_on:
49+
mongodb:
50+
condition: service_healthy
51+
networks:
52+
- notes-network
53+
healthcheck:
54+
test: ["CMD", "wget", "--no-verbose", "--tries=1", "--spider", "http://localhost:3000/health"]
55+
interval: 30s
56+
timeout: 10s
57+
retries: 3
58+
start_period: 40s
59+
60+
volumes:
61+
mongodb_data:
62+
driver: local
63+
64+
networks:
65+
notes-network:
66+
driver: bridge

env.template

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Environment template for Docker Compose setups
2+
# Copy this file to .env and set your own secure values
3+
# ALL VARIABLES ARE REQUIRED - no defaults provided for security
4+
5+
# MongoDB Configuration (set your own secure credentials)
6+
MONGODB_USERNAME=your_mongodb_username
7+
MONGODB_PASSWORD=your_secure_mongodb_password
8+
MONGODB_DATABASE=your_database_name
9+
10+
# CouchDB Configuration (set your own secure credentials)
11+
COUCHDB_USERNAME=your_couchdb_username
12+
COUCHDB_PASSWORD=your_secure_couchdb_password
13+
COUCHDB_DATABASE=your_database_name
14+
15+
# SECURITY NOTE:
16+
# - Never use default credentials like admin/password
17+
# - Use strong passwords in production
18+
# - Keep your .env file secure and never commit it to version control

notes.Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
FROM node:22.14-alpine3.21
1+
FROM node:22.16-alpine3.21
2+
3+
# Install wget for health checks
4+
RUN apk add --no-cache wget
25

36
# Set the working directory inside the container
47
WORKDIR /app

0 commit comments

Comments
 (0)