This is part 2 of a 3-part series on building a Docker image for your Ruby on Rails application.
- Part 1 is about making your application suitable to run inside a docker container.
- Part 2 (this post) covers building a base image that contains the prerequisites needed for a typical Ruby on Rails app.
- Part 3 (coming soon!) - describes how to add the application files, create and run your container.
Preparing the Environment
- Choose a base image to start from
- Install nodejs runtime
- Install yarn
- Install the database client
Let’s go over these steps, one at a time.
The Base Image
We start from the official ruby Docker image:
This tag, by design, contains the most common Debian packages, which you will need later to compile the gems that have native extensions.
Install nodejs runtime
We will be needing nodejs to handle assets precompiling. Here is how to install it from nodesource:
Note that this is a single
RUN command that executes multiple shell operations: installs the repository source, downloads package lists, installs node and removes the package lists. There is a reason to do it this way.
A Docker image is built up from a series of layers. Each docker RUN command that modifies the image contents creates an additional layer on the filesystem. If we had done the above operation on 4 steps, it would result in 4 layers that have take storage space on your system. Doing the 4 operaions in one step results in only one layer which has the files relevant to run node.
The installation, using
npm, is pretty straightforward:
Next step, database client. You will need to install the client library and header files. This will, of course, be completely different depending on the database you choose. Here is what you would need for postgresql:
We’ve seen how to prepare a Dockerfile with the typical prerequisites for a Rails application. In the next post, we put things together by installing our container-ready application on the base image. Stay tuned!