├── .devcontainer ├── Dockerfile ├── devcontainer.json └── docker-compose.yml ├── README.md ├── database.yml.example └── template.rb /.devcontainer/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM ruby:3.3.0-slim-bullseye 2 | 3 | RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ 4 | && apt-get -y install --no-install-recommends \ 5 | apt-utils \ 6 | build-essential \ 7 | curl \ 8 | git \ 9 | imagemagick \ 10 | iproute2 \ 11 | libpq-dev \ 12 | openssh-client \ 13 | postgresql-client \ 14 | vim 15 | 16 | RUN gem install bundler 17 | RUN gem install foreman 18 | 19 | WORKDIR /workspace 20 | -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "MyApp", 3 | "dockerComposeFile": "docker-compose.yml", 4 | "service": "app", 5 | "workspaceFolder": "/workspace", 6 | "containerEnv": { 7 | "GITHUB_TOKEN": "${localEnv:GITHUB_TOKEN}", 8 | "GITHUB_USER": "${localEnv:GITHUB_USER}" 9 | }, 10 | "remoteEnv": { 11 | "PATH": "/workspace/bin:${containerEnv:PATH}" 12 | }, 13 | "postCreateCommand": "bundle install" 14 | } 15 | -------------------------------------------------------------------------------- /.devcontainer/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | app: 5 | build: 6 | context: .. 7 | dockerfile: .devcontainer/Dockerfile 8 | 9 | volumes: 10 | - ..:/workspace:cached 11 | - bundle_cache:/bundle 12 | 13 | ports: 14 | - '3000:3000' 15 | 16 | command: sleep infinity 17 | 18 | environment: 19 | REDIS_URL: redis://redis:6379/1 20 | DB_HOST: db 21 | 22 | depends_on: 23 | - db 24 | - redis 25 | 26 | redis: 27 | image: redis:latest 28 | ports: 29 | - '6379:6379' 30 | 31 | db: 32 | image: postgres:latest 33 | restart: unless-stopped 34 | volumes: 35 | - postgres-data:/var/lib/postgresql/data 36 | environment: 37 | POSTGRES_USER: postgres 38 | POSTGRES_DB: postgres 39 | POSTGRES_PASSWORD: postgres 40 | ports: 41 | - '5432:5432' 42 | 43 | mailgun: 44 | image: marlonb/mailcrab:latest 45 | logging: 46 | driver: 'none' 47 | ports: 48 | - 1025:1025 49 | - 8025:8025 50 | 51 | volumes: 52 | postgres-data: 53 | bundle_cache: 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ruby on Rails devcontainer based development 2 | 3 | This devcontainer contains Ruby 3.3.0, Postgres and Redis. You can update the Dockerfile and docker-compose.yml to add any other dependencies you may need. 4 | 5 | When you use devcontainers for your Rails projects, each project is self-contained and its dependencies don't interfere with any other projects. Also your developer machine stays clean. 6 | 7 | Another benefit is that the configuration is checked into the source code so other developers can clone down the repo and get to work without any additional setup required. 8 | 9 | Make sure you have the [Remote Development](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack) extension pack installed in VS Code. 10 | 11 | ### Existing Rails Application 12 | 13 | 1. Clone this repo 14 | 2. Copy the `.devcontainer` from this repo into the existing application 15 | 3. Modify the `.devcontainer/Dockerfile` with correct Ruby and Node versions you need (and any other dev dependencies) 16 | 4. Start vscode and it will prompt you to start the project in a dev container - yes! 17 | 5. Open the integrated terminal in vscode and type `ruby -v` and `rails -v` you are now in the devcontainer environment. 18 | 19 | ### Brand new Rails Application 20 | 21 | 1. Clone this repo 22 | 2. `cd` into it 23 | 3. Start vscode and it will prompt you to start the project in a dev container - yes! 24 | 4. Open the integrated terminal in vscode `gem install rails` 25 | 5. `rails new . -d postgresql -c tailwind -j esbuild` (or whatever) 26 | 6. `bin/dev` to start normal rails dev servers 27 | 28 | #### Database config sample 29 | 30 | - see the `database.yml.example` on how to set the `host` for the database. As the database is in docker you need to give the docker host name `db` or the ENV VAR `DB_HOST` 31 | -------------------------------------------------------------------------------- /database.yml.example: -------------------------------------------------------------------------------- 1 | default: &default 2 | adapter: postgresql 3 | encoding: unicode 4 | username: postgres 5 | password: postgres 6 | pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 7 | 8 | development: 9 | <<: *default 10 | host: <%= ENV.fetch("DB_HOST", "127.0.0.1") %> 11 | database: database_development 12 | 13 | test: &test 14 | <<: *default 15 | host: <%= ENV.fetch("DB_HOST", "127.0.0.1") %> 16 | database: database_test 17 | -------------------------------------------------------------------------------- /template.rb: -------------------------------------------------------------------------------- 1 | inside 'config' do 2 | gsub_file 'database.yml', 'database: workspace_development', "database: #{ENV['APP_NAME']}_development" 3 | gsub_file 'database.yml', 'database: workspace_test', "database: #{ENV['APP_NAME']}_test" 4 | gsub_file 'database.yml', 'database: workspace_production', "database: #{ENV['APP_NAME']}_production" 5 | 6 | database_config = File.read('database.yml') 7 | database_config.gsub!(/adapter: postgresql\n/, "adapter: postgresql\n host: <%= ENV.fetch('DB_HOST', 'localhost') %>\n password: postgres\n username: postgres\n") 8 | File.open('database.yml', 'w') { |file| file.puts database_config } 9 | end 10 | --------------------------------------------------------------------------------