Add devcontainer for Codespaces - #2948
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.
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.
Is this necessary? I would have thought it happens already with bin/setup.
There was a problem hiding this comment.
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.
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.
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.
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.
There was a problem hiding this comment.
Cool. Can you clarify that in the comment, please? The current comment doesn't explain anything. Perhaps it should be:
# Print debug information in case something breaks
rvm list|
|
||
| # Install Ruby | ||
| rvm install 3.4.6 | ||
| rvm install 4.0.0 |
There was a problem hiding this comment.
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.
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.
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. |
bb06890 to
8585d9e
Compare
96de5e1 to
73325d8
Compare
73325d8 to
59a26cb
Compare
pablobm
left a comment
There was a problem hiding this comment.
I just tried this out and, after running bundle install, it complains that:
Your Ruby version is 3.4.7, but your Gemfile specified 4.0.0
I suspect that .devcontainer/codespaces/onCreateCommand.sh is not running. I'm also getting this when I try to sort the issue manually:
$ rvm get master
Downloading https://bitbucket.org/mpapis/rvm/raw/master/binscripts/rvm-installer.asc
Verifying /usr/local/rvm/archives/rvm-installer.asc
gpg: Signature made Tue Jul 23 21:59:45 2019 UTC
gpg: using RSA key 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
gpg: Can't check signature: No public key
GPG signature verification failed for '/usr/local/rvm/archives/rvm-installer' - 'https://bitbucket.org/mpapis/rvm/raw/master/binscripts/rvm-installer.asc'! Try to install GPG v2 and then fetch the public key:
gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
or if it fails:
command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
command curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -
In case of further problems with validation please refer to https://rvm.io/rvm/security
Same with the other commands. I don't see a config/database.yml for example.
Generally speaking: have you had much luck with Dev Containers? I provided a config for https://github.com/openstreetmap/openstreetmap-website and it gives a lot of trouble.
| @@ -0,0 +1,18 @@ | |||
| // For format details, see https://aka.ms/devcontainer.json. For config options, see the | |||
There was a problem hiding this comment.
| // For format details, see https://aka.ms/devcontainer.json. For config options, see the | |
| // For format details, see https://containers.dev/implementors/json_reference/. For config options, see the |
The domain aka.ms appears to be used by Microsoft as a link shortener (https://en.wikipedia.org/wiki/.ms#Usage_examples). It looks weird if you don't know this though, so I suggest using the real URL
| @@ -0,0 +1,33 @@ | |||
| # Workaround for install Ruby 4.0.0 with RVM | |||
| rvm list | |||
There was a problem hiding this comment.
Cool. Can you clarify that in the comment, please? The current comment doesn't explain anything. Perhaps it should be:
# Print debug information in case something breaks
rvm list

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?