Investigate: Extend Mechanism for Port Forwarding
Status: Backlog​
Goal: Enable projects to define forwarded ports via the .devcontainer.extend/ mechanism without modifying the generic devcontainer.json.
Last Updated: 2026-01-16
Problem​
The devcontainer-toolbox uses a generic devcontainer.json that works for all projects. However, some projects need specific ports forwarded for web development (e.g., port 3000 for Docusaurus, port 8080 for other frameworks).
Currently, VS Code only auto-forwards ports when servers are started from its integrated terminal. When Claude (or other tools) start servers via docker exec, the ports aren't forwarded because there's no static forwardPorts configuration.
The Hugo devcontainer at sovereignsky-site solves this with:
"forwardPorts": [1313]
But adding specific ports to the generic devcontainer.json defeats its purpose.
Questions to Answer​
-
Can devcontainer.json be extended/merged?
- Does the devcontainer spec support multiple config files?
- Can we use a
.devcontainer/devcontainer.local.jsonthat merges with the main config?
-
Can forwardPorts be set dynamically?
- Can postCreateCommand or postStartCommand configure port forwarding?
- Is there a VS Code API or CLI to add forwarded ports at runtime?
-
What's the best UX for users?
- A
ports.conffile in.devcontainer.extend/? - A JSON file that gets merged?
- Environment variables?
- A
Research Areas​
Devcontainer Spec​
- Check devcontainer.json reference for merge/extend capabilities
- Look for
localEnvor variable substitution options for forwardPorts - Check if multiple devcontainer.json files can coexist
VS Code Port Forwarding​
- How does VS Code detect ports to forward?
- Can we programmatically trigger port forwarding?
- Is there a
devcontainer.jsonoverride mechanism?
Alternative Approaches​
- Docker Compose with port mappings
- Using
runArgswith-pflag dynamically - Post-start script that uses VS Code CLI
Potential Solutions​
Option A: ports.conf file​
# .devcontainer.extend/ports.conf
3000
8080
1313
Pros: Simple, consistent with existing extend pattern Cons: Needs build-time processing to inject into devcontainer.json
Option B: Local override file​
// .devcontainer.extend/devcontainer.extend.json
{
"forwardPorts": [3000, 8080]
}
Pros: Standard JSON, easy to understand Cons: Needs merge logic, may not be supported by devcontainer spec
Option C: Dynamic port forwarding via script​
# postStartCommand adds ports dynamically
# Needs research on how to trigger VS Code port forwarding
Pros: No config file changes needed Cons: May not be possible, depends on VS Code internals
Option D: Docker run args​
Modify the container startup to include -p 3000:3000 via runArgs, but this requires knowing ports at build time.
Success Criteria​
- Users can specify ports in
.devcontainer.extend/folder - Ports are forwarded automatically when container starts
- Works with
docker exec(not just VS Code terminal) - No changes to generic
devcontainer.jsonrequired - Simple, documented pattern for users
Next Steps​
- Research devcontainer spec for merge/extend capabilities
- Test if
runArgswith-pcan be dynamically configured - Check VS Code extension API for port forwarding
- Prototype simplest viable solution
- Document findings and recommend approach
References​
- Devcontainer JSON Reference
- VS Code Port Forwarding Docs
- Devcontainer Features
- Hugo devcontainer example:
/Users/terje.christensen/learn/projects-2025/sovereignsky-site/.devcontainer/devcontainer.json