DevContainers with Local Domains

I just wanted to add this hint to get a default devcontainer working with local domains.

I have used Traefik as a reverse proxy to automatically generate LetsEncrypt certs. Now I use OrbStack (on a Mac) which automatically creates a local domain for you.

However, neither options works with a default generated devcontainer. The problem is that the server does not automatically start. Both Traefik and OrbStack immediately try to connect to the container to create the connection. When you run “bin/dev” after it is too late for them to pick up the server and it can’t properly configure the local domain to forward to the server.

As a fix I added the following to devcontainer.json:

// Use 'postStartCommand' to run commands after the container is started.
  "postStartCommand": "bin/dev"

This will start the development processes automatically after creating and setting up the containers. Both Traefik reverse proxy and OrbStack sucessfully connect the local domain to the server.

If anyone has a better way to make this happen please let me know. I am not a devcontainer expert.

I actually ran into a different Rails 7.2 default devcontainer + Orbstack issue, which I’ll share the solution to here in the hope it saves others some grief.

In order for Orbstack to route domain traffic correctly, Puma needs to listen on 0.0.0.0, but by default bin/dev launches Puma on 127.0.0.1.

Changing puma.rb to replace

port ENV.fetch("PORT", 3000)

with

bind "tcp://0.0.0.0:#{ENV.fetch("PORT", 3000)}"

doesn’t work — no matter what bin/dev still starts Puma at 127.0.0.1.

Ultimately I learned that Puma ignores the bind config when it’s started via Foreman. Based on the solutions in that Github issue, the simplest working fix is simply to edit Procfile.dev and change the web: line to

web: unset PORT && bin/rails server -b 0.0.0.0

After which the Orbstack domain works as expected.