Skip to content

Instantly share code, notes, and snippets.

@saravanabalagi
Last active September 13, 2022 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saravanabalagi/0c4771a91225a7d71ce94a383266e546 to your computer and use it in GitHub Desktop.
Save saravanabalagi/0c4771a91225a7d71ce94a383266e546 to your computer and use it in GitHub Desktop.
New Rails App using Docker

Creating a new Rails app using docker

  1. Create your project dir railsapp and copy the Dockerfile inside.
  2. Comment all but first 3 lines in Dockerfile.
  3. Run docker build -t railsapp:latest .
  4. Once done, run docker run --rm -it -v $(pwd):/project railsapp:latest /bin/bash
  5. Inside docker, run the following commands
    rails new railsapp --api --database=postgresql --skip-bundle
    mv railsapp/{*,.*} /project/
    exit
  6. Then in the host, own everything back from root using sudo chown -R $USER:$USER .
  7. Add the following in config/database.yml file
    default: &default
      # other stuff
      # adapter: ...
      # encoding: ...
      # pool: ...
      host: db
      username: postgres
      password: password
    
  8. Uncomment previously commented lines in Dockerfile
  9. Copy docker-compose.yml to your project folder
  10. Then run the following docker commands:
    docker compose build
    docker compose up
    
  11. Without closing the docker compose up running task, run in another terminal
    docker compose run web rails db:create
    
  12. Your app should now be running at http://localhost:3000

Adding env file

It is a good practice to keep the configuration in a separate file inside the codebase. Fortunately, the variables specified in docker-compose.yml file, without no extra setup, will be subsituted with the variables specified inside the .env file (if they are not present in the environment variables already).

  1. Add a new line .env at the end of .gitignore file if it is not present already. This .env file which we will create in the next step contains all the secrets and hence is added to .gitignore before creation!

  2. Add a new .env file with the following contents:

    RAILS_APPNAME=railsapp
    RAILS_DEMO_POSTGRES_USERNAME=railsapp
    RAILS_DEMO_POSTGRES_PASSWORD=password
    RAILS_DEMO_POSTGRES_HOST=db
    
  3. postgres docker image allows creating specific roles with the specified password using the entrypoint script. Add under db in docker-compose.yml,

    services:
    db:
      environment:
        - POSTGRES_USER=${RAILS_DEMO_POSTGRES_USERNAME}
        - POSTGRES_PASSWORD=${RAILS_DEMO_POSTGRES_PASSWORD}
    
  4. All the environment variables inside .env file are required for our rails app and so, under web in docker-compose.yml add,

    services:
    web:
      env_file:
        - .env
    
  5. Replace the hardcoded variables previously added in the database.yml file

    default: &default
      # other stuff
      # adapter: ...
      # encoding: ...
      # pool: ...
      host: <%= ENV["RAILS_DEMO_POSTGRES_HOST"] %>
      username: <%= ENV["RAILS_DEMO_POSTGRES_USERNAME"] %>
      password: <%= ENV["RAILS_DEMO_POSTGRES_PASSWORD"] %>
    
version: '3'
services:
db:
image: postgres
environment:
POSTGRES_PASSWORD: password
web:
build: .
command: bundle exec rails s -p 3000 -b '0.0.0.0'
ports:
- "3000:3000"
depends_on:
- db
volumes:
- ./:/railsapp
FROM ruby:3.1
RUN apt update -qq && apt install -y nodejs postgresql-client
RUN gem install rails -v '7.0.3.1'
ARG APP=/railsapp
WORKDIR ${APP}
ADD Gemfile ${APP}/
RUN bundle install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment