Skip to content

Commit 4744a8b

Browse files
committed
Update Codespaces configuration
1 parent faeced5 commit 4744a8b

9 files changed

Lines changed: 94 additions & 64 deletions

File tree

‎.devcontainer/README.md‎

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ This project is configured to work seamlessly in GitHub Codespaces or with VS Co
88
2. The devcontainer will automatically:
99
- Spin up a MongoDB instance (Atlas Local)
1010
- Install all dependencies
11-
- Seed sample data each time the Codespace starts
12-
- Start the Express API on port 5050
13-
- Start the React app on port 5173
14-
- Open the React app in the built-in Codespaces browser preview
11+
- Seed sample data
1512

1613
## Using with VS Code Dev Containers
1714

@@ -27,26 +24,25 @@ Once the devcontainer is running:
2724
|---------|-----|---------|
2825
| React App | http://localhost:5173 | Frontend (Vite dev server) |
2926
| Express API | http://localhost:5050 | Backend REST API |
30-
| MongoDB | mongodb://admin:mongodb@localhost:27017 | Database (no browser UI) |
27+
| MongoDB | mongodb://localhost:27017 | Database (no browser UI) |
3128
| MongoDB VS Code Extension | N/A | Query database directly in VS Code |
3229

3330
## Quick Start in Codespaces
3431

35-
Once the devcontainer is fully loaded, the app should already be running.
36-
37-
If you want to check logs:
32+
Once the devcontainer is fully loaded (wait for "Poststart" to complete):
3833

3934
```bash
40-
tail -f /tmp/mern-server.log
41-
tail -f /tmp/mern-client.log
42-
```
43-
44-
If you need to restart startup tasks manually:
35+
# Terminal 1: Start the Express server
36+
cd mern/server
37+
npm start
4538

46-
```bash
47-
bash .devcontainer/startup.sh
39+
# Terminal 2: Start the React dev server
40+
cd mern/client
41+
npm run dev
4842
```
4943

44+
Then open http://localhost:5173 in your browser.
45+
5046
## Database Credentials
5147

5248
- **Host**: `mongodb` (inside container) or `localhost:27017` (from host)
@@ -75,9 +71,8 @@ The devcontainer includes these extensions:
7571

7672
### MongoDB connection issues
7773
```bash
78-
# Check if mongodb is running
79-
cd mern/server
80-
node --input-type=module -e "import { MongoClient } from 'mongodb'; const c = new MongoClient(process.env.ATLAS_URI || 'mongodb://admin:mongodb@mongodb:27017/employees'); await c.connect(); console.log(await c.db('admin').command({ ping: 1 })); await c.close();"
74+
# Check if MongoDB is running
75+
mongosh -u admin -p mongodb --eval "db.adminCommand('ping')"
8176
```
8277

8378
### Port conflicts

‎.devcontainer/devcontainer.json‎

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
11
{
2-
"name": "MERN Stack with MongoDB",
2+
"name": "MERN Stack Example",
33
"dockerComposeFile": "docker-compose.yml",
44
"service": "app",
55
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
6-
7-
"features": {
8-
"ghcr.io/devcontainers/features/node:1": {
9-
"version": "22"
10-
}
11-
},
126

137
"customizations": {
148
"vscode": {
159
"extensions": [
16-
"mongodb.mongodb-vscode",
17-
"ms-vscode.vscode-typescript-next",
18-
"esbenp.prettier-vscode"
19-
],
20-
"settings": {
21-
"mongodb.connectionSslEnabled": false,
22-
"mongodb.defaultAuthenticationDatabase": "admin"
23-
}
10+
"mongodb.mongodb-vscode"
11+
]
2412
}
2513
},
2614

@@ -43,7 +31,14 @@
4331
"postCreateCommand": "cd mern/server && npm ci && cd ../client && npm ci",
4432
"postStartCommand": "bash .devcontainer/startup.sh",
4533

34+
// this will open 3 bash sessions, each one running the specified command
35+
"postAttachCommand": {
36+
"server": "cd mern/server && npm start",
37+
"client": "cd mern/client && npm run dev -- --host 0.0.0.0",
38+
"portSetup": "sleep 5 && .devcontainer/portSetup.sh"
39+
},
40+
4641
"remoteEnv": {
47-
"ATLAS_URI": "mongodb://admin:mongodb@mongodb:27017/employees"
42+
"ATLAS_URI": "mongodb://localhost:27017/"
4843
}
4944
}

‎.devcontainer/docker-compose.yml‎

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
1-
version: '3.8'
2-
31
services:
42
app:
53
image: mcr.microsoft.com/devcontainers/javascript-node:22-bookworm
64
volumes:
75
- ../..:/workspaces:cached
6+
7+
# Overrides default command so things don't shut down after the process ends.
88
command: sleep infinity
9+
10+
# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
911
network_mode: service:mongodb
10-
environment:
11-
- ATLAS_URI=mongodb://admin:mongodb@mongodb:27017/employees
12-
- PORT=5050
12+
13+
depends_on:
14+
- mongodb
1315

1416
mongodb:
15-
image: mongodb/mongodb-atlas-local:latest
17+
image: mongodb/mongodb-atlas-local:8.0.3-20250506T093411Z
1618
restart: unless-stopped
17-
ports:
18-
- 27017:27017
19-
environment:
20-
MONGODB_INITDB_ROOT_USERNAME: admin
21-
MONGODB_INITDB_ROOT_PASSWORD: mongodb
22-
MONGO_INITDB_DATABASE: employees
2319
volumes:
2420
- mongodb-data:/data/db
25-
- mongodb-config:/data/configdb
26-
healthcheck:
27-
test: mongosh --eval "db.adminCommand('ping')"
28-
interval: 10s
29-
timeout: 5s
30-
retries: 5
31-
21+
- ../..:/workspaces:cached
22+
ports:
23+
- 27017:27017
24+
3225
volumes:
33-
mongodb-data:
34-
mongodb-config:
26+
mongodb-data:

‎.devcontainer/portSetup.sh‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
if [ -z "$CODESPACE_NAME" ]; then
2+
echo "Not running in a codespace, exiting."
3+
exit
4+
fi
5+
6+
echo "Installing GH"
7+
8+
(type -p wget >/dev/null || (sudo apt update && sudo apt install wget -y)) \
9+
&& sudo mkdir -p -m 755 /etc/apt/keyrings \
10+
&& out=$(mktemp) && wget -nv -O$out https://cli.github.com/packages/githubcli-archive-keyring.gpg \
11+
&& cat $out | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \
12+
&& sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \
13+
&& sudo mkdir -p -m 755 /etc/apt/sources.list.d \
14+
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
15+
&& sudo apt update \
16+
&& sudo apt install gh -y
17+
18+
echo "Exposing ports"
19+
gh codespace ports visibility 5050:public 5173:public 27017:public -c $CODESPACE_NAME

‎.devcontainer/startup.sh‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ ROOT_DIR="/workspaces/${localWorkspaceFolderBasename:-$(basename "$PWD")}"
55
SERVER_DIR="$ROOT_DIR/mern/server"
66
CLIENT_DIR="$ROOT_DIR/mern/client"
77
CONFIG_FILE="$SERVER_DIR/config.env"
8-
ATLAS_URI_VALUE="${ATLAS_URI:-mongodb://admin:mongodb@mongodb:27017/employees}"
8+
ATLAS_URI_VALUE="${ATLAS_URI:-mongodb://localhost:27017/}"
99
PORT_VALUE="${PORT:-5050}"
1010

1111
ensure_config() {
12-
if [[ ! -f "$CONFIG_FILE" ]]; then
13-
cat > "$CONFIG_FILE" <<EOF
12+
cat > "$CONFIG_FILE" <<EOF
1413
ATLAS_URI=$ATLAS_URI_VALUE
1514
PORT=$PORT_VALUE
1615
EOF
17-
fi
16+
17+
echo "Wrote $CONFIG_FILE with ATLAS_URI and PORT."
1818
}
1919

2020
wait_for_mongodb() {
@@ -35,8 +35,28 @@ wait_for_mongodb() {
3535
seed_database() {
3636
(
3737
cd "$SERVER_DIR"
38-
node seed.js
38+
node --env-file=config.env seed.js > /tmp/mern-seed.log 2>&1
39+
)
40+
41+
echo "Seed completed (log: /tmp/mern-seed.log)."
42+
}
43+
44+
verify_seed_data() {
45+
local count
46+
47+
count=$(
48+
cd "$SERVER_DIR" &&
49+
node --env-file=config.env --input-type=module -e "import { MongoClient } from 'mongodb'; const c = new MongoClient(process.env.ATLAS_URI); await c.connect(); const n = await c.db('employees').collection('records').countDocuments(); console.log(n); await c.close();"
3950
)
51+
52+
echo "Seed verification: employees.records has $count documents."
53+
54+
if [[ "$count" -le 0 ]]; then
55+
echo "Seed verification failed: no documents found in employees.records."
56+
echo "Seed log tail:"
57+
tail -n 50 /tmp/mern-seed.log || true
58+
return 1
59+
fi
4060
}
4161

4262
start_server_if_needed() {
@@ -68,6 +88,7 @@ start_client_if_needed() {
6888
ensure_config
6989
wait_for_mongodb
7090
seed_database
91+
verify_seed_data
7192
start_server_if_needed
7293
start_client_if_needed
7394

‎.github/workflows/main.yaml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
mongodb:
2020
image: mongo:latest
2121
options: >-
22-
--health-cmd mongosh
22+
--health-cmd "mongosh --username admin --password mongodb --authenticationDatabase admin --eval \"db.adminCommand('ping')\""
2323
--health-interval 10s
2424
--health-timeout 5s
2525
--health-retries 5
@@ -51,7 +51,7 @@ jobs:
5151

5252
- name: Start server in the background
5353
env:
54-
ATLAS_URI: mongodb://admin:mongodb@localhost:27017/
54+
ATLAS_URI: mongodb://admin:mongodb@localhost:27017/employees?authSource=admin
5555
run: |
5656
echo "ATLAS_URI=$ATLAS_URI" > config.env
5757
npm start > /tmp/server.log 2>&1 &

‎.vscode/settings.json‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"mdb.presetConnections": [
3+
{
4+
"name": "Local Database",
5+
"connectionString": "mongodb://localhost:27017"
6+
}
7+
],
8+
"mdb.showOverviewPageAfterInstall": false
9+
}

‎README.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ Companion code for the [MERN Stack Tutorial](https://www.mongodb.com/languages/m
77
[![CI](https://github.com/mongodb-developer/mern-stack-example/actions/workflows/main.yaml/badge.svg)](https://github.com/mongodb-developer/mern-stack-example/actions/workflows/main.yaml)
88
[![License: Apache-2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE)
99
[![GitHub stars](https://img.shields.io/github/stars/mongodb-developer/mern-stack-example?style=social)](https://github.com/mongodb-developer/mern-stack-example/stargazers)
10-
[![GitHub issues](https://img.shields.io/github/issues/mongodb-developer/mern-stack-example)](https://github.com/mongodb-developer/mern-stack-example/issues)
1110

1211
## Project Overview
1312

‎mern/server/config.env.example‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# MongoDB Atlas Connection String
22
# Replace with your MongoDB Atlas connection string
33
# Format: mongodb+srv://<username>:<password>@<cluster>.mongodb.net/
4-
ATLAS_URI=mongodb://admin:mongodb@localhost:27017/
4+
ATLAS_URI=mongodb://localhost:27017/
55

66
# Express Server Port
77
PORT=5050

0 commit comments

Comments
 (0)