Generating a New Rails App Without Ruby Installed

Familiarize yourself with using Docker for some real-world tasks.

Interactive Bash shell

We need to run multiple commands in succession in a container in order to generate the Rails project. We could craft a really long, ugly docker run that executes instructions one after another. However, that’s going to be hard to comprehend.

Instead, we can start a container running an interactive Bash shell. This will provide us with a terminal session running inside the container. From there, we can run as many commands as we like, much as if we had a local Bash session.

Note: All the options and commands used below will be explained and run in the later section of the lesson.

We are now going to start an interactive Bash shell inside a container based on the familiar ruby:2.7 image:

$ docker run -i -t --rm -v ${PWD}:/usr/src/app ruby:2.7 bash 

The option --rm creates a throwaway container that will be deleted once we are done with it. When this command is run, the following terminal prompt appears:

root@0c286e8bda42:/# 

This different prompt shows that we are now successfully running a Bash shell inside a container. The root@ and # indicate that we are the root user and that this is the default user inside a container.

Create a Ruby project

From this new Bash prompt, we can now issue any commands we want the container to run. First, move into the folder we are going to use for our project:

root@0c286e8bda42:/# cd /usr/src
root@0c286e8bda42:/# mkdir app
root@0c286e8bda42:/# cd app 

After that, we will install the Rails gem:

root@0c286e8bda42:/usr/src/app# gem install rails

You will see the Rails gem and all its dependencies being installed. The following command will generate our project:

root@0c286e8bda42:/usr/src/app# rails new myapp --skip-test --skip-bundle

The option --skip-test tells Rails not to use its default of Minitest. That’s because, in the coming chapters, we will use RSpec to demonstrate how to configure our tests in a Dockerized environment.

The option --skip-bundle tells Rails not to run bundle install after generating the project. The container is just a temporary vehicle for us to generate the Rails project. Since we are going to get rid of it, there’s no need to install the project dependencies.

Try to connect the following terminal to see all how all commands work and view our Rails project files being created.

Get hands-on with 1400+ tech skills courses.