Other Pages

Expand All

Create A Rails App With Docker

Step 1: Change to your home directory

cd stands for change directory.

If you are still in irb, type quit to exit.

Windows
Type this in the terminal:
cd c:\Sites

cd c:\Sites sets our Sites directory to our current directory.

Mac or Linux
Type this in the terminal:
cd ~

cd ~ sets our home directory to our current directory.

Step 2: Create a railsbridge directory

Type this in the terminal:
mkdir railsbridge

mkdir stands for make directory (folder).

We've made a folder called railsbridge.

Step 3: Change to your new railsbridge directory

Type this in the terminal:
cd railsbridge

Step 4: Create a new Rails app

Type this in the terminal:
mkdir test_app
Type this in the terminal:
cd test_app

In your editor, create a new file called Gemfile

Copy the following into Gemfile:

source "https://rubygems.org"
gem "rails", "5.2.0"
Type this in the terminal:
touch Gemfile.lock

In your editor, create a new file called Dockerfile:

Copy the following into Dockerfile:

FROM ruby:2.5.1-slim

RUN apt-get update -qq && apt-get install -y build-essential libpq-dev git vim nodejs postgresql-client build-essential

ENV APP_HOME /test_app
RUN mkdir -p $APP_HOME
WORKDIR $APP_HOME
COPY Gemfile* $APP_HOME/
RUN bundle install
COPY . $APP_HOME

In your editor, create a new file called docker-compose.yml:

Copy the following into docker-compose.yml:

version: '3'
services:
  db:
    image: postgres:10.3
    volumes:
      - db_data:/var/lib/postgresql/data
  web:
    build: .
    environment:
      DATABASE_URL: db
    tty: true
    stdin_open: true
    volumes:
      - .:/test_app
    command: bash -c 'rm -rf /test_app/tmp/pids/server.pid && bin/rails s -p 3000 -b 0.0.0.0'
    ports:
      - "3000:3000"
    depends_on:
      - db
volumes:
  db_data:
    driver: local

In your editor, create a new file called .dockerignore:

Copy the following into .dockerignore:

.git
.dockerignore
.byebug_history
log/*
tmp/*
Dockerfile
README.md

Build your rails app:

Type this in the terminal:
docker-compose run web rails new . --force --database=postgresql
Type this in the terminal:
docker-compose build

Setup the database:

In your editor, open up config/database.yml and change it to the following:

default: &default
  adapter: postgresql
  encoding: unicode
  username: postgres
  password:
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  host: <%= ENV.fetch("DATABASE_URL") %>
development:
  <<: *default
  database: test_app_development
test:
  <<: *default
  database: test_app_test
production:
  <<: *default
  database: test_app_production
  password: <%= ENV['APP_DATABASE_PASSWORD'] %>
Type this in the terminal:
docker-compose run web rails db:create
Type this in the terminal:
docker-compose run web rails db:migrate
Type this in the terminal:
docker-compose up

docker-compose up will execute the command, bin/rails s -p 3000 -b '0.0.0.0', in docker-compose.yml

In your browser, go to http://localhost:3000

Screenshot of the browser on localhost 3000 showing the rails intro page

Open a new terminal tab, and stop the server.

Type this in the terminal:
docker-compose down

Step 5: Generate a database model

If your prompt doesn't already show that you are (still) in the test_app folder

Type this in the terminal:
cd test_app

Run the following in terminal:

Type this in the terminal:
docker-compose run web bash
Type this in the terminal:
rails generate scaffold drink name:string temperature:integer
Type this in the terminal:
rails db:migrate

Open another tab in terminal to bring the rails server up in Docker with:

Type this in the terminal:
docker-compose up

In the browser, visit http://localhost:3000/drinks

  1. Click on "New drink"
  2. Enter Cappuccino for the name
  3. Enter 135 for the temperature.
  4. Click on "Create Drink".

You should see: Screenshot of the drink detail page

Open a new terminal tab, and stop the server.

Type this in the terminal:
docker-compose down

Next Step: