Skip to content

feat: Add Docker Compose setup for frontend, backend, and MongoDB - #223

Open
Vinayak097 wants to merge 3 commits into
adrianhajdin:PART_1_and_2from
Vinayak097:PART_1_and_2
Open

Vinayak097 wants to merge 3 commits into
adrianhajdin:PART_1_and_2from
Vinayak097:PART_1_and_2

Conversation

@Vinayak097

@Vinayak097 Vinayak097 commented Jun 15, 2026 •

Copy link
Copy Markdown

Summary

This PR adds Docker support to the project, allowing the frontend, backend, and MongoDB database to run in separate containers using Docker Compose.

Changes

  • Added Dockerfile for the React frontend
  • Added Dockerfile for the Express backend
  • Added docker-compose.yml for container orchestration
  • Added .dockerignore files to optimize Docker builds
  • Configured MongoDB as a separate service
  • Updated environment variable configuration for Docker networking
  • Added documentation for running the application with Docker

How to Run

docker compose up --build

Frontend: http://localhost:3000/

Backend: http://localhost:5000/

MongoDB: mongodb://localhost:27017

Related Issue

Closes #217

Summary by CodeRabbit

  • Chores
    • Added Docker containerization for the client and server applications
    • Implemented Docker Compose configuration to orchestrate MongoDB, server, and client services
    • Updated build tooling dependencies for improved compatibility
    • Configured application to use local MongoDB instance

@coderabbitai

coderabbitai Bot commented Jun 15, 2026 •

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Adds Docker support to the MERN stack project: new Dockerfiles for the client (port 3000) and server (port 5000), .dockerignore files for both, and a docker-compose.yml orchestrating all three services. The server's MongoDB connection string is switched from Atlas to the compose-internal hostname, and nodemon, react-scripts 5.0.1, and babel-eslint are added as dependencies.

Changes

Docker Containerization

Layer / File(s) Summary
Dockerfiles and build context ignores
client/Dockerfile, client/.dockerignore, server/Dockerfile, server/.dockerignore
Creates node:18-alpine-based Dockerfiles for client (port 3000) and server (port 5000), each backed by .dockerignore files that exclude node_modules, build output, and git metadata.
Docker Compose orchestration
docker-compose.yml
Defines three services — mongodb (mongo:7 with mongo_data named volume), server (built from ./server, depends on mongodb, CONNECTION_URL env var set), and client (built from ./client, depends on server) — with all port mappings and service dependencies declared.
Application changes for container environment
server/index.js, server/package.json, client/package.json
Replaces the Atlas SRV connection string with mongodb://mongodb:27017/memories to match the compose service hostname; adds nodemon to server dependencies; upgrades react-scripts from 3.4.1 to 5.0.1 and adds babel-eslint to client devDependencies.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 Hop hop, the containers align,
Three services humming, everything fine!
MongoDB nestled with a volume to keep,
Server and client from slumber awake from sleep.
No more Atlas strings — local is grand,
Docker Compose sets up the whole band! 🎶

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Out of Scope Changes check ❓ Inconclusive All changes directly support Docker setup objectives. However, package.json modifications (react-scripts upgrade, nodemon addition) and babel-eslint addition are tangential dependencies not explicitly required by issue #217. Clarify whether the react-scripts upgrade from 3.4.1 to 5.0.1 and nodemon/babel-eslint additions are necessary for Docker functionality or separate improvements.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main changes: adding Docker Compose setup for frontend, backend, and MongoDB services.
Linked Issues check ✅ Passed The PR fulfills all coding requirements from issue #217: Dockerfiles for backend and frontend, docker-compose.yml orchestrating all services, .dockerignore files, and updated connection configuration.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@client/Dockerfile`:
- Around line 1-13: Both the client/Dockerfile (lines 1-13) and
server/Dockerfile (lines 1-13) are running containers as root due to missing
USER directives, creating a security vulnerability. For each file, after the
COPY instruction that copies the application files, add a RUN instruction to
change ownership of the /app directory using chown command (chown -R node:node
/app), then add a USER node directive before the CMD instruction to ensure the
container runs as the non-root node user instead of root.

In `@docker-compose.yml`:
- Around line 13-14: The depends_on section using the short form only ensures
MongoDB container starts but does not wait for it to be healthy and ready to
accept connections. Replace the short form depends_on with the expanded form
that includes a condition parameter set to service_healthy, which will make the
backend service wait for MongoDB to pass its health check before attempting to
start. This requires that the mongodb service has a healthcheck defined in its
configuration.

In `@server/index.js`:
- Line 17: The CONNECTION_URL constant is hardcoded with the MongoDB hostname
instead of using the environment variable from docker-compose.yml. Replace the
hardcoded string value 'mongodb://mongodb:27017/memories' with
process.env.CONNECTION_URL to use the environment variable passed from Compose,
which ensures the connection works in both Docker and non-Docker environments.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 87cfdb3d-4ce8-4390-bd07-799e979f205b

📥 Commits

Reviewing files that changed from the base of the PR and between 9c2c416 and 8cb3a5b.

⛔ Files ignored due to path filters (2)
  • client/package-lock.json is excluded by !**/package-lock.json
  • server/package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (8)
  • client/.dockerignore
  • client/Dockerfile
  • client/package.json
  • docker-compose.yml
  • server/.dockerignore
  • server/Dockerfile
  • server/index.js
  • server/package.json

Comment thread client/Dockerfile
Comment on lines +1 to +13
FROM node:18-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000

CMD ["npm","start"] No newline at end of file

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Both Docker images run as root at runtime due the same missing USER directive.
This is one shared security posture gap across both service images.

  • client/Dockerfile#L1-L13: after copying sources, switch ownership and set USER node before CMD.
  • server/Dockerfile#L1-L13: apply the same non-root runtime pattern (chown + USER node) before CMD.
🧰 Tools
🪛 Trivy (0.69.3)

[error] 1-1: Image user should not be 'root'

Specify at least 1 USER command in Dockerfile with non-root user as argument

Rule: DS-0002

Learn more

(IaC/Dockerfile)

📍 Affects 2 files
  • client/Dockerfile#L1-L13 (this comment)
  • server/Dockerfile#L1-L13
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@client/Dockerfile` around lines 1 - 13, Both the client/Dockerfile (lines
1-13) and server/Dockerfile (lines 1-13) are running containers as root due to
missing USER directives, creating a security vulnerability. For each file, after
the COPY instruction that copies the application files, add a RUN instruction to
change ownership of the /app directory using chown command (chown -R node:node
/app), then add a USER node directive before the CMD instruction to ensure the
container runs as the non-root node user instead of root.

Source: Linters/SAST tools

Comment thread docker-compose.yml
Comment on lines +13 to +14
depends_on:
- mongodb

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Gate backend startup on MongoDB health, not only startup order.

Line 13/Line 14 use short depends_on, which does not wait for MongoDB readiness. This can race the server boot and cause failed initial connections.

Suggested fix
 services:
   mongodb:
     image: mongo:7
     ports:
       - "27017:27017"
     volumes:
       - mongo_data:/data/db
+    healthcheck:
+      test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
+      interval: 10s
+      timeout: 5s
+      retries: 5

   server:
     build: ./server
     ports:
       - "5000:5000"
     depends_on:
-      - mongodb
+      mongodb:
+        condition: service_healthy
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
depends_on:
- mongodb
services:
mongodb:
image: mongo:7
ports:
- "27017:27017"
volumes:
- mongo_data:/data/db
healthcheck:
test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
interval: 10s
timeout: 5s
retries: 5
server:
build: ./server
ports:
- "5000:5000"
depends_on:
mongodb:
condition: service_healthy
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docker-compose.yml` around lines 13 - 14, The depends_on section using the
short form only ensures MongoDB container starts but does not wait for it to be
healthy and ready to accept connections. Replace the short form depends_on with
the expanded form that includes a condition parameter set to service_healthy,
which will make the backend service wait for MongoDB to pass its health check
before attempting to start. This requires that the mongodb service has a
healthcheck defined in its configuration.

Comment thread server/index.js
app.use('/posts', postRoutes);

const CONNECTION_URL = 'mongodb+srv://js_mastery:123123123@practice.jto9p.mongodb.net/test';
const CONNECTION_URL = 'mongodb://mongodb:27017/memories';

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Use process.env.CONNECTION_URL instead of hardcoding the Compose hostname.

Line 17 hardcodes mongodb and ignores the environment variable passed from Compose (docker-compose.yml, Line 16). This also breaks non-Docker runs where mongodb DNS does not exist.

Suggested fix
-const CONNECTION_URL = 'mongodb://mongodb:27017/memories';
+const CONNECTION_URL = process.env.CONNECTION_URL || 'mongodb://localhost:27017/memories';
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const CONNECTION_URL = 'mongodb://mongodb:27017/memories';
const CONNECTION_URL = process.env.CONNECTION_URL || 'mongodb://localhost:27017/memories';
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@server/index.js` at line 17, The CONNECTION_URL constant is hardcoded with
the MongoDB hostname instead of using the environment variable from
docker-compose.yml. Replace the hardcoded string value
'mongodb://mongodb:27017/memories' with process.env.CONNECTION_URL to use the
environment variable passed from Compose, which ensures the connection works in
both Docker and non-Docker environments.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Docker Compose setup with separate containers for frontend(client), backend(server), and database(mongodb)

1 participant