Fix MongoDB hostname + build contexts - #24
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a critical Docker Compose configuration issue where the backend service attempted to connect to a MongoDB instance with hostname mongodb, but the Docker service was named mongo. The PR also relocates the docker-compose.yaml file from the repository root to the mern/ directory and updates build contexts accordingly.
Changes:
- Fixed MongoDB hostname from
mongodbtomongoin backend connection string - Moved docker-compose.yaml to mern/ directory with corrected build contexts
- Added MongoDB healthcheck and service dependency management
- Removed outdated GitHub Actions workflow and root-level docker-compose.yaml
- Added basic README documentation
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| mern/docker-compose.yaml | New compose file with correct MongoDB service name, healthcheck, and build contexts relative to mern/ directory |
| mern/backend/db/connection.js | Updated MongoDB URI to use correct hostname 'mongo' with environment variable support |
| mern/frontend/Dockerfile | Removed redundant comments and reformatted |
| docker-compose.yaml | Removed outdated root-level compose file that had incorrect service names and paths |
| README.md | Added basic documentation for running the project locally and with Docker |
| .github/workflows/main.yaml | Removed broken workflow that referenced non-existent directory paths |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| **Note** - To run this project using `docker compose`, follow the below steps. | ||
|
|
||
| Switch to the `compose` branch to learn the |
There was a problem hiding this comment.
The README reference to a 'compose' branch and instructions to "Switch to the compose branch to learn" appears incomplete. The sentence on line 5 ends abruptly without completing the thought. Either complete this sentence or remove the reference if the compose branch doesn't exist or isn't relevant.
| Switch to the `compose` branch to learn the | |
| Switch to the `compose` branch to learn the following Docker-based setup: |
| ports: | ||
| - "27017:27017" |
There was a problem hiding this comment.
The MongoDB service is exposing port 27017 to the host without authentication configured. This creates a security risk as anyone with network access can connect to the database. Consider either removing the port mapping (services within the same Docker network can communicate without exposing ports to the host) or adding authentication via environment variables like MONGO_INITDB_ROOT_USERNAME and MONGO_INITDB_ROOT_PASSWORD.
| ports: | |
| - "27017:27017" |
| 2. Run the containers using `Docker Compose`. | ||
|
|
There was a problem hiding this comment.
The README mentions running the project using 'docker compose' but doesn't provide the actual commands. Since the docker-compose.yaml file is now located in the mern/ directory, users need to know to run 'cd mern && docker compose up' or 'docker compose -f mern/docker-compose.yaml up' from the root directory. Consider adding these instructions to make the documentation complete.
| 2. Run the containers using `Docker Compose`. | |
| 2. Run the containers using `Docker Compose`. From the project root you can either: | |
| - Change into the `mern` directory and run: | |
| ```bash | |
| cd mern | |
| docker compose up | |
| ``` | |
| - Or, from the project root, specify the compose file explicitly: | |
| ```bash | |
| docker compose -f mern/docker-compose.yaml up | |
| ``` |
| volumes: | ||
| - mongo-data:/data/db | ||
| healthcheck: | ||
| test: ["CMD", "mongosh", "--eval", "db.runCommand({ ping: 1 })"] |
There was a problem hiding this comment.
The healthcheck uses 'mongosh' which is only available in MongoDB 5.0+. While 'mongo:latest' should include this, it would be more robust to add '--quiet' flag to suppress verbose output and use 'mongosh --quiet --eval' to make the healthcheck cleaner. Alternatively, for better compatibility with older MongoDB versions, consider using 'echo "db.runCommand({ ping: 1 })" | mongosh --quiet' or the legacy 'mongo' command.
| test: ["CMD", "mongosh", "--eval", "db.runCommand({ ping: 1 })"] | |
| test: ["CMD", "mongosh", "--quiet", "--eval", "db.runCommand({ ping: 1 })"] |
| ports: | ||
| - "5173:5173" | ||
| networks: | ||
| - mern |
There was a problem hiding this comment.
The frontend environment is missing the backend API URL configuration. When running in Docker, the frontend container cannot reach the backend using 'http://localhost:5050' because each container has its own localhost. The old docker-compose.yaml had 'REACT_APP_API_URL: http://backend:5050' configured. You should either add this environment variable back or update the frontend code to use a configurable backend URL. For Vite, use 'VITE_API_URL' as the environment variable name.
| - mern | |
| - mern | |
| environment: | |
| - VITE_API_URL=http://backend:5050 |
| #### Start Server: | ||
|
|
||
| ``` | ||
| cd mern/server |
There was a problem hiding this comment.
The directory path 'mern/server' is incorrect. The actual backend directory is 'mern/backend'. This should be updated to 'cd mern/backend' to match the actual project structure.
| cd mern/server | |
| cd mern/backend |
| #### Start Client | ||
|
|
||
| ``` | ||
| cd mern/client |
There was a problem hiding this comment.
The directory path 'mern/client' is incorrect. The actual frontend directory is 'mern/frontend'. This should be updated to 'cd mern/frontend' to match the actual project structure.
| cd mern/client | |
| cd mern/frontend |
Short summary:
backend tried to resolve
mongodbwhile service name ismongo.Changes:
updated
MONGODB_URI, compose build contexts, added healthcheck. Fixes #23.