Develop Ruby on Rails with Docker!
- Ruby 2.5.0
- Rails 5.1.4
Install Docker for your machine here.
Clone this repo:
$ git clone [email protected]:pdarden/railsbridgenyc_docker.git
Create your project directory:
- Remove git remote
$ git remote remove origin
- Change directory name
$ cd .. && mv railsbridgenyc_docker <<new_project_name>> && cd <<new_project_name>>
Create a bootstrap Gemfile
which just loads Rails:
$ touch Gemfile
Open Gemfile in your editor:
source 'https://rubygems.org'
gem 'rails', '5.1.4'
Create an empty Gemfile.lock
:
$ touch Gemfile.lock
Build new project:
$ docker-compose run web rails new . --force --database=postgresql
Build image again:
$ docker-compose build
Just add Dockerfile
, docker-compose.yml
, and run.sh
to your project and run
$ docker-compose build
In config/database.yml
replace the file with this content (feel free to rename the database, see <<my_app_name>>
):
default: &default
adapter: postgresql
encoding: unicode
username: postgres
password:
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: <%= ENV.fetch("DB_HOST") %>
development:
<<: *default
database: <<my_app_name>>_development
test:
<<: *default
database: <<my_app_name>>_test
production:
<<: *default
database: <<my_app_name>>_production
password: <%= ENV['APP_DATABASE_PASSWORD'] %>
- Setup the database for your project:
$ docker-compose run web rake db:create
$ docker-compose run web rake db:migrate
- Make
run.sh
excutable:
$ chmod +x run.sh
- Spinning up the rails server:
$ docker-compose up
Note: This will excute the commands in run.sh
- Visit http://localhost:3000 🚀
docker-compose run web <<commands>>
, runs commands in web service container E.g.,
$ docker-compose run web bin/rails c
docker-compose run web bash
, runs bash to interact with the directory inside the web service containerdocker-compose build
, build image whenDockerfile
changes or when you want to update or add gems.docker-compose up
, starts up containers.docker-compose up -d
, starts up containers in the background.docker-compose down
, stops containers.docker-compose ps
, view container processes, container name, container id.docker attach <<contianer_name>>
, attach container to interact withbinding.pry
.Control + p
Control + q
, detach from container.