Debugging Rails apps running inside Docker with VS Code
13 March 2022 | Updated on 02 April 2022
Since starting in my new developer position, I have been struggling to create a good debugging setup for our development environment. Now that I seem to have cracked it, I’m documenting below how to do it.
The development environment
The specific environments we work in work in generally use:
- Docker for development
- Dip for controlling these environments
- SSL in a development environment
And this combination has made it somewhat harder to get things running.
I’ve not tested this setup extensively and am fully expecting it to be somewhat brittle. For one, earlier (< 2.0) versions of Ruby apparently require different gems installed.
For this setup to work, we will need the
debase gems installed in the project’s
Next we need to tell Docker how to run the debugger for us to attach to. The key parts are included below, and you should note;
- “backend” is going to be dependent on your own setup.
- Everything after
commandline is the command you use to start your Rails server. Often this will simply be
bin/bundle exec rails s, but is a little more complex here due to running with SSL.
- The second of these entries, as should be apparent, will be used for debugging RSpec tests. For minitest/Cucumber/whatever you should simply be able to insert the appropriate command.
Update: On setting up debugging again using the procedure documented here I began to hit problems with the server hanging whenever I had breakpoints set. The environment variables I’m setting below limit concurrency in Puma and may or may not be necessary for your project.
Now we add information to our
dip.yml so we can launch the debugger. I’ve ommitted the irrelevant parts here, but this is organised as subcommands under
So now we’re able to launch the debugger, the final step is to be able to
connect to it. From Run and Debug in Visual Studio Code, click on the cog icon
to open your
launch.json file, and add the below information.
Putting it all together
So now, you can start up your debugger at the command line by running
rails debug (or
dip rspec debug), then in Run and Debug in
Visual Studio Code, choose Debug in Docker (or Debug RSpec in Docker) and
hit the play button. You should see the server start at the command line, and
can now go ahead and set breakpoints and inspect variables using Code’s
I will update this page in the future with any additional information I find.