Skip to main content

Forwarding ports in your codespace

您可以转发代码空间中的端口以测试和调试应用程序。 您还可以管理端口协议并在组织内或公开共享端口。

代码空间可用于使用 GitHub Team 或 GitHub Enterprise Cloud 的组织。 更多信息请参阅“GitHub 的产品”。

About forwarded ports

Port forwarding gives you access to TCP ports running within your codespace. For example, if you're running a web application on a particular port in your codespace, you can forward that port. This allows you to access the application from the browser on your local machine for testing and debugging.

When an application running inside a codespace prints output to the terminal that contains a localhost URL, such as http://localhost:PORT or http://127.0.0.1:PORT, the port is automatically forwarded. If you're using Codespaces in the browser or in Visual Studio Code, the URL string in the terminal is converted into a link that you can click to view the web page on your local machine. By default, Codespaces forwards ports using HTTP.

Automatic port forwarding

You can also forward a port manually, label forwarded ports, share forwarded ports with members of your organization, share forwarded ports publicly, and add forwarded ports to the codespace configuration.

Note: 组织所有者可以限制公开或在组织内提供转发端口的能力。 更多信息请参阅“限制转发端口的可见性”。

Forwarding a port

You can manually forward a port that wasn't forwarded automatically.

  1. 在代码空间中的文本编辑器下,单击“Ports(端口)”。 端口选项卡

  2. Under the list of ports, click Add port.

    Add port button

  3. Type the port number or address, then press enter.

    Text box to type port button

Using HTTPS forwarding

By default, Codespaces forwards ports using HTTP but you can update any port to use HTTPS, as needed.

  1. 在代码空间中的文本编辑器下,单击“Ports(端口)”。 端口选项卡
  2. Right click the port you want to update, then hover over Change Port Protocol. Option to change port protocol
  3. Select the protocol needed for this port. The protocol that you select will be remembered for this port for the lifetime of the codespace.
  1. 在代码空间中的文本编辑器下,单击“Ports(端口)”。 端口选项卡

  2. Under the list of ports, click Add port.

    Add port button

  3. Type the port number or address, then press enter.

    Text box to type port button

要了解有关 GitHub CLI 的更多信息,请参阅“关于 GitHub CLI”。

To forward a port use the gh codespace ports forward subcommand. Replace codespace-port:local-port with the remote and local ports that you want to connect. After entering the command choose from the list of codespaces that's displayed.

gh codespace ports forward codespace-port:local-port

For more information about this command, see the GitHub CLI manual.

To see details of forwarded ports enter gh codespace ports and then choose a codespace.

Sharing a port

Note: You can only make a port private to an organization if your organization uses GitHub Team or GitHub Enterprise Cloud.

If you want to share a forwarded port with others, you can either make the port private to your organization or make the port public. After you make a port private to your organization, anyone in the organization with the port's URL can view the running application. After you make a port public, anyone who knows the URL and port number can view the running application without needing to authenticate.

Note: Your choice of port visibility options may be limited by a policy configured for your organization. For more information, see "Restricting the visibility of forwarded ports."

  1. 在代码空间中的文本编辑器下,单击“Ports(端口)”。 端口选项卡
  2. Right click the port that you want to share, select the "Port Visibility" menu, then click Private to Organization or Public. Option to select port visibility in right-click menu
  3. To the right of the local address for the port, click the copy icon. Copy icon for port URL
  4. Send the copied URL to the person you want to share the port with.
  1. 在代码空间中的文本编辑器下,单击“Ports(端口)”。 端口选项卡
  2. Right click the port that you want to share, select the "Port Visibility" menu, then click Private to Organization or Public. Option to make port public in right-click menu
  3. To the right of the local address for the port, click the copy icon. Copy icon for port URL
  4. Send the copied URL to the person you want to share the port with.

To change the visibility of a forwarded port, use the gh codespace ports visibility subcommand. 有三种可见性设置:

  • private - 仅对您可见。 这是转发端口时的默认设置。
  • org - 对拥有存储库的组织成员可见。
  • public - 对知道 URL 和端口号的任何人都可见。

Replace codespace-port with the forwarded port number. Replace setting with private, org, or public. After entering the command choose from the list of codespaces that's displayed.

gh codespace ports visibility codespace-port:setting

You can set the visibility for multiple ports with one command. For example:

gh codespace ports visibility 80:private 3000:public 3306:org

For more information about this command, see the GitHub CLI manual.

Labeling a port

You can label a port to make the port more easily identifiable in a list.

  1. 在代码空间中的文本编辑器下,单击“Ports(端口)”。 端口选项卡
  2. Hover over the port you want to label, then click the label icon. Label icon for port
  3. 键入端口的标签,然后按 enter。 输入端口标签的文本框

Adding a port to the codespace configuration

You can add a forwarded port to the Codespaces configuration for the repository, so the port will automatically be forwarded for all codespaces created from the repository. After you update the configuration, any previously created codespaces must be rebuilt for the change to apply. For more information, see "Configuring Codespaces for your project."

You can manually configure forwarded ports in a .devcontainer.json file using the forwardPorts property, or you can use the "Ports" panel in your codespace.

  1. 在代码空间中的文本编辑器下,单击“Ports(端口)”。 端口选项卡
  2. Right click the port you want to add to the codespace configuration, then click Set Label and Update devcontainer.json. Option to set label and add port to devcontainer.json in the right-click menu
  3. 键入端口的标签,然后按 enter。 输入端口标签的文本框