Skip to content

bdavidxyz/simplest-rails-docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simplest possible Rails 6 + docker-compose project

This repository is intended to be the simplest possible Rails 6 + docker-compose demo.

It builds and runs a simple "hello" page locally.

Rails, Database and Webpacker are the 3 mandatory services to get things works.

Steps to reproduce

0. Prerequisites

$> docker -v
Docker version 17.12.0-ce

$> docker-compose -v
docker-compose version 1.18.0

Any upper version should work.

1. Build images

Run :

docker-compose build

2. Build project

Run :

docker-compose run --rm --no-deps web-srv rails new . --skip --database=postgresql

This will run the rails new command on our web-srv service defined in docker-compose.yml.

Flag explanations:

  • --no-deps - Tells docker-compose run not to start any of the services in depends_on.
  • --skip - Tells rails NOT to overwrite existing files, such as README or .gitignore
  • --database=postgresql - Tells Rails to default our db config to use postgres.
  • --rm - Removes container after run

3. Adapt database.yml and webpacker.yml to Docker configuration

Open and change config/database.yml

default: &default
  host: db-srv # <---- add this property
  username: myuser # <---- add this property

4. Create hello world page

Run :

docker-compose run --rm --no-deps web-srv rails generate controller hello say_hello

Flag explanations:

  • --no-deps - Tells docker-compose run not to start any of the services in depends_on.
  • --rm - Removes container after run

5. Start services

docker-compose up

6. Visit "hello world" page

go to http://localhost:3000/hello/say_hello

  • Open the browser console to check that CSS, JS loaded correctly and no 404 request occured.
  • Check also the logs of the services in your terminal to notice that no error occured.

Restart from scratch

removes all docker containers, images, and volumes. Warning, all data will be lost.

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
docker rmi $(docker images -q) -f
docker volume prune

remove all files generated by the "rails new" command

rm -rf -v !("Dockerfile"|".dockerenv"|".gitignore"|"README.md"|"docker-compose.yml") && rm .browserslistrc && rm .ruby-version

About

docker-compose + Rails 6, Postgre, Webpacker "hello world" example

Resources

Stars

Watchers

Forks

Packages

No packages published