Skip to content
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

Add Devcontainer Support #1620

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ aliases:
paths:
- node_modules
- &ruby_node_browsers_docker_image
- image: cimg/ruby:3.3.3-browsers
- image: cimg/ruby:3.3.4-browsers
environment:
PGHOST: localhost
PGUSER: untitled_application
Expand Down
5 changes: 5 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# [Choice] Ruby version: 3, 3.3, 3.2, 3.1, 3.0, 2, 2.7, 2.6
ARG VARIANT="3.3.4"
FROM ghcr.io/rails/devcontainer/images/ruby:${VARIANT}

RUN apt-get update && apt-get install -y graphviz
50 changes: 50 additions & 0 deletions .devcontainer/compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
services:
rails:
build:
context: ..
dockerfile: .devcontainer/Dockerfile

volumes:
- ../..:/workspaces:cached

# Overrides default command so things don't shut down after the process ends.
command: sleep infinity

depends_on:
- postgres
- redis
# - memcached
- selenium

# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
# (Adding the "ports" property to this file will not forward from a Codespace.)
ports:
- 45678:45678

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd propose adding an env variable to improve the corepack prompt on the install process

environment:
      COREPACK_ENABLE_DOWNLOAD_PROMPT: 0 # prevent yarn install y/n prompt


selenium:
image: seleniarm/standalone-chromium
restart: unless-stopped

redis:
image: redis:latest
restart: unless-stopped
volumes:
- redis-data:/data

postgres:
image: postgres:latest
restart: unless-stopped
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres

# memcached:
# image: memcached:latest
# restart: unless-stopped
# command: ["-m", "1024"]

volumes:
postgres-data:
redis-data:
60 changes: 60 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// For format details, see https://aka.ms/devcontainer.json.
{
"name": "Untitled Application",
"dockerComposeFile": "compose.yaml",
"service": "rails",
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",

// Features to add to the dev container. More info: https://containers.dev/features.
"features": {
"ghcr.io/devcontainers/features/github-cli:1": {},
"ghcr.io/devcontainers/features/node:1": {},
"ghcr.io/rails/devcontainer/features/activestorage": {},
"ghcr.io/rails/devcontainer/features/postgres-client": {}
},

// https://code.visualstudio.com/remote/advancedcontainers/environment-variables
// https://containers.dev/implementors/json_reference/#variables-in-devcontainerjson
"containerEnv": {
"PGHOST": "postgres",
"PGUSER": "postgres",
"PGPASSWORD": "postgres",
"REDIS_URL": "redis://redis/0",
"CAPYBARA_SERVER_PORT": "45678",
"SELENIUM_HOST": "selenium",
// "MEMCACHE_SERVERS": "memcached:11211"
},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [
3000,
5432,
6379
],

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "corepack enable && yarn set version 4.2.2 && bin/setup-devcontainer",

// Configure tool-specific properties.
"customizations": {
"vscode": {
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"marcoroth.stimulus-lsp",
"bradlc.vscode-tailwindcss",
// "manuelpuyol.erb-linter",
// "setobiralo.erb-commenter",
// "streetsidesoftware.code-spell-checker",
// "GitHub.copilot",
// "GitHub.copilot-chat",
// "donjayamanne.githistory",
// "esbenp.prettier-vscode",
// "jemmyw.rails-fast-nav",
// "tmikoss.rails-latest-migration"
]
}
}

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.3.3
3.3.4
1 change: 1 addition & 0 deletions bin/configure-scripts/update_configs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
puts ""
puts "Replacing instances of \"Untitled Application\" with \"#{human}\" throughout the codebase.".green
replace_in_file("./.circleci/config.yml", "untitled_application", variable)
replace_in_file("./.devcontainer/devcontainer.json", "Untitled Application", human)
replace_in_file("./config/application.rb", "untitled_application", connected_name)
replace_in_file("./config/database.yml", "untitled_application", variable)
replace_in_file("./config/database.yml", "UNTITLED_APPLICATION", environment_variable)
Expand Down
44 changes: 44 additions & 0 deletions bin/setup-devcontainer
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env ruby

require 'bundler/inline'

# First we require a util file that will install a few utility gems
# and define some utility methods used by other scirpts.
require "#{__dir__}/setup-scripts/utils"



# path to your application root.
APP_ROOT = File.expand_path("..", __dir__)



FileUtils.chdir APP_ROOT do
# This script is a way to set up or update your development environment automatically.
# This script is idempotent, so that you can run it at any time and get an expectable outcome.
# Add necessary setup steps to individual scripts in `bin/setup-scripts` and then `require` them below.

# We need to check for the same version of ruby as bin/configure, so we
# reuse this one from the configure-scritps directory.
require "#{__dir__}/configure-scripts/check_ruby"
require "#{__dir__}/setup-scripts/bundle_install"

require "#{__dir__}/setup-scripts/check_node"
require "#{__dir__}/setup-scripts/check_yarn"
require "#{__dir__}/setup-scripts/yarn_install"

# Now we check that you have base-level system dependencies installed.
# Not needed for devcontainer, as we have a compose file with postgres and redis.
# require "#{__dir__}/setup-scripts/check_postgres"
# require "#{__dir__}/setup-scripts/check_redis"
# require "#{__dir__}/setup-scripts/check_icu"

require "#{__dir__}/setup-scripts/prepare_local_app"

require "#{__dir__}/setup-scripts/build_assets"

require "#{__dir__}/setup-scripts/check_overmind"

puts ""
puts "You're set! Now you can run bin/dev to boot your server and access your new application.".blue
end
2 changes: 1 addition & 1 deletion bin/setup-scripts/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
gemfile do
source "https://rubygems.org"
require "fileutils"
require "colorize"
gem "colorize"
require "json"
end

Expand Down
9 changes: 9 additions & 0 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ default: &default
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
<% if ENV["PGHOST"] %>
host: <%= ENV["PGHOST"] %>
<% end %>
<% if ENV["PGUSER"] %>
username: <%= ENV["PGUSER"] %>
<% end %>
<% if ENV["PGPASSWORD"] %>
password: <%= ENV["PGPASSWORD"] %>
<% end %>

development:
<<: *default
Expand Down