-
Notifications
You must be signed in to change notification settings - Fork 63.2k
First public PR: devcontainer JSON fix + minimal CI guardrails #40093
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
becb1e0
373c7f7
a2c07f1
64404d9
38433d4
921f8d9
108d1a2
5b0a086
a8e8daa
2fd1fbc
1e1c079
81b63dc
5aff31b
f7e8e86
8045d10
2900603
57290a5
caad731
4bb0bdb
fafce21
863c33b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
--- | ||
title: Securing port forwarding in dev containers | ||
shortTitle: Secure port forwarding | ||
intro: 'Learn how to configure secure port forwarding settings in your dev container configuration to control port visibility and automate security settings.' | ||
permissions: People with write permissions to a repository can create or edit the codespace configuration. | ||
versions: | ||
fpt: '*' | ||
ghec: '*' | ||
type: how_to | ||
topics: | ||
- Codespaces | ||
- Set up | ||
- Security | ||
--- | ||
|
||
## About port forwarding security in dev containers | ||
|
||
When you configure a dev container for {% data variables.product.prodname_github_codespaces %}, you can control how ports are forwarded and their visibility settings. This is important for security, especially when working with sensitive applications or in organizations with strict access policies. | ||
|
||
By default, {% data variables.product.prodname_github_codespaces %} forwards ports privately, meaning only you can access them. However, you can configure your dev container to automatically apply specific visibility settings when the codespace starts. | ||
|
||
## Configuring port forwarding with security in mind | ||
|
||
You can configure port forwarding in your dev container using several properties, each serving different security purposes. The key properties for secure port forwarding are `forwardPorts`, `portsAttributes`, and `postAttachCommand`. | ||
|
||
### Using forwardPorts with portsAttributes | ||
|
||
The most basic approach is to specify which ports should be forwarded and configure their attributes: | ||
|
||
```jsonc | ||
{ | ||
"name": "My Secure Dev Container", | ||
"image": "mcr.microsoft.com/devcontainers/base:bullseye", | ||
|
||
"forwardPorts": [3000, 8080, 8443], | ||
|
||
"portsAttributes": { | ||
"3000": { | ||
"label": "Application Server", | ||
"protocol": "http" | ||
}, | ||
"8080": { | ||
"label": "API Server", | ||
"protocol": "http" | ||
}, | ||
"8443": { | ||
"label": "Secure API", | ||
"protocol": "https" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
{% data reusables.codespaces.portsattributes-configuration %} | ||
|
||
### Automating port visibility settings | ||
|
||
You can automate port visibility settings using the `postAttachCommand` property. This ensures consistent security settings every time someone connects to the codespace: | ||
|
||
```jsonc | ||
{ | ||
"name": "My Secure Dev Container", | ||
"image": "mcr.microsoft.com/devcontainers/base:bullseye", | ||
|
||
"forwardPorts": [3000, 8080], | ||
|
||
"portsAttributes": { | ||
"3000": { | ||
"label": "Dev Server (Private)" | ||
}, | ||
"8080": { | ||
"label": "API Server (Team Only)" | ||
} | ||
}, | ||
|
||
"features": { | ||
"ghcr.io/devcontainers/features/github-cli:1": {} | ||
}, | ||
|
||
"postAttachCommand": "gh codespace ports visibility 3000:private 8080:org -c \"$CODESPACE_NAME\"" | ||
} | ||
``` | ||
|
||
{% data reusables.codespaces.port-visibility-automation %} | ||
|
||
## Security best practices | ||
|
||
{% data reusables.codespaces.port-security-best-practices %} | ||
|
||
## Working with organization policies | ||
|
||
Organization administrators can set policies that restrict which port visibility options are available. For more information, see [AUTOTITLE](/codespaces/managing-codespaces-for-your-organization/restricting-the-visibility-of-forwarded-ports). | ||
|
||
If your organization has port visibility restrictions in place, make sure your dev container automation commands comply with these policies. For example, if your organization disallows public port forwarding, don't use `public` in your `postAttachCommand`. | ||
|
||
## Example configurations | ||
|
||
The following examples demonstrate different security approaches for common development scenarios. | ||
|
||
### Development environment with private ports | ||
|
||
```jsonc | ||
{ | ||
"name": "Private Development Environment", | ||
"image": "mcr.microsoft.com/devcontainers/javascript-node:0-18", | ||
|
||
"forwardPorts": [3000, 3001], | ||
|
||
"portsAttributes": { | ||
"3000": { | ||
"label": "Web Server (Private)", | ||
"protocol": "http" | ||
}, | ||
"3001": { | ||
"label": "API Server (Private)", | ||
"protocol": "http" | ||
} | ||
}, | ||
|
||
"features": { | ||
"ghcr.io/devcontainers/features/github-cli:1": {} | ||
}, | ||
|
||
"postAttachCommand": "gh cs ports visibility 3000:private 3001:private -c \"$CODESPACE_NAME\"" | ||
} | ||
``` | ||
|
||
### Team collaboration with organization-only ports | ||
|
||
```jsonc | ||
{ | ||
"name": "Team Collaboration Environment", | ||
"image": "mcr.microsoft.com/devcontainers/python:3-bullseye", | ||
|
||
"forwardPorts": [5000, 8000], | ||
|
||
"portsAttributes": { | ||
"5000": { | ||
"label": "Flask App (Team)", | ||
"protocol": "http" | ||
}, | ||
"8000": { | ||
"label": "Django Admin (Team)", | ||
"protocol": "https" | ||
} | ||
}, | ||
|
||
"features": { | ||
"ghcr.io/devcontainers/features/github-cli:1": {} | ||
}, | ||
|
||
"postAttachCommand": "gh codespace ports visibility 5000:org 8000:org -c \"$CODESPACE_NAME\"" | ||
02ez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
``` | ||
|
||
## Further reading | ||
|
||
* [AUTOTITLE](/codespaces/developing-in-a-codespace/forwarding-ports-in-your-codespace) | ||
* [AUTOTITLE](/codespaces/managing-codespaces-for-your-organization/restricting-the-visibility-of-forwarded-ports) | ||
* {% data reusables.codespaces.more-info-devcontainer %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -147,9 +147,82 @@ With your dev container configuration added and a basic understanding of what ev | |
|
||
After the dev container is rebuilt, and your codespace becomes available again, the `postCreateCommand` will have been run, installing npm, and the "Code Spell Checker" extension will be available for use. | ||
|
||
## Step 4: Run your application | ||
## Step 4: Configure port forwarding and security | ||
|
||
In the previous section, you used the `postCreateCommand` to install a set of packages via the `npm install` command. With the dependencies now installed, you can run the application. | ||
Node.js applications typically run on port 3000. You can configure your dev container to automatically forward this port and set appropriate security settings. | ||
|
||
1. Add port forwarding configuration by uncommenting and modifying the `forwardPorts` property: | ||
|
||
```jsonc copy | ||
// Use 'forwardPorts' to make a list of ports inside the container available locally. | ||
"forwardPorts": [3000], | ||
``` | ||
|
||
1. Add port attributes to label your forwarded port: | ||
|
||
```jsonc copy | ||
"portsAttributes": { | ||
"3000": { | ||
"label": "Node.js App" | ||
} | ||
}, | ||
``` | ||
|
||
1. For enhanced security, you can add a `postAttachCommand` to automatically set port visibility. Add the {% data variables.product.prodname_cli %} feature first: | ||
|
||
```jsonc copy | ||
"features": { | ||
"ghcr.io/devcontainers-contrib/features/jshint:2": {}, | ||
"ghcr.io/devcontainers/features/github-cli:1": {} | ||
}, | ||
``` | ||
|
||
1. Then add the `postAttachCommand` to control port visibility: | ||
|
||
```jsonc copy | ||
// Automatically set port visibility when attaching to the codespace | ||
"postAttachCommand": "gh cs ports visibility 3000:private -c \"$CODESPACE_NAME\"" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The GitHub CLI command syntax should be 'gh codespace ports' instead of 'gh cs ports'. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback
02ez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
Your updated `devcontainer.json` should look similar to this: | ||
|
||
```jsonc | ||
{ | ||
"name": "Node.js", | ||
"image": "mcr.microsoft.com/devcontainers/javascript-node:0-18-bullseye", | ||
"features": { | ||
"ghcr.io/devcontainers-contrib/features/jshint:2": {}, | ||
"ghcr.io/devcontainers/features/github-cli:1": {} | ||
}, | ||
|
||
"forwardPorts": [3000], | ||
|
||
"portsAttributes": { | ||
"3000": { | ||
"label": "Node.js App" | ||
} | ||
}, | ||
|
||
"postCreateCommand": "npm install", | ||
|
||
"postAttachCommand": "gh cs ports visibility 3000:private -c \"$CODESPACE_NAME\"", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same GitHub CLI syntax correction needed - use 'gh codespace ports' instead of 'gh cs ports'. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
|
||
"customizations": { | ||
"vscode": { | ||
"extensions": [ | ||
"streetsidesoftware.code-spell-checker" | ||
] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
{% data reusables.codespaces.save-changes %} | ||
{% data reusables.codespaces.rebuild-command %} | ||
|
||
## Step 5: Run your application | ||
|
||
In the previous section, you configured port forwarding for your Node.js application. Now you can run the application and see it in action. | ||
|
||
1. In the Terminal of your codespace, enter `npm start`. | ||
|
||
|
@@ -159,12 +232,14 @@ In the previous section, you used the `postCreateCommand` to install a set of pa | |
|
||
 | ||
|
||
## Step 5: Commit your changes | ||
## Step 6: Commit your changes | ||
|
||
{% data reusables.codespaces.committing-link-to-procedure %} | ||
|
||
## Next steps | ||
|
||
You should now be able to add a custom dev container configuration to your own Node.js, JavaScript, or TypeScript project. | ||
|
||
For more advanced port security configurations, see [AUTOTITLE](/codespaces/setting-up-your-project-for-codespaces/adding-a-dev-container-configuration/securing-port-forwarding-in-dev-containers). | ||
|
||
{% data reusables.codespaces.next-steps-adding-devcontainer %} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,7 @@ The default development container, or "dev container," for {% data variables.pro | |
## Step 3: Modify your devcontainer.json file | ||
|
||
With your dev container configuration added and a basic understanding of what everything does, you can now make changes to customize your environment further. In this example, you'll add properties that will: | ||
* Configure port forwarding for the Flask application with security settings. | ||
* Install a package required by the application. | ||
* Install a {% data variables.product.prodname_vscode_shortname %} extension in this codespace. | ||
|
||
|
@@ -101,13 +102,46 @@ With your dev container configuration added and a basic understanding of what ev | |
// "features": {}, | ||
``` | ||
|
||
1. Uncomment the `forwardPorts` property and configure it for Flask (port 5000): | ||
|
||
```jsonc copy | ||
// Use 'forwardPorts' to make a list of ports inside the container available locally. | ||
"forwardPorts": [5000], | ||
``` | ||
|
||
1. Add port attributes and security configuration: | ||
|
||
```jsonc copy | ||
"portsAttributes": { | ||
"5000": { | ||
"label": "Flask Application" | ||
} | ||
}, | ||
``` | ||
|
||
1. Add the {% data variables.product.prodname_cli %} feature for port visibility automation: | ||
|
||
```jsonc copy | ||
"features": { | ||
"ghcr.io/devcontainers-contrib/features/coverage-py:2": {}, | ||
"ghcr.io/devcontainers/features/github-cli:1": {} | ||
}, | ||
``` | ||
|
||
1. Uncomment the `postCreateCommand` property. | ||
|
||
```jsonc copy | ||
// Use 'postCreateCommand' to run commands after the container is created. | ||
"postCreateCommand": "pip3 install --user -r requirements.txt", | ||
``` | ||
|
||
1. Add automated port visibility configuration: | ||
|
||
```jsonc copy | ||
// Automatically set port visibility when attaching to the codespace | ||
"postAttachCommand": "gh cs ports visibility 5000:private -c \"$CODESPACE_NAME\"" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The GitHub CLI command should use 'gh codespace ports' instead of 'gh cs ports' for the correct API syntax. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback
02ez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
|
||
{% data reusables.codespaces.add-extension-to-devcontainer %} | ||
|
||
```jsonc | ||
|
@@ -118,14 +152,24 @@ With your dev container configuration added and a basic understanding of what ev | |
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile | ||
"image": "mcr.microsoft.com/devcontainers/python:0-3.11-bullseye", | ||
"features": { | ||
"ghcr.io/devcontainers-contrib/features/coverage-py:2": {} | ||
"ghcr.io/devcontainers-contrib/features/coverage-py:2": {}, | ||
"ghcr.io/devcontainers/features/github-cli:1": {} | ||
}, | ||
|
||
// Use 'forwardPorts' to make a list of ports inside the container available locally. | ||
// "forwardPorts": [], | ||
"forwardPorts": [5000], | ||
|
||
"portsAttributes": { | ||
"5000": { | ||
"label": "Flask Application" | ||
} | ||
}, | ||
|
||
// Use 'postCreateCommand' to run commands after the container is created. | ||
"postCreateCommand": "pip3 install --user -r requirements.txt", | ||
|
||
// Automatically set port visibility when attaching to the codespace | ||
"postAttachCommand": "gh cs ports visibility 5000:private -c \"$CODESPACE_NAME\"", | ||
02ez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Configure tool-specific properties. | ||
"customizations": { | ||
|
@@ -147,7 +191,7 @@ With your dev container configuration added and a basic understanding of what ev | |
{% data reusables.codespaces.rebuild-command %} | ||
{% data reusables.codespaces.rebuild-reason %} | ||
|
||
After the dev container is rebuilt, and your codespace becomes available again, the `postCreateCommand` will have been run, installing the package listed in the `requirements.txt` file, and the "Code Spell Checker" extension will be available for use. | ||
After the dev container is rebuilt, and your codespace becomes available again, the `postCreateCommand` will have been run, installing the package listed in the `requirements.txt` file, the `postAttachCommand` will have configured the port visibility settings, and the "Code Spell Checker" extension will be available for use. | ||
|
||
## Step 4: Run your application | ||
|
||
|
@@ -169,4 +213,6 @@ In the previous section, you used the `postCreateCommand` to install a package f | |
|
||
You should now be able to add a custom dev container configuration to your own Python project. | ||
|
||
For more advanced port security configurations, see [AUTOTITLE](/codespaces/setting-up-your-project-for-codespaces/adding-a-dev-container-configuration/securing-port-forwarding-in-dev-containers). | ||
|
||
{% data reusables.codespaces.next-steps-adding-devcontainer %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The GitHub CLI command syntax should be 'gh codespace ports' instead of 'gh cs ports'.
Copilot uses AI. Check for mistakes.