Skip to content

Port forwarding does not work correctly in Codespaces when using ghcr.io/rails/devcontainer/images/ruby, but works with mcr.microsoft.com/devcontainers/ruby #83

Open
@hamajyotan

Description

@hamajyotan

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:

  1. Create a Codespace with a .devcontainer/devcontainer.json like this:
{
  "dockerFile": "Dockerfile",
  "forwardPorts": [3000],
  "portsAttributes": {
    "3000": {
      "label": "Rails Dev Server",
      "onAutoForward": "notify",
      "visibility": "private"
    }
  }
}
  1. 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
  1. Start the Codespace and run a web server (e.g., bin/rails server -b 0.0.0.0 -p 3000)

  2. Try to access the forwarded port (3000) from the Codespaces UI.

  3. You’ll see a connection failure (timeout).

  4. Then, manually change the port visibility from "Private" → "Public" → "Private" via the UI.

  5. 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 curl http://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 the rails 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions