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:
.envdocker-compose.yamlDockerfileentrypoint.shGemfileGemfile.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=postgresqlGemfile has different content after new Rails application has been created, so you have to build Docker image once
again:
$ docker compose buildConfigure database connection:
-
Open
config/database.ymlfile -
Find
default: &defaultsection: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: &defaultsection 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 -dCreate all databases:
$ docker compose exec web rake db:createOpen application by entering http://localhost:3000 in web browser:
Enjoy!
