> docker image pull ruby:2.6
						2.6: Pulling from library/ruby
						90fe46dd8199: Downloading [=======================>                           ]  24.09MB/50.38MB
						35a4f1977689: Download complete
						bbc37f14aded: Download complete
						74e27dc593d4: Downloading [===========>                                       ]  12.11MB/51.83MB
						4352dcff7819: Downloading [==========>                                        ]  38.61MB/192.2MB
						cffd0bce6043: Waiting
						342084bc3791: Waiting
						00830f8007ce: Waiting
						...
						...
						Digest: sha256:a16808c92f50c6be34be65da57f22b58bc5e7fbea4707b9cfe2516898b0f3922
						Status: Downloaded newer image for ruby:2.6
						docker.io/library/ruby:2.6
						>_
					

Enter the chroot jail...


						> docker container run -ti --rm ruby:2.6 bash
						root@41a8326ef001:/>
					

Filesystem isolation + process isolation


						root@41a8326ef001:/> ps uxa
						USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
						root         1  0.0  0.1   3864  3256 pts/0    Ss   15:53   0:00 bash
						root         8  0.0  0.1   7636  2660 pts/0    R+   15:56   0:00 ps uxa
						root@41a8326ef001:/>_
					

Install rails inside our chroot jail...


						root@41a8326ef001:/> gem install rails
						...
						root@41a8326ef001:/> rails --version
						Rails 6.0.2.2
						root@41a8326ef001:/>_
					

Exit our chroot jail...


						root@41a8326ef001:/> exit
						>_
					

Enter our chroot jail for the second time...


						> docker run --rm -ti ruby:2.6 bash
						root@41a8326ef001:/> rails
						bash: rails: command not found
						root@41a8326ef001:/> _
					

Create a docker image with rails inside

1: create a Dockerfile

						# Dockerfile
						FROM ruby:2.6
						RUN gem install rails
					
2: Build the new image

						> docker build -f Dockerfile -t rails:6.0 .
						Sending build context to Docker daemon  2.048kB
						Step 1/2 : FROM ruby:2.6
						 ---> 5d2ee2444c80
						Step 2/2 : RUN gem install rails
						 ---> Running in be541c362bc8
						Successfully installed concurrent-ruby-1.1.6

						HEADS UP! i18n 1.1 changed fallbacks to exclude default locale.
						But that may break your application.

						If you are upgrading your Rails application from an older version of Rails:

						Please check your Rails app for 'config.i18n.fallbacks = true'.
						If you're using I18n (>= 1.1.0) and Rails (< 5.2.2), this should be
						'config.i18n.fallbacks = [I18n.default_locale]'.
						If not, fallbacks will be broken in your app by I18n 1.1.x.

						If you are starting a NEW Rails application, you can ignore this notice.

						For more info see:
						https://github.com/svenfuchs/i18n/releases/tag/v1.1.0

						Successfully installed i18n-1.8.2
						Successfully installed thread_safe-0.3.6
						Successfully installed tzinfo-1.2.7
						...
						...
						40 gems installed
						Removing intermediate container be541c362bc8
						 ---> 8cb455dd004c
						Successfully built 8cb455dd004c
						Successfully tagged rails:6
					
3: Run rails inside a container using our new docker image

						> docker run --rm -ti rails:6 bash
						root@44493fd551b5:/> rails --version
						Rails 6.0.2.2	
					

Run rails app inside a container

1: Run bash inside the rails container...

						docker run --rm -ti \ 
						-v $(pwd):/bind_volume \
						-v bundle_cache:/usr/local/bundle \
						-v node_modules_cache:/bind_volume/simple_app/node_modules \
						-p "3000:3000" \
						rails:6 bash

						root@44493fd551b5:/> 
					
...with all the volumes we need attached to it
2: Create the rails app

						root@44493fd551b5:/> cd /bind_volume
						root@44493fd551b5:/> rails new simple_app
						... (all the output of the rails new command goes here) 
					
3: Launch the rails app

						root@44493fd551b5:/> rails s -b 0.0.0.0
					

I should see it using my browser...

4: Exit the container

						root@44493fd551b5:/> exit
						>_
					

I should not be able to access the rails app from my browser.

5: Launch the rails app again

						docker run --rm -ti \ 
						-v $(pwd):/bind_volume \
						-v bundle_cache:/usr/local/bundle \
						-v node_modules_cache:/bind_volume/simple_app/node_modules \
						-p "3000:3000" \ 
						-w /bind_volume
						rails:6 rails s -b 0.0.0.0
					

Thanks to the volumes I've used, everything I needed to be persisted, was persisted

Start a multi container application


						> git clone https://github.com/alfonso-slides/docker-and-rails-where-to-start-multicontainer-app.git multicontainer_app
						> cd multicontainer_app
					
Let's have a look at the docker compose file

						version: '3'
						services:

						  db:
						    image: postgres:12
						    volumes:
						      - db_data:/var/lib/postgresql/data
						    environment:
						      - POSTGRES_PASSWORD=postgres
						      - POSTGRES_DB=development
						  web:
						    build:
						      context: .
						      dockerfile: Dockerfile
						    ports:
						      - '3000:3000'
						    working_dir: '/app'
						    command: ["rails", "s", "-b", "0.0.0.0"]
						    volumes:
						      - .:/app  
						      - gems_cache:/usr/local/bundle
						      - node_modules_cache:/app/node_modules
						    depends_on:
						      - db    

						volumes:
						  db_data:
						  gems_cache:
						  node_modules_cache:
					
Warm up the cache

						> docker-compose run --rm web bundle install
						> docker-compose run --rm web rails yarn:install
					
Create the database

						> docker-compose run --rm web rails db:create
					
Load the schema

						> docker-compose run --rm web rails db:schema:load
					
Seed the database

						> docker-compose run --rm web rails db:seed
					
Start the application

						> docker-compose up
					

Resources

Some resources that helped me