Skip to content

Commit 5a10cea

Browse files
authored
Merge pull request #189 from DefangLabs/jordan/rails
Improve rails sample
2 parents 4405235 + 3f1e625 commit 5a10cea

File tree

16 files changed

+192
-41
lines changed

16 files changed

+192
-41
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ARG RUBY_VERSION=3.3.4
2+
FROM ghcr.io/rails/devcontainer/images/ruby:$RUBY_VERSION
3+
4+
RUN apt-get update && apt-get install -y postgresql-client libpq-dev
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"build": {
3+
"dockerfile": "Dockerfile",
4+
"context": "."
5+
},
6+
"features": {
7+
"ghcr.io/defanglabs/devcontainer-feature/defang-cli:1.0.4": {},
8+
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
9+
},
10+
"runArgs": [
11+
"--env-file", ".env"
12+
],
13+
"appPort": 3000,
14+
"forwardPorts": [],
15+
"customizations": {
16+
"vscode": {
17+
"settings": {
18+
"remote.autoForwardPorts": false
19+
}
20+
}
21+
}
22+
}

samples/rails/.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
SECRET_KEY_BASE=example
2+
POSTGRES_USERNAME=postgres
3+
POSTGRES_HOST=db

samples/rails/.github/workflows/deploy.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ jobs:
1717
uses: actions/checkout@v4
1818

1919
- name: Deploy
20-
uses: DefangLabs/[email protected]
20+
uses: DefangLabs/[email protected]
21+
with:
22+
config-env-vars: POSTGRES_PASSWORD
23+
env:
24+
POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }}

samples/rails/README.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,41 @@
44

55
This template is a member list project developed using Ruby on Rails, offering a starting point to help you quickly build your team management system. We have prepared all the essential files for deployment. By spending less than 10 minutes setting up the environment, as detailed in the prerequisites, and executing the commands in our step-by-step guide, your website will be ready to go live to the world!
66

7-
## NOTE
8-
9-
This sample showcases how you could deploy a full-stack application with Defang in Ruby on Rails. However, it uses a SQLite database, which isn't production-ready and will be reset with every deployment. For production use cases you should use a managed database like RDS, Aiven, or others. If you stick to Rail's default SQLite database, your stored data will be lost on every deployment, and in some other cases. In the future, Defang will help you provision and connect to managed databases.
10-
117
## Essential Setup Files
128

139
1. A [Dockerfile](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/) to describe the basic image of your applications.
1410
2. A [docker-compose file](https://docs.defang.io/docs/concepts/compose) to define and run multi-container Docker applications.
1511
3. A [.dockerignore](https://docs.docker.com/build/building/context/#dockerignore-files) file to comply with the size limit (10MB).
1612

17-
## Prerequisite
13+
## Development Using [Dev Containers](https://containers.dev/)
14+
15+
1. Open the working directory with Visual Studio Code or any editor which supports Dev Containers.
16+
2. Click on the bottom left corner of the window where you see "Reopen in Container".
17+
3. Open up a shell in the VS Code terminal and run `docker compose -f compose.dev.yaml up`.
18+
19+
20+
## Configuration
21+
22+
For this sample, you will need to provide the following [configuration](https://docs.defang.io/docs/concepts/configuration). Note that if you are using the 1-click deploy option, you can set these values as secrets in your GitHub repository and the action will automatically deploy them for you.
23+
24+
### `POSTGRES_PASSWORD`
25+
This password will be used to initialize the PostgreSQL database and to connect to it.
1826

19-
1. Download [Defang CLI](https://github.com/DefangLabs/defang)
20-
2. If you are using [Defang BYOC](https://docs.defang.io/docs/concepts/defang-byoc), make sure you have properly [authenticated your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)
21-
3. Plus, make sure that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`.
2227

2328
## Deployment
2429

25-
1. Open the terminal and type `defang login`
26-
2. Type `defang compose up` in the CLI
27-
3. Now your application will be launched
30+
> [!NOTE]
31+
> Download [Defang CLI](https://github.com/DefangLabs/defang)
32+
33+
### Defang Playground
34+
35+
Deploy your application to the defang playground by opening up your terminal and typing `defang up`.
36+
37+
### BYOC (AWS)
38+
39+
If you want to deploy to your own cloud account, you can use Defang BYOC:
40+
41+
1. [Authenticate your AWS account](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html), and that you have properly set your environment variables like `AWS_PROFILE`, `AWS_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY`.
2842

2943
---
3044

samples/rails/app/.ruby-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ruby-3.0.0
1+
ruby-3.3.4

samples/rails/app/Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# syntax = docker/dockerfile:1
22

33
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
4-
ARG RUBY_VERSION=3.3.0
4+
ARG RUBY_VERSION=3.3.4
55
FROM ruby:$RUBY_VERSION-slim as base
66

77
# Rails app lives here
@@ -18,7 +18,7 @@ FROM base as build
1818

1919
# Install packages needed to build gems
2020
RUN apt-get update -qq && \
21-
apt-get install --no-install-recommends -y build-essential git libvips pkg-config
21+
apt-get install --no-install-recommends -y build-essential git libvips pkg-config libpq-dev
2222

2323
# Set bundler config to force ruby platform
2424
RUN bundle config set force_ruby_platform true
@@ -44,7 +44,7 @@ FROM base
4444

4545
# Install packages needed for deployment
4646
RUN apt-get update -qq && \
47-
apt-get install --no-install-recommends -y curl libsqlite3-0 libvips && \
47+
apt-get install --no-install-recommends -y curl libvips libpq-dev && \
4848
rm -rf /var/lib/apt/lists /var/cache/apt/archives
4949

5050
# Copy built artifacts: gems, application

samples/rails/app/Dockerfile.dev

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
2+
ARG RUBY_VERSION=3.3.4
3+
FROM ruby:$RUBY_VERSION-slim
4+
5+
# Rails app lives here
6+
WORKDIR /rails
7+
8+
# Set development environment
9+
ENV RAILS_ENV="development" \
10+
BUNDLE_PATH="/usr/local/bundle" \
11+
PORT=3000
12+
13+
# Install packages needed to build gems
14+
RUN apt-get update -qq && \
15+
apt-get install --no-install-recommends -y build-essential git libvips pkg-config libpq-dev postgresql-client
16+
17+
# Set bundler config to force ruby platform
18+
RUN bundle config set force_ruby_platform true
19+
20+
# Install application gems
21+
COPY Gemfile Gemfile.lock ./
22+
RUN bundle install && \
23+
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
24+
bundle exec bootsnap precompile --gemfile
25+
26+
# Copy application code
27+
COPY . .
28+
29+
RUN ls /rails
30+
31+
# Entrypoint prepares the database.
32+
ENTRYPOINT ["/rails/bin/docker-entrypoint"]
33+
34+
# Start the server by default, this can be overwritten at runtime
35+
EXPOSE 3000
36+
CMD ["./bin/rails", "server", "-b", "0.0.0.0"]

samples/rails/app/Gemfile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
source "https://rubygems.org"
22

3-
ruby "3.3.0"
3+
ruby "3.3.4"
44

55
# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
66
gem "rails", "~> 7.1.3", ">= 7.1.3.2"
77

88
# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
99
gem "sprockets-rails"
1010

11-
# Use sqlite3 as the database for Active Record
12-
gem "sqlite3", "~> 1.4"
11+
# Use postgres as the database for Active Record
12+
gem "pg", "~> 1.5"
1313

1414
# Use the Puma web server [https://github.com/puma/puma]
1515
gem "puma", ">= 5.0"
@@ -49,6 +49,7 @@ gem 'devise', '~> 4.9', '>= 4.9.4'
4949
group :development, :test do
5050
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
5151
gem "debug", platforms: %i[ mri mswin mswin64 mingw x64_mingw ]
52+
gem "dotenv-rails", "~> 3.1"
5253
end
5354

5455
group :development do

samples/rails/app/Gemfile.lock

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ GEM
106106
railties (>= 4.1.0)
107107
responders
108108
warden (~> 1.2.3)
109+
dotenv (3.1.2)
110+
dotenv-rails (3.1.2)
111+
dotenv (= 3.1.2)
112+
railties (>= 6.1)
109113
drb (2.2.1)
110114
erubi (1.12.0)
111115
globalid (1.2.1)
@@ -152,6 +156,7 @@ GEM
152156
mini_portile2 (~> 2.8.2)
153157
racc (~> 1.4)
154158
orm_adapter (0.5.0)
159+
pg (1.5.8)
155160
psych (5.1.2)
156161
stringio
157162
public_suffix (5.0.5)
@@ -218,8 +223,6 @@ GEM
218223
actionpack (>= 5.2)
219224
activesupport (>= 5.2)
220225
sprockets (>= 3.0.0)
221-
sqlite3 (1.7.3)
222-
mini_portile2 (~> 2.8.0)
223226
stimulus-rails (1.3.3)
224227
railties (>= 6.0.0)
225228
stringio (3.1.0)
@@ -255,20 +258,21 @@ DEPENDENCIES
255258
capybara
256259
debug
257260
devise (~> 4.9, >= 4.9.4)
261+
dotenv-rails (~> 3.1)
258262
importmap-rails
259263
jbuilder
264+
pg (~> 1.5)
260265
puma (>= 5.0)
261266
rails (~> 7.1.3, >= 7.1.3.2)
262267
selenium-webdriver
263268
sprockets-rails
264-
sqlite3 (~> 1.4)
265269
stimulus-rails
266270
turbo-rails
267271
tzinfo-data
268272
web-console
269273

270274
RUBY VERSION
271-
ruby 3.3.0p0
275+
ruby 3.3.4p94
272276

273277
BUNDLED WITH
274278
2.2.3

0 commit comments

Comments
 (0)