Scaffold Application with Docker + Ruby on Rails!
Prerequistics
- Docker installed in PC
- Ruby and Rails installed in PC
Premise in windwons
- WSL2 / Ubuntu / Docker Desktop must be installed * Click here for the procedure
Procedure
File preparation
-
Launch Docker Desktop
-
Start powershell or termical
-
Create a D: \ docker \ test-app directory
In Mac or ubuntu or linux
mkdir docter/test-app
in Windows
make directory manually docket\test-app
-
Change to the test-app directory
cd D:\docker\test-app
- Create a Dockerfile
D:\docker\test-app\Dockerfile
Describe the following in the Dockerfile
- Create a Gemfile
D:\docker\test-app\Gemfile
Describe the following in the Gemfile
- Create Gemfile.lock (empty file)
D:\docker\test-app\Gemfile.lock
- Create a data folder
D:\docker\test-app\data
- Create docker-compose.yml
D:\docker\test-app\docker-compose.yml
Describe the following in docker-compose.yml
App creation + settings
Hinagata app creation
docker-compose run web rails new . --force --database=postgresql
- Rebuild the image
docker-compose build
- Rewrite config / database.yml
- Launch the app
docker-compose up
- Create DB * Start and execute powershell separately from running
docker-compose run web rake db:create
- Execute scaffold * Start and execute powershell separately from running
docker-compose run web bin/rails g scaffold User name:string
- Apply migration * Start and execute powershell separately from running
docker-compose run web bin/rails db:migrate
- Confirmation
Access the site with a browser and check
http://localhost:3000
Error handling
- If “Gem :: InstallError: XXXX requires Ruby version” appears when you execute the command in the terminal → Sorry to trouble you, but let’s delete the test-app created this time and start over. In step 6 of file preparation, set the ruby version in the Dockerfile to 2.4.0 .
FROM ruby:2.4.0
- If you run http: // localhost: 3000 in your browser and get a Rails error of “key must be 32 bytes” → Sorry to trouble you, but let’s delete the test-app created this time and start over….
In step 7 of file preparation, set the Rails version in Gemfile to 5.0.1 .
gem 'rails', '5.0.1'
Reference article
Happy Coding :)