From 72d4e6a0d3830d3837c65351bdb133e22eb5c6ed Mon Sep 17 00:00:00 2001 From: Jordan Stephens Date: Fri, 6 Sep 2024 15:35:32 -0700 Subject: [PATCH 01/17] upgrade to ruby 3.3.4 --- samples/rails/app/.ruby-version | 2 +- samples/rails/app/Dockerfile | 2 +- samples/rails/app/Gemfile | 2 +- samples/rails/app/Gemfile.lock | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/samples/rails/app/.ruby-version b/samples/rails/app/.ruby-version index 85588beb..6d5369b9 100644 --- a/samples/rails/app/.ruby-version +++ b/samples/rails/app/.ruby-version @@ -1 +1 @@ -ruby-3.0.0 +ruby-3.3.4 diff --git a/samples/rails/app/Dockerfile b/samples/rails/app/Dockerfile index 92ade196..02ccecab 100644 --- a/samples/rails/app/Dockerfile +++ b/samples/rails/app/Dockerfile @@ -1,7 +1,7 @@ # syntax = docker/dockerfile:1 # Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile -ARG RUBY_VERSION=3.3.0 +ARG RUBY_VERSION=3.3.4 FROM ruby:$RUBY_VERSION-slim as base # Rails app lives here diff --git a/samples/rails/app/Gemfile b/samples/rails/app/Gemfile index 5a1c90aa..ae69be48 100644 --- a/samples/rails/app/Gemfile +++ b/samples/rails/app/Gemfile @@ -1,6 +1,6 @@ source "https://rubygems.org" -ruby "3.3.0" +ruby "3.3.4" # Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main" gem "rails", "~> 7.1.3", ">= 7.1.3.2" diff --git a/samples/rails/app/Gemfile.lock b/samples/rails/app/Gemfile.lock index c4179d0a..311d8881 100644 --- a/samples/rails/app/Gemfile.lock +++ b/samples/rails/app/Gemfile.lock @@ -257,18 +257,18 @@ DEPENDENCIES devise (~> 4.9, >= 4.9.4) importmap-rails jbuilder + pg (~> 1.5) puma (>= 5.0) rails (~> 7.1.3, >= 7.1.3.2) selenium-webdriver sprockets-rails - sqlite3 (~> 1.4) stimulus-rails turbo-rails tzinfo-data web-console RUBY VERSION - ruby 3.3.0p0 + ruby 3.3.4p94 BUNDLED WITH 2.2.3 From 098a9242e8da9dd0daf28c8728348311d4166ae4 Mon Sep 17 00:00:00 2001 From: Jordan Stephens Date: Fri, 6 Sep 2024 15:39:23 -0700 Subject: [PATCH 02/17] migrate to postgres --- samples/rails/app/Dockerfile | 2 +- samples/rails/app/Gemfile | 4 ++-- samples/rails/app/Gemfile.lock | 3 +-- samples/rails/app/config/database.yml | 18 ++++++++---------- samples/rails/app/db/schema.rb | 3 +++ samples/rails/compose.yaml | 22 +++++++++++++++++++++- 6 files changed, 36 insertions(+), 16 deletions(-) diff --git a/samples/rails/app/Dockerfile b/samples/rails/app/Dockerfile index 02ccecab..566630eb 100644 --- a/samples/rails/app/Dockerfile +++ b/samples/rails/app/Dockerfile @@ -44,7 +44,7 @@ FROM base # Install packages needed for deployment RUN apt-get update -qq && \ - apt-get install --no-install-recommends -y curl libsqlite3-0 libvips && \ + apt-get install --no-install-recommends -y curl libvips && \ rm -rf /var/lib/apt/lists /var/cache/apt/archives # Copy built artifacts: gems, application diff --git a/samples/rails/app/Gemfile b/samples/rails/app/Gemfile index ae69be48..3690f3d8 100644 --- a/samples/rails/app/Gemfile +++ b/samples/rails/app/Gemfile @@ -8,8 +8,8 @@ gem "rails", "~> 7.1.3", ">= 7.1.3.2" # The original asset pipeline for Rails [https://github.com/rails/sprockets-rails] gem "sprockets-rails" -# Use sqlite3 as the database for Active Record -gem "sqlite3", "~> 1.4" +# Use postgres as the database for Active Record +gem "pg", "~> 1.5" # Use the Puma web server [https://github.com/puma/puma] gem "puma", ">= 5.0" diff --git a/samples/rails/app/Gemfile.lock b/samples/rails/app/Gemfile.lock index 311d8881..95c3acdb 100644 --- a/samples/rails/app/Gemfile.lock +++ b/samples/rails/app/Gemfile.lock @@ -152,6 +152,7 @@ GEM mini_portile2 (~> 2.8.2) racc (~> 1.4) orm_adapter (0.5.0) + pg (1.5.8) psych (5.1.2) stringio public_suffix (5.0.5) @@ -218,8 +219,6 @@ GEM actionpack (>= 5.2) activesupport (>= 5.2) sprockets (>= 3.0.0) - sqlite3 (1.7.3) - mini_portile2 (~> 2.8.0) stimulus-rails (1.3.3) railties (>= 6.0.0) stringio (3.1.0) diff --git a/samples/rails/app/config/database.yml b/samples/rails/app/config/database.yml index 796466ba..185b68d7 100644 --- a/samples/rails/app/config/database.yml +++ b/samples/rails/app/config/database.yml @@ -1,25 +1,23 @@ -# SQLite. Versions 3.8.0 and up are supported. -# gem install sqlite3 -# -# Ensure the SQLite 3 gem is defined in your Gemfile -# gem "sqlite3" -# default: &default - adapter: sqlite3 + adapter: postgresql pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> + username: <%= ENV.fetch("DATABASE_USERNAME") %> + password: <%= ENV.fetch("DATABASE_PASSWORD") %> + host: <%= ENV.fetch("DATABASE_HOST") %> + port: <%= ENV.fetch("DATABASE_PORT") %> timeout: 5000 development: <<: *default - database: storage/development.sqlite3 + database: <%= ENV.fetch("DATABASE_NAME") %>_development # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default - database: storage/test.sqlite3 + database: <%= ENV.fetch("DATABASE_NAME") %>_test production: <<: *default - database: storage/production.sqlite3 + database: <%= ENV.fetch("DATABASE_NAME") %>_production diff --git a/samples/rails/app/db/schema.rb b/samples/rails/app/db/schema.rb index 0b9188e8..42981be4 100644 --- a/samples/rails/app/db/schema.rb +++ b/samples/rails/app/db/schema.rb @@ -11,6 +11,9 @@ # It's strongly recommended that you check this file into your version control system. ActiveRecord::Schema[7.1].define(version: 2024_04_18_210923) do + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + create_table "members", force: :cascade do |t| t.string "first_name" t.string "last_name" diff --git a/samples/rails/compose.yaml b/samples/rails/compose.yaml index 6b81dfb8..11077b2a 100644 --- a/samples/rails/compose.yaml +++ b/samples/rails/compose.yaml @@ -5,6 +5,9 @@ services: context: ./app dockerfile: Dockerfile environment: + DATABASE_USERNAME: rails_user + DATABASE_HOST: db + DATABASE_PASSWORD: secret SECRET_KEY_BASE: example # set your own key here. ports: - mode: ingress @@ -12,4 +15,21 @@ services: deploy: resources: reservations: - memory: 1GB \ No newline at end of file + memory: 1GB + db: + restart: unless-stopped + image: postgres:16 + environment: + POSTGRES_USER: rails_user + POSTGRES_PASSWORD: secret + ports: + - "5432:5432" + healthcheck: + test: ["CMD-SHELL", "pg_isready -U app_user"] + interval: 10s + timeout: 5s + retries: 5 + deploy: + resources: + reservations: + memory: 1GB From b097a32e392fc6111b10ff9f83ad725458a3e1e2 Mon Sep 17 00:00:00 2001 From: Jordan Stephens Date: Fri, 6 Sep 2024 15:48:13 -0700 Subject: [PATCH 03/17] use dotenv --- samples/rails/.env | 3 +++ samples/rails/app/Gemfile | 1 + samples/rails/app/Gemfile.lock | 5 +++++ samples/rails/app/config/database.yml | 8 ++++---- samples/rails/compose.yaml | 7 ++----- 5 files changed, 15 insertions(+), 9 deletions(-) create mode 100644 samples/rails/.env diff --git a/samples/rails/.env b/samples/rails/.env new file mode 100644 index 00000000..60a977c7 --- /dev/null +++ b/samples/rails/.env @@ -0,0 +1,3 @@ +SECRET_KEY_BASE=example +DATABASE_USER=rails_user +DATABASE_PASSWORD=secret diff --git a/samples/rails/app/Gemfile b/samples/rails/app/Gemfile index 3690f3d8..bfc667e5 100644 --- a/samples/rails/app/Gemfile +++ b/samples/rails/app/Gemfile @@ -49,6 +49,7 @@ gem 'devise', '~> 4.9', '>= 4.9.4' group :development, :test do # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem gem "debug", platforms: %i[ mri mswin mswin64 mingw x64_mingw ] + gem "dotenv-rails", "~> 3.1" end group :development do diff --git a/samples/rails/app/Gemfile.lock b/samples/rails/app/Gemfile.lock index 95c3acdb..18420c8e 100644 --- a/samples/rails/app/Gemfile.lock +++ b/samples/rails/app/Gemfile.lock @@ -106,6 +106,10 @@ GEM railties (>= 4.1.0) responders warden (~> 1.2.3) + dotenv (3.1.2) + dotenv-rails (3.1.2) + dotenv (= 3.1.2) + railties (>= 6.1) drb (2.2.1) erubi (1.12.0) globalid (1.2.1) @@ -254,6 +258,7 @@ DEPENDENCIES capybara debug devise (~> 4.9, >= 4.9.4) + dotenv-rails (~> 3.1) importmap-rails jbuilder pg (~> 1.5) diff --git a/samples/rails/app/config/database.yml b/samples/rails/app/config/database.yml index 185b68d7..3c772d85 100644 --- a/samples/rails/app/config/database.yml +++ b/samples/rails/app/config/database.yml @@ -4,20 +4,20 @@ default: &default username: <%= ENV.fetch("DATABASE_USERNAME") %> password: <%= ENV.fetch("DATABASE_PASSWORD") %> host: <%= ENV.fetch("DATABASE_HOST") %> - port: <%= ENV.fetch("DATABASE_PORT") %> + port: 5432 timeout: 5000 development: <<: *default - database: <%= ENV.fetch("DATABASE_NAME") %>_development + database: rails_development # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default - database: <%= ENV.fetch("DATABASE_NAME") %>_test + database: rails_test production: <<: *default - database: <%= ENV.fetch("DATABASE_NAME") %>_production + database: rails_production diff --git a/samples/rails/compose.yaml b/samples/rails/compose.yaml index 11077b2a..94792fea 100644 --- a/samples/rails/compose.yaml +++ b/samples/rails/compose.yaml @@ -4,11 +4,8 @@ services: build: context: ./app dockerfile: Dockerfile - environment: - DATABASE_USERNAME: rails_user - DATABASE_HOST: db - DATABASE_PASSWORD: secret - SECRET_KEY_BASE: example # set your own key here. + env_file: + - .env ports: - mode: ingress target: 3000 From 7612a8fcaa1ad89ad219abaea63cbdf312bd3793 Mon Sep 17 00:00:00 2001 From: Jordan Stephens Date: Fri, 6 Sep 2024 15:49:12 -0700 Subject: [PATCH 04/17] docker start: delete lingering server.pid --- samples/rails/app/bin/docker-entrypoint | 2 ++ 1 file changed, 2 insertions(+) diff --git a/samples/rails/app/bin/docker-entrypoint b/samples/rails/app/bin/docker-entrypoint index 67ef4931..3cfa13b6 100755 --- a/samples/rails/app/bin/docker-entrypoint +++ b/samples/rails/app/bin/docker-entrypoint @@ -5,4 +5,6 @@ if [ "${1}" == "./bin/rails" ] && [ "${2}" == "server" ]; then ./bin/rails db:prepare fi +rm -f tmp/pids/server.pid + exec "${@}" From 308b85d119a664b678d58f0cc88c93c4f8cf283c Mon Sep 17 00:00:00 2001 From: Jordan Stephens Date: Fri, 6 Sep 2024 15:51:00 -0700 Subject: [PATCH 05/17] configure dev containers --- samples/rails/app/Dockerfile.dev | 34 ++++++++++++++++++++++++++++++++ samples/rails/compose.dev.yaml | 26 ++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 samples/rails/app/Dockerfile.dev create mode 100644 samples/rails/compose.dev.yaml diff --git a/samples/rails/app/Dockerfile.dev b/samples/rails/app/Dockerfile.dev new file mode 100644 index 00000000..ab7fd112 --- /dev/null +++ b/samples/rails/app/Dockerfile.dev @@ -0,0 +1,34 @@ +# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile +ARG RUBY_VERSION=3.3.4 +FROM ruby:$RUBY_VERSION-slim + +# Rails app lives here +WORKDIR /rails + +# Set development environment +ENV RAILS_ENV="development" \ + BUNDLE_PATH="/usr/local/bundle" \ + PORT=3000 + +# Install packages needed to build gems +RUN apt-get update -qq && \ + apt-get install --no-install-recommends -y build-essential git libvips pkg-config libpq-dev postgresql-client + +# Set bundler config to force ruby platform +RUN bundle config set force_ruby_platform true + +# Install application gems +COPY Gemfile Gemfile.lock ./ +RUN bundle install && \ + rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \ + bundle exec bootsnap precompile --gemfile + +# Copy application code +COPY . . + +# Entrypoint prepares the database. +ENTRYPOINT ["/rails/bin/docker-entrypoint"] + +# Start the server by default, this can be overwritten at runtime +EXPOSE 3000 +CMD ["./bin/rails", "server", "-b", "0.0.0.0"] diff --git a/samples/rails/compose.dev.yaml b/samples/rails/compose.dev.yaml new file mode 100644 index 00000000..eb48960a --- /dev/null +++ b/samples/rails/compose.dev.yaml @@ -0,0 +1,26 @@ +services: + web: + extends: + file: compose.yaml + service: web + build: + context: ./app + dockerfile: Dockerfile.dev + env_file: + - .env + ports: + - "3000:3000" + volumes: + - .:/rails/ + + db: + extends: + file: compose.yaml + service: db + environment: + POSTGRES_USER: app_user + POSTGRES_PASSWORD: secret + volumes: + - db-data:/var/lib/postgresql/data +volumes: + db-data: From 2e71298bec84a6ec2074e30420b24bc998e5b0bd Mon Sep 17 00:00:00 2001 From: Jordan Stephens Date: Fri, 6 Sep 2024 15:51:49 -0700 Subject: [PATCH 06/17] set up devcontainer --- samples/rails/.devcontainer/Dockerfile | 4 ++++ samples/rails/.devcontainer/devcontainer.json | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 samples/rails/.devcontainer/Dockerfile create mode 100644 samples/rails/.devcontainer/devcontainer.json diff --git a/samples/rails/.devcontainer/Dockerfile b/samples/rails/.devcontainer/Dockerfile new file mode 100644 index 00000000..a7f39527 --- /dev/null +++ b/samples/rails/.devcontainer/Dockerfile @@ -0,0 +1,4 @@ +ARG RUBY_VERSION=3.3.4 +FROM ghcr.io/rails/devcontainer/images/ruby:$RUBY_VERSION + +RUN apt-get update && apt-get install -y postgresql-client libpq-dev diff --git a/samples/rails/.devcontainer/devcontainer.json b/samples/rails/.devcontainer/devcontainer.json new file mode 100644 index 00000000..7cc0d8d4 --- /dev/null +++ b/samples/rails/.devcontainer/devcontainer.json @@ -0,0 +1,22 @@ +{ + "build": { + "dockerfile": "Dockerfile", + "context": "." + }, + "features": { + "ghcr.io/defanglabs/devcontainer-feature/defang-cli:1.0.4": {}, + "ghcr.io/devcontainers/features/docker-in-docker:2": {} + }, + "runArgs": [ + "--env-file", ".env" + ], + "appPort": 3000, + "forwardPorts": [], + "customizations": { + "vscode": { + "settings": { + "remote.autoForwardPorts": false + } + } + } +} From 504c0ffc3a2caf0be76cbe0abe0979932a3d2659 Mon Sep 17 00:00:00 2001 From: Jordan Stephens Date: Fri, 6 Sep 2024 15:58:13 -0700 Subject: [PATCH 07/17] add byod comment --- samples/rails/compose.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/samples/rails/compose.yaml b/samples/rails/compose.yaml index 94792fea..d883c4cd 100644 --- a/samples/rails/compose.yaml +++ b/samples/rails/compose.yaml @@ -1,5 +1,7 @@ services: rails: + # uncomment to add your own domain + # domainname: example.com restart: unless-stopped build: context: ./app From a6b618c5651ac39f7469ab9bcb3e8cd1070e8b17 Mon Sep 17 00:00:00 2001 From: Jordan Stephens Date: Fri, 6 Sep 2024 16:19:48 -0700 Subject: [PATCH 08/17] readme --- samples/rails/README.md | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/samples/rails/README.md b/samples/rails/README.md index 16f0b5c3..ee63e31a 100644 --- a/samples/rails/README.md +++ b/samples/rails/README.md @@ -4,27 +4,33 @@ 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! -## NOTE - -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. - ## Essential Setup Files 1. A [Dockerfile](https://docs.docker.com/develop/develop-images/dockerfile_best-practices/) to describe the basic image of your applications. 2. A [docker-compose file](https://docs.defang.io/docs/concepts/compose) to define and run multi-container Docker applications. 3. A [.dockerignore](https://docs.docker.com/build/building/context/#dockerignore-files) file to comply with the size limit (10MB). -## Prerequisite +## Development Using [Dev Containers](https://containers.dev/) + +1. Open the working directory with Visual Studio Code or any editor which supports Dev Containers. +2. Click on the bottom left corner of the window where you see "Reopen in Container". +3. Open up a shell in the VS Code terminal and run `docker compose -f compose.dev.yaml up`. -1. Download [Defang CLI](https://github.com/DefangLabs/defang) -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) -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`. ## Deployment -1. Open the terminal and type `defang login` -2. Type `defang compose up` in the CLI -3. Now your application will be launched +> [!NOTE] +> Download [Defang CLI](https://github.com/DefangLabs/defang) + +### Defang Playground + +Deploy your application to the defang playground by opening up your terminal and typing `defang up`. + +### BYOC (AWS) + +If you want to deploy to your own cloud account, you can use Defang BYOC: + +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`. --- From fe0f6df657e6d7d426cc0f761dc173c7680dbcd8 Mon Sep 17 00:00:00 2001 From: Jordan Stephens Date: Mon, 9 Sep 2024 11:30:45 -0700 Subject: [PATCH 09/17] rails healthcheck --- samples/rails/compose.yaml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/samples/rails/compose.yaml b/samples/rails/compose.yaml index d883c4cd..cfc44c21 100644 --- a/samples/rails/compose.yaml +++ b/samples/rails/compose.yaml @@ -15,6 +15,11 @@ services: resources: reservations: memory: 1GB + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:3000"] + interval: 10s + timeout: 5s + retries: 5 db: restart: unless-stopped image: postgres:16 From 6bd9c8b564048e1c881d5daa3f97f5a211d8a81e Mon Sep 17 00:00:00 2001 From: Jordan Stephens Date: Mon, 9 Sep 2024 13:42:22 -0700 Subject: [PATCH 10/17] add libpq-dev to production image --- samples/rails/app/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/rails/app/Dockerfile b/samples/rails/app/Dockerfile index 566630eb..9a8d2259 100644 --- a/samples/rails/app/Dockerfile +++ b/samples/rails/app/Dockerfile @@ -18,7 +18,7 @@ FROM base as build # Install packages needed to build gems RUN apt-get update -qq && \ - apt-get install --no-install-recommends -y build-essential git libvips pkg-config + apt-get install --no-install-recommends -y build-essential git libvips pkg-config libpq-dev # Set bundler config to force ruby platform RUN bundle config set force_ruby_platform true From 783fddc9fa1647cf437156ce4c14f928d5ee88a6 Mon Sep 17 00:00:00 2001 From: Jordan Stephens Date: Mon, 9 Sep 2024 13:42:47 -0700 Subject: [PATCH 11/17] only specify host port for postgres container --- samples/rails/compose.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/samples/rails/compose.yaml b/samples/rails/compose.yaml index cfc44c21..5ebe5ab4 100644 --- a/samples/rails/compose.yaml +++ b/samples/rails/compose.yaml @@ -27,7 +27,8 @@ services: POSTGRES_USER: rails_user POSTGRES_PASSWORD: secret ports: - - "5432:5432" + - mode: host + target: 5432 healthcheck: test: ["CMD-SHELL", "pg_isready -U app_user"] interval: 10s From e343351d7ac68f0d6e17253f6c0f8776999f847f Mon Sep 17 00:00:00 2001 From: Jordan Stephens Date: Mon, 9 Sep 2024 18:01:56 -0700 Subject: [PATCH 12/17] fix db username env var --- samples/rails/.env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/rails/.env b/samples/rails/.env index 60a977c7..89787ec6 100644 --- a/samples/rails/.env +++ b/samples/rails/.env @@ -1,3 +1,3 @@ SECRET_KEY_BASE=example -DATABASE_USER=rails_user +DATABASE_USERNAME=rails_user DATABASE_PASSWORD=secret From d635b9ebaa26f799eee07c9f19cf0124c31263ab Mon Sep 17 00:00:00 2001 From: Jordan Stephens Date: Mon, 9 Sep 2024 18:07:41 -0700 Subject: [PATCH 13/17] the env does not have DB credentials at build time but we still want to be able to compile assets --- samples/rails/app/config/database.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/samples/rails/app/config/database.yml b/samples/rails/app/config/database.yml index 3c772d85..3aa272d1 100644 --- a/samples/rails/app/config/database.yml +++ b/samples/rails/app/config/database.yml @@ -1,9 +1,9 @@ default: &default adapter: postgresql pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> - username: <%= ENV.fetch("DATABASE_USERNAME") %> - password: <%= ENV.fetch("DATABASE_PASSWORD") %> - host: <%= ENV.fetch("DATABASE_HOST") %> + username: <%= ENV["DATABASE_USERNAME"] %> + password: <%= ENVs["DATABASE_PASSWORD"] %> + host: <%= ENV["DATABASE_HOST"] %> port: 5432 timeout: 5000 From 2d86793dc9012a1f416433eb9168260cab93b33d Mon Sep 17 00:00:00 2001 From: Jordan Stephens Date: Tue, 10 Sep 2024 11:11:27 -0700 Subject: [PATCH 14/17] fix prod db access --- samples/rails/.env | 3 ++- samples/rails/app/Dockerfile | 2 +- samples/rails/app/config/database.yml | 2 +- samples/rails/compose.dev.yaml | 3 --- samples/rails/compose.yaml | 4 ++-- 5 files changed, 6 insertions(+), 8 deletions(-) diff --git a/samples/rails/.env b/samples/rails/.env index 89787ec6..71ca5324 100644 --- a/samples/rails/.env +++ b/samples/rails/.env @@ -1,3 +1,4 @@ SECRET_KEY_BASE=example -DATABASE_USERNAME=rails_user +DATABASE_USERNAME=postgres DATABASE_PASSWORD=secret +DATABASE_HOST=db diff --git a/samples/rails/app/Dockerfile b/samples/rails/app/Dockerfile index 9a8d2259..182bd516 100644 --- a/samples/rails/app/Dockerfile +++ b/samples/rails/app/Dockerfile @@ -44,7 +44,7 @@ FROM base # Install packages needed for deployment RUN apt-get update -qq && \ - apt-get install --no-install-recommends -y curl libvips && \ + apt-get install --no-install-recommends -y curl libvips libpq-dev && \ rm -rf /var/lib/apt/lists /var/cache/apt/archives # Copy built artifacts: gems, application diff --git a/samples/rails/app/config/database.yml b/samples/rails/app/config/database.yml index 3aa272d1..0bd8c368 100644 --- a/samples/rails/app/config/database.yml +++ b/samples/rails/app/config/database.yml @@ -2,7 +2,7 @@ default: &default adapter: postgresql pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: <%= ENV["DATABASE_USERNAME"] %> - password: <%= ENVs["DATABASE_PASSWORD"] %> + password: <%= ENV["DATABASE_PASSWORD"] %> host: <%= ENV["DATABASE_HOST"] %> port: 5432 timeout: 5000 diff --git a/samples/rails/compose.dev.yaml b/samples/rails/compose.dev.yaml index eb48960a..a6024efc 100644 --- a/samples/rails/compose.dev.yaml +++ b/samples/rails/compose.dev.yaml @@ -17,9 +17,6 @@ services: extends: file: compose.yaml service: db - environment: - POSTGRES_USER: app_user - POSTGRES_PASSWORD: secret volumes: - db-data:/var/lib/postgresql/data volumes: diff --git a/samples/rails/compose.yaml b/samples/rails/compose.yaml index 5ebe5ab4..3935e7c6 100644 --- a/samples/rails/compose.yaml +++ b/samples/rails/compose.yaml @@ -24,13 +24,13 @@ services: restart: unless-stopped image: postgres:16 environment: - POSTGRES_USER: rails_user + POSTGRES_USER: postgres POSTGRES_PASSWORD: secret ports: - mode: host target: 5432 healthcheck: - test: ["CMD-SHELL", "pg_isready -U app_user"] + test: ["CMD-SHELL", "pg_isready -U postgres"] interval: 10s timeout: 5s retries: 5 From abedfd3bfd3456518bbe7174542faf96f50bad72 Mon Sep 17 00:00:00 2001 From: Raphael Titsworth-Morin Date: Thu, 19 Sep 2024 19:48:08 +0000 Subject: [PATCH 15/17] working local and cloud --- samples/rails/.env | 5 ++--- samples/rails/app/Dockerfile.dev | 2 ++ samples/rails/app/config/database.yml | 6 +++--- samples/rails/compose.dev.yaml | 8 +++++--- samples/rails/compose.yaml | 6 ++++-- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/samples/rails/.env b/samples/rails/.env index 71ca5324..f85288e6 100644 --- a/samples/rails/.env +++ b/samples/rails/.env @@ -1,4 +1,3 @@ SECRET_KEY_BASE=example -DATABASE_USERNAME=postgres -DATABASE_PASSWORD=secret -DATABASE_HOST=db +POSTGRES_USERNAME=postgres +POSTGRES_HOST=db diff --git a/samples/rails/app/Dockerfile.dev b/samples/rails/app/Dockerfile.dev index ab7fd112..8a9759e8 100644 --- a/samples/rails/app/Dockerfile.dev +++ b/samples/rails/app/Dockerfile.dev @@ -26,6 +26,8 @@ RUN bundle install && \ # Copy application code COPY . . +RUN ls /rails + # Entrypoint prepares the database. ENTRYPOINT ["/rails/bin/docker-entrypoint"] diff --git a/samples/rails/app/config/database.yml b/samples/rails/app/config/database.yml index 0bd8c368..863b8431 100644 --- a/samples/rails/app/config/database.yml +++ b/samples/rails/app/config/database.yml @@ -1,9 +1,9 @@ default: &default adapter: postgresql pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> - username: <%= ENV["DATABASE_USERNAME"] %> - password: <%= ENV["DATABASE_PASSWORD"] %> - host: <%= ENV["DATABASE_HOST"] %> + username: <%= ENV["POSTGRES_USERNAME"] %> + password: <%= ENV["POSTGRES_PASSWORD"] %> + host: <%= ENV["POSTGRES_HOST"] %> port: 5432 timeout: 5000 diff --git a/samples/rails/compose.dev.yaml b/samples/rails/compose.dev.yaml index a6024efc..371e4317 100644 --- a/samples/rails/compose.dev.yaml +++ b/samples/rails/compose.dev.yaml @@ -1,17 +1,19 @@ services: - web: + rails: extends: file: compose.yaml - service: web + service: rails build: context: ./app dockerfile: Dockerfile.dev env_file: - .env + environment: + - POSTGRES_PASSWORD=secret ports: - "3000:3000" volumes: - - .:/rails/ + - ./app:/rails/ db: extends: diff --git a/samples/rails/compose.yaml b/samples/rails/compose.yaml index 3935e7c6..6452117f 100644 --- a/samples/rails/compose.yaml +++ b/samples/rails/compose.yaml @@ -8,6 +8,8 @@ services: dockerfile: Dockerfile env_file: - .env + environment: + POSTGRES_PASSWORD: ports: - mode: ingress target: 3000 @@ -23,9 +25,9 @@ services: db: restart: unless-stopped image: postgres:16 + x-defang-postgres: true environment: - POSTGRES_USER: postgres - POSTGRES_PASSWORD: secret + POSTGRES_PASSWORD: ports: - mode: host target: 5432 From 994401ff7eaa3f61579f95999e279a7e1fbc6b22 Mon Sep 17 00:00:00 2001 From: Raphael Titsworth-Morin Date: Thu, 19 Sep 2024 19:50:01 +0000 Subject: [PATCH 16/17] upgrade github action and add config vals --- samples/rails/.github/workflows/deploy.yaml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/samples/rails/.github/workflows/deploy.yaml b/samples/rails/.github/workflows/deploy.yaml index 84d3dcf4..0f63ee68 100644 --- a/samples/rails/.github/workflows/deploy.yaml +++ b/samples/rails/.github/workflows/deploy.yaml @@ -17,4 +17,8 @@ jobs: uses: actions/checkout@v4 - name: Deploy - uses: DefangLabs/defang-github-action@v1.0.4 \ No newline at end of file + uses: DefangLabs/defang-github-action@v1.1.0 + with: + config-env-vars: POSTGRES_PASSWORD + env: + POSTGRES_PASSWORD: ${{ secrets.POSTGRES_PASSWORD }} \ No newline at end of file From b34905159084099a70cca6c506ccc561e9c13812 Mon Sep 17 00:00:00 2001 From: Raphael Titsworth-Morin Date: Thu, 19 Sep 2024 20:21:27 +0000 Subject: [PATCH 17/17] update config info --- samples/rails/README.md | 8 ++++++++ starter-sample/README.md | 20 ++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/samples/rails/README.md b/samples/rails/README.md index ee63e31a..b44ef8cd 100644 --- a/samples/rails/README.md +++ b/samples/rails/README.md @@ -17,6 +17,14 @@ This template is a member list project developed using Ruby on Rails, offering a 3. Open up a shell in the VS Code terminal and run `docker compose -f compose.dev.yaml up`. +## Configuration + +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. + +### `POSTGRES_PASSWORD` +This password will be used to initialize the PostgreSQL database and to connect to it. + + ## Deployment > [!NOTE] diff --git a/starter-sample/README.md b/starter-sample/README.md index 24df7e5c..e62054dd 100644 --- a/starter-sample/README.md +++ b/starter-sample/README.md @@ -6,7 +6,7 @@ This is a sample that shows the rough structure of an actual Defang sample. This ## Prerequisites -1. Download [Defang CLI](https://github.com/DefangLabs/defang) +1. [!NOTE] Download [Defang CLI](https://github.com/DefangLabs/defang) 2. (Optional) If you are using [Defang BYOC](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html) authenticated with your AWS account 3. (Optional for local development) [Docker CLI](https://docs.docker.com/engine/install/) @@ -27,13 +27,21 @@ For this sample, you will need to provide the following [configuration](https:// ### `API_KEY` #REMOVE_ME_AFTER_EDITING An explanation of what the env var (`API_KEY`) is, etc. +## Deployment -## Deploying +> [!NOTE] +> Download [Defang CLI](https://github.com/DefangLabs/defang) -1. Open the terminal and type `defang login` -2. Use the [`defang config`](https://docs.defang.io/docs/concepts/compose#configuration) command to setup environment variables. #REMOVE_ME_AFTER_EDITING -3. Type `defang compose up` in the CLI. -4. Your app will be running within a few minutes. +### Defang Playground + +Deploy your application to the defang playground by opening up your terminal and typing `defang up`. + +### BYOC (AWS) + +If you want to deploy to your own cloud account, you can use Defang BYOC: + +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`. +2. Run `defang up` in a terminal that has access to your AWS environment variables. ---