Git Product home page Git Product logo

ruby-on-rails-on-docker's Introduction

ruby-on-rails-on-docker

This is a living code example (step-by-step) inspired by https://docs.docker.com/compose/rails

Steps

This will repo will provide commit points so that it will be easy to checkout each step to follow along in case mistakes are made. This example was done on a MacBook with Docker version 18.01. Your example may differ based on platform.

Step 1

docker build -t ruby-on-rails .

docker run -it -v "${PWD}:/app" ruby-on-rails /bin/bash

While running interactively in the Docker container, we're going to install the rails gem.

gem install rails -v 5.0.0.1

rails new app --force --database=postgresql

Step 2

You should now have rails installed and a new rails application. You may exit the container by typing exit.

Step 3

Now we can finish our Dockerfile and build our image to run our application.

docker build -t ruby-on-rails .

docker run --name web -p 3000:3000 ruby-on-rails bundle exec rails s -p 3000 -b '0.0.0.0'

Step 4

This results in an error though because we did not configure the PostgreSQL database connection properly. We need to add the following to our config/database.yml:

default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password:
  pool: 5

Also, instead of forcing ourselves to rebuild our container each time, we will mount our code directly into the container.

docker run -v "${PWD}:/myapp" --name web -p 3000:3000 ruby-on-rails bundle exec rails s -p 3000 -b '0.0.0.0'

We encounter another error though because we are not running our PostgreSQL database container.

docker run --name db postgres

docker run -v "${PWD}:/myapp" --name web --link db -p 3000:3000 ruby-on-rails bundle exec rails s -p 3000 -b '0.0.0.0'

Step 5

Now we get a database does not exist error. We can fix that by running the rake db:create in our web container.

docker exec web rake db:create

Finally, we have a fully functioning Ruby on Rails application.

ruby-on-rails-on-docker's People

Contributors

williamsbdev avatar

Stargazers

Jeremy Wells avatar Edward R avatar

Forkers

drteresavasquez

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.