Can Codespaces be configured to forward ports to localhost instead of 127.0.0.1 #184624
Replies: 6 comments
-
|
No direct config to force Codespaces forward ports to http://localhost/ instead of http://127.0.0.1/ (they're basically the same thing — localhost resolves to 127.0.0.1 anyway). Private forwarded ports bind to 127.0.0.1:PORT (or localhost:PORT) on your local machine automatically. In browser Codespaces, it always uses the remote *.app.github.dev domain (secure, but not localhost). Install VS Code desktop if you don't have it. This gives the exact same dev experience as local devcontainer. No need to manually open links anymore! |
Beta Was this translation helpful? Give feedback.
-
|
@jhapendra-kandel I am using the desktop version of VS Code. Also, Microsoft doesn't just recommend the localhost string literal, they actually enforce it I am a frontend dev and my knowledge of networking and port forwarding is limited. I know that localhost is the hostname for 127.0.0.1. When I develop locally in devcontainers ports get forwarded to the localhost hostname but in Codespaces all ports get forwarded to 127.0.0.1. Why does Codespaces use 127.0.0.1 instead of localhost? |
Beta Was this translation helpful? Give feedback.
-
|
🕒 Discussion Activity Reminder 🕒 This Discussion has been labeled as dormant by an automated system for having no activity in the last 60 days. Please consider one the following actions: 1️⃣ Close as Out of Date: If the topic is no longer relevant, close the Discussion as 2️⃣ Provide More Information: Share additional details or context — or let the community know if you've found a solution on your own. 3️⃣ Mark a Reply as Answer: If your question has been answered by a reply, mark the most helpful reply as the solution. Note: This dormant notification will only apply to Discussions with the Thank you for helping bring this Discussion to a resolution! 💬 |
Beta Was this translation helpful? Give feedback.
-
|
Hey @andrewalderson, Right-click the port → Stop Forwarding Worst case, just edit the URL in your browser from 127.0.0.1 → localhost each time, it works since they're the same. |
Beta Was this translation helpful? Give feedback.
-
|
Yeah, this is a pretty common annoyance with Codespaces + auth providers that are strict about localhost. Short answer: there’s no built-in way to force Codespaces to use http://localhost instead of 127.0.0.1 for forwarded ports. It defaults to 127.0.0.1, and that’s what VS Code/toasts will use. That said, a few workarounds that usually help:
Instead of binding to 0.0.0.0 or letting it default, try: --host localhost or in code/config: host: "localhost" Some frameworks will then print/use localhost URLs instead of 127.0.0.1.
In your auth config (Entra External ID), explicitly set: http://localhost:{PORT}/callback and make sure your app uses that exact value for redirects, regardless of what Codespaces opens automatically.
Codespaces also gives you a URL like: https://-.app.github.dev If your IdP allows HTTPS redirects (most do), you can: register that URL instead of localhost Yeah, it’s a bit annoying, but the simplest flow right now is: ignore the auto-opened tab You can also copy/paste once and reuse the tab during dev. TL;DR Would definitely be nice if Codespaces let us configure this — feels like a small DX improvement that would save a lot of friction. |
Beta Was this translation helpful? Give feedback.
-
|
Yes! In your devcontainer config, you can set the forwardPorts option and use "portsAttributes" to control behavior, but for the localhost vs 127.0.0.1 distinction specifically, you can configure it in your devcontainer.json: |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
Select Topic Area
Question
Body
This is a (lazy) Developer Experience issue.
The Identity Provider I am using (Entra External Id) only allows insecure redirect urls to start with
http://localhost. Currently, automatically opening a browser when serving the app will open it athttp://127.0.0.1. This includes thehttp://localhost:{PORT}link printed in the console and the VS Code toast message to open a browser. This causes the authentication to fail. I can manually open thehttp://localhosturl but that's just getting to be annoying.I have been searching for a solution but currently myself and my trusty Copilot are stumped.
Is there a way to configure Codespaces to forward ports to
http://localhostinstead ofhttp://127.0.0.1? Basically I would like the same experience in Codespaces that I have when I develop locally in a devcontainer.Beta Was this translation helpful? Give feedback.
All reactions