|
| 1 | +--- |
| 2 | +title: Securing port forwarding in dev containers |
| 3 | +shortTitle: Secure port forwarding |
| 4 | +intro: 'Learn how to configure secure port forwarding settings in your dev container configuration to control port visibility and automate security settings.' |
| 5 | +permissions: People with write permissions to a repository can create or edit the codespace configuration. |
| 6 | +versions: |
| 7 | + fpt: '*' |
| 8 | + ghec: '*' |
| 9 | +type: how_to |
| 10 | +topics: |
| 11 | + - Codespaces |
| 12 | + - Set up |
| 13 | + - Security |
| 14 | +--- |
| 15 | + |
| 16 | +## About port forwarding security in dev containers |
| 17 | + |
| 18 | +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. |
| 19 | + |
| 20 | +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. |
| 21 | + |
| 22 | +## Configuring port forwarding with security in mind |
| 23 | + |
| 24 | +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`. |
| 25 | + |
| 26 | +### Using forwardPorts with portsAttributes |
| 27 | + |
| 28 | +The most basic approach is to specify which ports should be forwarded and configure their attributes: |
| 29 | + |
| 30 | +```jsonc |
| 31 | +{ |
| 32 | + "name": "My Secure Dev Container", |
| 33 | + "image": "mcr.microsoft.com/devcontainers/base:bullseye", |
| 34 | + |
| 35 | + "forwardPorts": [3000, 8080, 8443], |
| 36 | + |
| 37 | + "portsAttributes": { |
| 38 | + "3000": { |
| 39 | + "label": "Application Server", |
| 40 | + "protocol": "http" |
| 41 | + }, |
| 42 | + "8080": { |
| 43 | + "label": "API Server", |
| 44 | + "protocol": "http" |
| 45 | + }, |
| 46 | + "8443": { |
| 47 | + "label": "Secure API", |
| 48 | + "protocol": "https" |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +{% data reusables.codespaces.portsattributes-configuration %} |
| 55 | + |
| 56 | +### Automating port visibility settings |
| 57 | + |
| 58 | +You can automate port visibility settings using the `postAttachCommand` property. This ensures consistent security settings every time someone connects to the codespace: |
| 59 | + |
| 60 | +```jsonc |
| 61 | +{ |
| 62 | + "name": "My Secure Dev Container", |
| 63 | + "image": "mcr.microsoft.com/devcontainers/base:bullseye", |
| 64 | + |
| 65 | + "forwardPorts": [3000, 8080], |
| 66 | + |
| 67 | + "portsAttributes": { |
| 68 | + "3000": { |
| 69 | + "label": "Dev Server (Private)" |
| 70 | + }, |
| 71 | + "8080": { |
| 72 | + "label": "API Server (Team Only)" |
| 73 | + } |
| 74 | + }, |
| 75 | + |
| 76 | + "features": { |
| 77 | + "ghcr.io/devcontainers/features/github-cli:1": {} |
| 78 | + }, |
| 79 | + |
| 80 | + "postAttachCommand": "gh cs ports visibility 3000:private 8080:org -c \"$CODESPACE_NAME\"" |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +{% data reusables.codespaces.port-visibility-automation %} |
| 85 | + |
| 86 | +## Security best practices |
| 87 | + |
| 88 | +{% data reusables.codespaces.port-security-best-practices %} |
| 89 | + |
| 90 | +## Working with organization policies |
| 91 | + |
| 92 | +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). |
| 93 | + |
| 94 | +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`. |
| 95 | + |
| 96 | +## Example configurations |
| 97 | + |
| 98 | +The following examples demonstrate different security approaches for common development scenarios. |
| 99 | + |
| 100 | +### Development environment with private ports |
| 101 | + |
| 102 | +```jsonc |
| 103 | +{ |
| 104 | + "name": "Private Development Environment", |
| 105 | + "image": "mcr.microsoft.com/devcontainers/javascript-node:0-18", |
| 106 | + |
| 107 | + "forwardPorts": [3000, 3001], |
| 108 | + |
| 109 | + "portsAttributes": { |
| 110 | + "3000": { |
| 111 | + "label": "Web Server (Private)", |
| 112 | + "protocol": "http" |
| 113 | + }, |
| 114 | + "3001": { |
| 115 | + "label": "API Server (Private)", |
| 116 | + "protocol": "http" |
| 117 | + } |
| 118 | + }, |
| 119 | + |
| 120 | + "features": { |
| 121 | + "ghcr.io/devcontainers/features/github-cli:1": {} |
| 122 | + }, |
| 123 | + |
| 124 | + "postAttachCommand": "gh cs ports visibility 3000:private 3001:private -c \"$CODESPACE_NAME\"" |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +### Team collaboration with organization-only ports |
| 129 | + |
| 130 | +```jsonc |
| 131 | +{ |
| 132 | + "name": "Team Collaboration Environment", |
| 133 | + "image": "mcr.microsoft.com/devcontainers/python:3-bullseye", |
| 134 | + |
| 135 | + "forwardPorts": [5000, 8000], |
| 136 | + |
| 137 | + "portsAttributes": { |
| 138 | + "5000": { |
| 139 | + "label": "Flask App (Team)", |
| 140 | + "protocol": "http" |
| 141 | + }, |
| 142 | + "8000": { |
| 143 | + "label": "Django Admin (Team)", |
| 144 | + "protocol": "https" |
| 145 | + } |
| 146 | + }, |
| 147 | + |
| 148 | + "features": { |
| 149 | + "ghcr.io/devcontainers/features/github-cli:1": {} |
| 150 | + }, |
| 151 | + |
| 152 | + "postAttachCommand": "gh cs ports visibility 5000:org 8000:org -c \"$CODESPACE_NAME\"" |
| 153 | +} |
| 154 | +``` |
| 155 | + |
| 156 | +## Further reading |
| 157 | + |
| 158 | +* [AUTOTITLE](/codespaces/developing-in-a-codespace/forwarding-ports-in-your-codespace) |
| 159 | +* [AUTOTITLE](/codespaces/managing-codespaces-for-your-organization/restricting-the-visibility-of-forwarded-ports) |
| 160 | +* {% data reusables.codespaces.more-info-devcontainer %} |
0 commit comments