Skip to content

Instantly share code, notes, and snippets.

@juanca
Last active May 20, 2024 22:41
Show Gist options
  • Save juanca/b627f87a98229f36b5e456d59299214f to your computer and use it in GitHub Desktop.
Save juanca/b627f87a98229f36b5e456d59299214f to your computer and use it in GitHub Desktop.
[GUIDE] Starting a new Ruby on Rails project in docker

Bootstrap project

  1. Initialize your project folder with git: git init and create .gitignore

  2. Create your project files with a ruby image:

    docker run -it -v .:/rails/<app_name> ruby:3.2.2 /bin/bash
    cd /rails/<app_name>
    gem install rails
    rails new .
    exit
    
  3. Configure the rails web console to work in docker in development.rb:

    # Allow IRB in browser for a docker environment
    config.web_console.permissions = '0.0.0.0/0'
    

Simple environment

  1. Create a docker-compose.yml:

    version: "3"
    services:
       app:
         image: ruby:3.2.2
         command: ./bin/rails server -b 0.0.0.0
       ports:
         - "3000:3000"
       volumes:
         - .:/rails/<app_name>
         - bundle:/usr/local/bundle
       working_dir: /rails/<app_name>
    volumes:
      bundle:
    

Advanced environment

  1. Copy the Dockerfile into a new Dockerfile.dev

    1. Remove any environment variables
    2. Modify bundle install step to only bundle install
    3. Remove precompilation steps
    4. Test it with docker build Dockerfile.dev .
  2. Create a docker-compose.yml:

    version: "3"
    services:
       app:
         build:
           context: .
           dockerfile: Dockerfile.dev
         command: ./bin/rails server -b 0.0.0.0
       ports:
         - "3000:3000"
       volumes:
         - .:/rails
    

Postgres

  1. Setup docker service:

    version: "3"
    services:
      app:
        # ...
        environment:
          DATABASE_HOST: db
          DATABASE_PASSWORD: password
        # ...
      db:
        image: postgres:15
        environment:
          POSTGRES_PASSWORD: password
        volumes:
          - db:/var/lib/postgresql/data
    volumes:
      db:
    
  2. Run db docker service: docker compose up db

  3. docker compose run app bin/rails db:system:change --to=postgresql

  4. docker compose run app bin/setup

  5. Modify ./config/database.yml:

    default: &default
      adapter: postgresql
      encoding: unicode
      host: <%= ENV["DATABASE_HOST"] %>
      password: <%= ENV["DATABASE_PASSWORD"] %>
      # For details on connection pooling, see Rails configuration guide
      # https://guides.rubyonrails.org/configuring.html#database-pooling
      pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
      username: postgres
    

Fly.io (WIP)

  1. Setup docker volume:

    services:
      app:
        # ...
        volumes:
          # ...
          - fly:/root/.fly
        # ...
    volumes:
      fly:
    
  2. Install flyctl: ...

  3. /root/.fly/bin/fly auth login

  4. /root/.fly/bin/fly auth launch NOTE: Use Web UI to confirm settings!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment