Use this template for basic Ruby projects that will run in a Docker container, which ensures consistency and portability.
Projects based on this template will use Docker to quickly provide a consistent cross-platform Ruby development environment using the latest version of Ruby 3.
If you need a container for production use, consider the official Docker Ruby image as a starting point.
- Install Visual Studio Code.
- If Windows 10, install and configure WSL2. If Windows 11, search for a guide to do the same.
- If Mac, hit Shift-Command-P, type
Shell Command
, and select Shell Command: Install 'code' command in PATH option.- It's also essential to drag VS Code into the Applications folder.
- Install Docker Desktop.
- If Windows 10: enable the WSL2 integration for best performance.
- Install the VS Code Remote Containers extension.
- Copy the following to a project's root folder:
- .devcontainer folder
- TODOS:
-
In the devcontainer.json file in this folder, edit the
"name"
property to something that makes sense for the project. -
Add a
.env
file containing following variables (make sure that file is excluded via .gitignore):POSTGRES_USER=app_dev POSTGRES_PASSWORD=REPLACE_with_your_secure_password!
- Note: the user and password above will only be set when initially creating the
postgres-data
Docker volume. Rebuilding the container does not delete the volume, and so would not change the initial password if you simply change your.env
file. To change the password after initial creation, usepsql
or another admin tool, then update your.env
for next time. Or, delete the container and then thepostgres-data
Docker volume if it doesn't contain needed data, then rebuild the container. - The same username and password are used when
postCreateCommand.sh
creates a password file,~/.pgpass
, which enables automatic localpsql
logins. See PSQL for details.
- Note: the user and password above will only be set when initially creating the
-
Dockerfile
declaratively defines container build, including Ruby v3-bullseye, rvm, zsh, Gems, Pry configuration, and more.devcontainer.json
configures VS Code and coordinates Docker Compose.docker-compose.yml
configures:- Shared network
- Ruby dev container (service)
- PostgreSQL database container
.vscode
folder. If you need to make persistent changes to the contents of this folder, or to anything else in this repository, be sure to fork or copy this repo and modify there for your purposes. This folder will completely replace anything in{workspaceFolder}/.vscode
when the container is created or rebuilt..pryrc
: pry Gem configuration. Copied to home folder viapostCreateCommand.sh
.postCreateCommand.sh
: devcontainer.json'spostCreateCommand
executes this after the container is created.- Add custom commands to the
postCreateCommandCustom.sh
file, whichpostCreateCommand.sh
will execute when it's finished. It includes some common ones you can uncomment and customize.
- Add custom commands to the
- TODOS:
- src folder
- Create or clone code repos here.
- VS Code uses this folder the Workspace folder, which is the Explorer root.
- .devcontainer folder
- Navigate to the project folder that contains the
.devcontainer
folder in your (Linux) shell, then entercode .
. Visual Studio will prompt you to open the folder in a dev container, automatically building the image if it doesn't already exist and opening thesrc
folder.- Clone one or more repositories to the
src
folder. - Your project files will automatically sync between the container and your host file system, so the container can restart without data loss.
- Consider incorporating the container configuration into specific projects that need customized containers, in which case open those projects in a separate VS Code window that runs in its own container.
- Clone one or more repositories to the
- Install with a command like this:
rvm install ruby-x.x.x
(ruby-major.minor.patch) - Set the default Ruby version with a command like this:
rvm --default use ruby-x.x.x
- Revert to the system-default Ruby version (run Ruby programs without rvm):
rvm --default use system
This template is configured to support both pry/pry-byebug (REPL debugger) and VS Code debugging out of the box.
It's worth knowing how to debug using the REPL-based Pry, especially Pry-ByeBug, because the VS Code debugger may not do everything you need it to do, especially with complex or remote dev environments.
Hit F5 to begin debugging the focused open file using the default configuration.
VS Code offers a number of advantages over REPL-based debugging, such as:
- Streamlined and source code-integrated debugging experience: hover over source code variables to view values.
- Set temporary breakpoints and start debugging with a quick F5 press; no need to modify source code with
require 'pry'
andbinding.pry
statements. - View a list of variables and their values in a breakpoint's scope.
Usually, one interacts with a database server remotely using a client. PSQL is one of those clients. This container installs postgresql-client to enable that.
- Secure login is configured using the
POSTGRES_USER
andPOSTGRES_PASSWORD
values specified in your.env
file is configured during dev environment creation with the creation of a~/.pgpass
password file.- More information here.
- Dev container setup adds a custom command to simplify that connection to a single command:
dpsql
.
dpsql
: connect to Postgres viapsql
with.env
-specifed username and default host (db
) + port (5432
).dpsql --show-command
: show the command that would execute.dpsql [any psql commands]
: all arguments are forwarded topsql
.- The same is available for
createdb
(usedcreatedb
) anddropdb
(useddropdb
).
- The same is available for
After completing the pre-requisites above:
- Hit F1 (Show All Commands shortcut) | Type/select: >Remote-Containers: Add Development Container Configuration Files...
- Type Ruby and select it.
- There's also Ruby on Rails and others; select Show All Definitions..., then search again to view.
- Select a version.
- Customize the dev container, including VS Code settings, to your liking.