[Rails] Docked Rails - Running Rails from Docker for easy start to development

Rails released a new official repository rails/docked on GitHub to help beginners to run Rails from Docker for easy start to development.

Docked Rails

Setting up Rails for the first time with all the dependencies necessary can be daunting for beginners. Docked Rails uses a Rails CLI Docker image to make it much easier, requiring only Docker to be installed.

Getting started

First install Docker (and WSL on Windows). Then copy’n’paste into your terminal:

1
2
3
4
5
6
7
8
9
10
11
12
13
docker volume create ruby-bundle-cache

alias rails='docker run --rm -it -v $PWD:/rails -v ruby-bundle-cache:/bundle ghcr.io/rails/cli'

alias rails-server='docker run --rm -it -v $PWD:/rails -v ruby-bundle-cache:/bundle -p 3000:3000 ghcr.io/rails/cli server -b 0.0.0.0'

alias rails-dev='docker run --rm -it -v $PWD:/rails -v ruby-bundle-cache:/bundle -p 3000:3000 --entrypoint bin/dev ghcr.io/rails/cli'

alias bundle='docker run --rm -it -v $PWD:/rails -v ruby-bundle-cache:/bundle --entrypoint bundle ghcr.io/rails/cli'

alias rake='docker run --rm -it -v $PWD:/rails -v ruby-bundle-cache:/bundle --entrypoint rake ghcr.io/rails/cli'

alias yarn='docker run --rm -it -v $PWD:/rails -v ruby-bundle-cache:/bundle --entrypoint yarn ghcr.io/rails/cli'

Then create your Rails app:

1
2
3
4
5
6
7
8
9
rails new weblog

cd weblog

rails generate scaffold post title:string body:text

rails db:migrate

rails-server

That’s it! You’re running Rails on http://localhost:3000/posts.

More

Of course you can also choose to enter a Docker container to create and run your Rails application. (Note the location of the --entrypoint /bin/bash parameter.)

1
docker run --rm -it --entrypoint /bin/bash -v $PWD:/rails -v ruby-bundle-cache:/bundle ghcr.io/rails/cli

Run the following command after entering the container: (Note that the -b 0.0.0.0 parameter is specified in the last command.)

1
2
3
4
5
6
7
8
9
rails new weblog

cd weblog

rails generate scaffold post title:string body:text

rails db:migrate

rails s -b 0.0.0.0

BTW, Using a Docker container to run an Rails application in a virtualized Linux environment (such as macOS, Windows) may be slower than running the same application directly.

References

[1] rails/docked: Running Rails from Docker for easy start to development - https://github.com/rails/docked

[2] Ruby on Rails — A web-app framework that includes everything needed to create database-backed web applications according to the Model-View-Controller (MVC) pattern. - https://rubyonrails.org/

[3] Package cli - https://github.com/orgs/rails/packages/container/package/cli

[4] Docker run reference | Docker Documentation - https://docs.docker.com/engine/reference/run/#entrypoint-default-command-to-execute-at-runtime