Description
Describe the bug
When using GitHub Codespaces with a devcontainer based on ghcr.io/rails/devcontainer/images/ruby
, the forwardPorts
and portsAttributes
configurations in .devcontainer/devcontainer.json
do not behave as expected. Specifically, private port forwarding does not activate correctly upon container startup.
By contrast, when switching the base image to mcr.microsoft.com/devcontainers/ruby
, the port forwarding works correctly and immediately after the Codespace starts.
This suggests an issue specific to the Rails-provided devcontainer image.
To Reproduce
Steps to reproduce the behavior:
- Create a Codespace with a
.devcontainer/devcontainer.json
like this:
{
"dockerFile": "Dockerfile",
"forwardPorts": [3000],
"portsAttributes": {
"3000": {
"label": "Rails Dev Server",
"onAutoForward": "notify",
"visibility": "private"
}
}
}
- Use the following
Dockerfile
:
# RUBY_VERSION can be any compatible version
ARG RUBY_VERSION=3.4.3
FROM ghcr.io/rails/devcontainer/images/ruby:$RUBY_VERSION
-
Start the Codespace and run a web server (e.g.,
bin/rails server -b 0.0.0.0 -p 3000
) -
Try to access the forwarded port (3000) from the Codespaces UI.
-
You’ll see a connection failure (timeout).
-
Then, manually change the port visibility from "Private" → "Public" → "Private" via the UI.
-
Now, the port becomes accessible as expected.
Workaround
Changing the base image from:
FROM ghcr.io/rails/devcontainer/images/ruby:3.4.3
to:
FROM mcr.microsoft.com/devcontainers/ruby:3.4
resolves the issue. With this image, port forwarding works correctly at container startup.
Expected behavior
Port 3000 should be forwarded and accessible with the visibility set to private
, immediately after Codespace startup, without requiring manual toggling.
Additional context
-
This issue seems to occur only when using
ghcr.io/rails/devcontainer/images/ruby
. -
Adding a
postStartCommand
such as curlhttp://localhost:3000
does not help. -
Manually toggling the port visibility from Private → Public → Private always fixes the issue.
-
The GitHub CLI workaround using
gh codespace ports visibility
also works, but it’s not ideal for long-term use. -
This issue reproduces not only when manually creating a
.devcontainer
, but also when using therails new --devcontainer
command to automatically generate the.devcontainer
directory. In other words, even when following the official Rails workflow with the minimum setup, the port forwarding issue occurs.