Skip to content

Commit 8bbc8f1

Browse files
committed
Update Codespaces configuration
1 parent faeced5 commit 8bbc8f1

6 files changed

Lines changed: 69 additions & 54 deletions

File tree

‎.devcontainer/README.md‎

Lines changed: 12 additions & 17 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

@@ -32,21 +29,20 @@ Once the devcontainer is running:
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://admin:mongodb@localhost:27017/employees"
4843
}
4944
}

‎.devcontainer/docker-compose.yml‎

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
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+
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
13+
# (Adding the "ports" property to this file will not forward from a Codespace.)
14+
15+
depends_on:
16+
- mongodb
1317

1418
mongodb:
15-
image: mongodb/mongodb-atlas-local:latest
19+
image: mongodb/mongodb-atlas-local:8.0.3-20250506T093411Z
1620
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
2321
volumes:
2422
- 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
23+
- ../..:/workspaces:cached
24+
ports:
25+
- 27017:27017
26+
27+
# Add "forwardPorts": ["27017"] to **devcontainer.json** to forward MongoDB locally.
28+
# (Adding the "ports" property to this file will not forward from a Codespace.)
3129

3230
volumes:
33-
mongodb-data:
34-
mongodb-config:
31+
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ 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://admin:mongodb@localhost:27017/employees}"
99
PORT_VALUE="${PORT:-5050}"
1010

1111
ensure_config() {

‎.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+
}

0 commit comments

Comments
 (0)