Based on https://github.com/docker/awesome-compose/tree/master/official-documentation-samples/rails/ conception.
Useful for creating new, empty application.
Clone this repository or copy to your project these files:
.env
docker-compose.yaml
Dockerfile
entrypoint.sh
Gemfile
Gemfile.lock
All files from root directory, besides images/
, .gitattributes
and README.md
.
Create new Rails application:
$ docker compose run --rm --build --no-deps web rails new . --force --database=postgresql
Gemfile
has different content after new Rails application has been created, so you have to build Docker image once
again:
$ docker compose build
Configure database connection:
-
Open
config/database.yml
file -
Find
default: &default
section:default: &default adapter: postgresql encoding: unicode # For details on connection pooling, see Rails configuration guide # https://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
-
Append these options at the end:
host: db username: app password: p4ssw0rd
So the
default: &default
section may look like this:default: &default adapter: postgresql encoding: unicode # For details on connection pooling, see Rails configuration guide # https://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> host: db username: app password: p4ssw0rd
Let's start all containers:
$ docker compose up -d
Create all databases:
$ docker compose exec web rake db:create
Open application by entering http://localhost:3000 in web browser:
Enjoy!