This may be the only post with links at the top, but check out Rails Skeleton on my Gitlab Page for a secure, updated, and easy to manage rails starter kit with Docker Support and Gitlab CI/CD!
Building a Production Rails Docker Image
There have been many road bumps along the way while building this docker file and the docker image, but I finally created a dockerfile that works for production rails!
10,000ft Overview of the Dockerfile
This massive Dockerfile has so much going on in it, it’s only approprite to give a brief overview before diving in to specific pieces.
The image basically starts with the base alpine image to keep everything slim. It then goes on to define the environment variables, and install the correct packages on the base image. I ran into a lot of trouble with the node packages and ensuring they were up to date and configured properly, so I pulled the node repos from the edge packages to keep them current. From there I defined the work directory for the dockerfile which is pretty standard for building any image. After I defined the work directory I started working on the Ruby aspects of the dockerfile ensuring all the gems were up to date and installed in the image.
After having this setup, I set the arguments up for the
RAILS_KEY and set the environment variables so I could compile all the assets and install webpacker correctly on the image.
I updated everything for rails webpacker and then checked to confirm it was installed with the correct versions.
After installing webpacker, I copied the rest of the
/app files, the
package.json file and then
I then compiled them with a production
rake assets:precompile and everything with the image was ready to go!
But that wasn’t all! I needed it to be slimmer. This image was just too big!
Slimming down the Image
Slimming an image was a difficult concept to grasp initially, but after reading the docs on the Docker site, it wasn’t too difficult to put the pieces together and slim down the image above.
At the root of slimming down any image is taking a “bigger” image and only using the pieces that are required from the larger image.
With this image and this dockerfile, I took the basics from the builder image and built only the pieces that I needed. I only copied only the parts that were necessary to run a production rails application. After taking these pieces, I started the server and everything was looking good!
Building the Image
docker build -t cc/builder_prod --build-arg RAILS_KEY=$MASTER_KEY . -f docker.builder.Dockerfile