Skip to content

Commit aff76a1

Browse files
committed
Update Codespaces configuration
1 parent faeced5 commit aff76a1

6 files changed

Lines changed: 54 additions & 27 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: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
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": {
@@ -43,7 +37,14 @@
4337
"postCreateCommand": "cd mern/server && npm ci && cd ../client && npm ci",
4438
"postStartCommand": "bash .devcontainer/startup.sh",
4539

40+
// this will open 3 bash sessions, each one running the specified command
41+
"postAttachCommand": {
42+
"server": "cd mern/server && npm start",
43+
"client": "cd mern/client && npm run dev -- --host 0.0.0.0",
44+
"portSetup": "sleep 5 && .devcontainer/portSetup.sh"
45+
},
46+
4647
"remoteEnv": {
47-
"ATLAS_URI": "mongodb://admin:mongodb@mongodb:27017/employees"
48+
"ATLAS_URI": "mongodb://admin:mongodb@localhost:27017/employees"
4849
}
4950
}

‎.devcontainer/docker-compose.yml‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ services:
77
- ../..:/workspaces:cached
88
command: sleep infinity
99
network_mode: service:mongodb
10+
depends_on:
11+
- mongodb
1012
environment:
11-
- ATLAS_URI=mongodb://admin:mongodb@mongodb:27017/employees
13+
- ATLAS_URI=mongodb://admin:mongodb@localhost:27017/employees
1214
- PORT=5050
1315

1416
mongodb:
15-
image: mongodb/mongodb-atlas-local:latest
17+
image: mongodb/mongodb-atlas-local:8.0.3-20250506T093411Z
1618
restart: unless-stopped
1719
ports:
1820
- 27017:27017
@@ -23,6 +25,7 @@ services:
2325
volumes:
2426
- mongodb-data:/data/db
2527
- mongodb-config:/data/configdb
28+
- ../..:/workspaces:cached
2629
healthcheck:
2730
test: mongosh --eval "db.adminCommand('ping')"
2831
interval: 10s

‎.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)