-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add devcontainer for Codespaces #2948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
837c945 to
f3e95bd
Compare
|
I started digging back into this last week, because I had a bit of time at last. I'd already played around with Codespaces on a generic Rails app, which is where I found it to be too slow, so I wanted to explore building a custom image. But what worked 9 months ago no longer does, so I ended up opening a support ticket for it on Monday. I've not heard back yet. But, prebuilds might solve a good chunk of the problem, it's good to hear it's down to 1-2 minutes! I'll come back to this as soon as I can, but it's going to be a bit slow because it's something that I'm keen to properly understand myself so it can be reused elsewhere. |
pablobm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been playing with devcontainers lately and I'm still very new to them. This review here is more me trying to understand them better and get your opinions.
At another project, I've been trying to get devcontainers set up too (see openstreetmap/openstreetmap-website#6424). I started from the template provided with Rails (bin/rails devcontainer) and iterated from there. The Rails one is based on the image ghcr.io/rails/devcontainer/images/ruby and uses a separate container for Selenium. I also set up Postgres in a separate container.
My intention was to follow The Rails Way as closely as possible. However it's not been smooth sailing and I have had to work around some issues. The setup you offer here appears to just work? Have you experimented with other ways of setting devcontainers in Rails? Do you have any opinions as to how to best do this?
|
|
||
| ./bin/setup | ||
| ./bin/setup | ||
| RAILS_ENV=test bundle exec rake db:setup dev:prime |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this necessary? I would have thought it happens already with bin/setup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With bin/setup alone, bin/rspec fails with Database not found: administrate-prototype_test..
However, it felt a bit verbose, so I refined it slightly.
| ./bin/setup | ||
| RAILS_ENV=test bundle exec rake db:setup dev:prime | ||
|
|
||
| echo "" >> .env && echo "DATABASE_URL=postgresql://postgres:@localhost" >> .env |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I just lost a couple of hours to this line 😅
This writes the .env file, not just within the container but also my real files. Later I was trying to work with Administrate normally (not with this PR) and the change had stayed in the .env file. The envvar DATABASE_URL has precedence over the setting in database.yml, and I didn't realise it was set. I couldn't understand why it was trying to sign into Postgres with the user postgres (as in this envvar) when I was using a different username in database.yml. I ended up having to debug ActiveRecord and finally found it.
So this line has to go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for taking your time on this. 🙇
After taking another look, it didn’t seem necessary after all, so I removed it.
Previously, if this wasn’t added here, I would sometimes run into an error where the database couldn’t be found when running certain commands (I don’t remember whether it was bundle exec or something under bin/*). Because of that, I’d been adding it out of habit.
It seems that the ENV settings in devcontainer.json are applied globally, and after testing again, everything appears to work fine without it.
|
@pablobm In my mind, this is primarily for reviewers: it enables a workflow where they can launch a per-PR environment directly on GitHub, verify the changes, and approve them without touching their local setup. For that reason, I’m not planning to build a development-optimized configuration. Instead, the goal is to use a generic image ( What do you think? |
|
I thought I'd just publish what I'd already started working on, even though it's not working: https://github.com/thoughtbot/codespaces-demo/tree/custom-config This is what I was doing at the start of last year. I came back to it towards the end of last year, where I was just trying to confirm everything I'd already tried was working with the plan of then building out something custom for Rails which is a lot faster and less confusing to get up and running with. However, this is no longer works (even though nothing is meaningfully different from what did work previously). I opened the support ticket around that, but I haven't heard anything in the last two weeks. |
6e73cfa to
93bbd88
Compare
|
It looks like a new version of Since things tend to break as soon as the version changes, I think it might be safer to pin the image using a specific version tag. |
93bbd88 to
cd3fafd
Compare
cd6a0f7 to
bb06890
Compare
| @@ -0,0 +1,33 @@ | |||
| # Workaround for install Ruby 4.0.0 with RVM | |||
| rvm list | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn’t required for the installation itself, but when it fails, the cause is often unclear, so I’m outputting version information and similar details to the build logs.
|
|
||
| # Install Ruby | ||
| rvm install 3.4.6 | ||
| rvm install 4.0.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some older PR branches still use older Ruby versions, so I install several versions to allow switching easily via RVM when needed.
| rvm list | ||
| curl -sSL https://rvm.io/mpapis.asc | gpg2 --import - | ||
| curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import - | ||
| rvm get master |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ruby 4.0 isn’t available in the stable RVM release yet, so I’m installing it from master.
Once it becomes stable, I’ll remove this workaround.
goosys
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated to support Ruby 4.0.
|
After initially waiting for weeks, and then a lot of back and forth with GitHub Support, I finally got my baseline configuration completed. I'm going to look at making a custom image next, but I thought it'd be good to share my parallel progress as it's going along. |


Regarding #2798, I'd like to propose an alternative approach.
Codespaces now supports using multiple devcontainer configurations, which means we can keep additional settings without causing conflicts. Based on this, I added an initialization script to the devcontainer to help users get started with this repository more easily.
If you select the “For Codespaces” configuration when launching a Codespace and wait for a moment, the environment will come up with both
bin/devandbin/rspecimmediately available.What do you think?