Unit 4 Chapter 4: Rails Tutorial Sample App
In this chapter, you will download and run Michael Hartl's Rails Tutorial Sample App. In the interest of saving time, the rvm-rails-general Docker image comes preinstalled with the latest version of Ruby and the versions of the rails, pg, and nokogiri gems specified in the Rails Tutorial Sample App. At the end of this chapter, you will also learn the docker-restart.sh and nuke.sh scripts.
Topics Covered
Using the join.sh script to perform multiple tasks simultaneously
Downloading the Source Code
- From the same rvm-rails-general directory where you entered the rvm-rails-general Docker container, enter the command "sh reset.sh" to return to Docker.
- Enter the following command:
git clone https://bitbucket.org/railstutorial/sample_app_4th_ed.git
Setting Up and Viewing The App
- Enter the following commands:
cd sample_app_4th_ed
bundle install; rails db:migrate; rails test; rails s -b 0.0.0.0 -p 3000
- It will take a few minutes for the Sample App to be set up and tested. All tests should pass.
- After the setup process has been completed, the Rails server will be activated. In your web browser, go to http://localhost:3010/ to view the Sample App.
Starting the Rails Sandbox
- In LXTerminal, open a new tab. You should be within the rvm-rails-general directory where you entered the rvm-rails-general Docker container.
- Enter the command "ls". One of the scripts you'll see is join.sh.
- Enter the command "sh join.sh". This enters you into the same rvm-rails-general Docker container where you are running the Sample App. Note that the time stamp showing when you entered the Docker container earlier in this chapter is present.
- Enter the following commands:
cd sample_app_4th_ed
rails console --sandbox
- You are now in the Rails sandbox.
- Enter the command "Micropost.all". You'll see some screen output but no actual microposts.
- Enter the command "User.all". You'll see some screen output but no actual users.
Seeding the Database
- In LXTerminal, open a new tab. In this new tab, enter the command "sh join.sh". This is a third terminal for interacting with the Sample App. Remember that the first terminal is dedicated to the Rails server, and the second terminal is dedicated to the Rails sandbox.
- Enter the following commands:
cd sample_app_4th_ed
rails db:migrate:reset; rails db:seed
- It will take a few minutes for the Sample App to finish seeding the database with fake data.
Viewing Data in Rails Sandbox
- In order to update what Rails sandbox shows, you must exit it and then restart it. Enter the command "exit", and then enter the command "rails console --sandbox".
- Enter the command "Micropost.all". You will now see a list of all microposts.
- Enter the command "User.all". You will now see a list of all users.
Starting the Guard Server
- NOTE: I don't agree with the use of the automated test tool Guard, but it's a good example of simultaneous actions.
- In LXTerminal, go to the third tab, the one where you seeded the database.
- Enter the command "bundle exec guard" to start the Guard server.
- Once Guard has started up, press the Enter key again to run the tests, which should all pass.
- In your desktop Linux system, edit the app/models/micropost.rb file in the sample app project. Change the maximum length of the content from 140 to 1.
- Check the Guard tab (third tab) in LXTerminal. You'll see that the micropost model test now fails.
- In LXTerminal, start a fourth tab, and enter the command "join.sh".
- Enter the command "cd sample_app_4th_ed; git status". Note that the app/models/micropost.rb has now changed.
- Enter the command "git checkout app/models/micropost.rb". This removes the change you just made.
- Return to the third tab of LXTerminal, the Guard tab. You'll see that the micropost model test now passes again.
Exiting
- Go to the fourth LXTerminal tab used to access the Sample App. Enter the command "exit". You have exited the rvm-rails-general Docker container in that Terminal tab, but you are still connected in the remaining tabs.
- In the third tab, press Ctrl-C to stop the Guard server. Then enter the command "exit" to leave the Docker container.
- In the second tab, enter the command "exit" to exit the Rails sandbox. Then enter "exit" again to leave the Docker container.
- In the first tab, press Ctrl-C to stop the Rails server. Then enter the command "exit" to leave the Docker container.
Restarting Docker
- In the first tab, enter "sh resume.sh" to re-enter the Docker container. Then enter the following commands:
cd sample_app_4th_ed
rails s -b 0.0.0.0 -p 3000
- In the second tab, enter "sh join.sh". Once in Docker, enter the following commands:
cd sample_app_4th_ed
rails console --sandbox
- In the third tab, enter "sh join.sh". Once in Docker, enter the following commands:
cd sample_app_4th_ed
rails console --sandbox
- In the fourth tab, enter the command "sh docker-restart.sh". This shuts down and restarts Docker. Note that you are kicked out of the Docker container in all of the previous tabs. You must use the resume.sh script to go back in, and then you must start up the processes that were shut down during the restart process.
Nuking Docker Containers and Images
In any of the four tabs, enter the command "sh nuke.sh". This erases all Docker containers and images currently on your machine, and the "docker ps -a" and "docker images -a" commands that are automatically executed when you run this script confirm this.
Conclusions
- Congratulations! You now understand how to use Docker under the Ruby on Racetracks way.
- For every Rails project that I'm on, I provide a custom Docker image that includes the correct versions of Ruby, the Rails gem, and certain other gems. This makes the setup process much faster and shorter.
- Note that you use a different Docker offset for each Docker container used in this tutorial. Using a different offset value for different Docker containers allows you to run multiple Docker containers simultaneously. If a Docker container you are running uses any ports, trying to start a second Docker container that uses the same ports will lead to error messages due to the conflict.