Dockerizing git Version in a Rails App
From my article on tracking a Ruby on Rails server version with git, this provided an easy way to track the version the server is running from going to a specific endpoint and you can track the server version all the way down to the commit it is running through the git branch name and SHA.
I will show how to construct a Dockerfile that includes the
directory so the version information is in the container.
For the rest of the article, I will be working from the directory of the cloned repository.
Create Docker Image
I have the following contents in my Dockerfile that describes specific steps to build the Docker image:
A quick high level overview of each section:
||use the ruby 2.5.0 docker image as a base|
||install operating dependencies|
||make and set the working directory|
||Copy the Gemfiles and bundle install items|
||Copy the application to the
||The default command to run for the container, in this case, start the server|
To build the container, with an image tag:
rails_git_version run the
Let’s check to make sure the build succeeded:
Now we are ready to test things out by running the container and verifying endpoints.
Start the container from the just created Docker image
Let’s do a quick check to see if it’s really running by loading the
default Rails endpoint using the
Cool, the main homepage is working, let’s check for the server version
by using the
curl command on the
Great - just what we expected. It’s the same result as the local server, which we can also quickly test, by starting up the server using command:
In another terminal, run the same
curl command, but on a different
port (3000 vs. 3100):
If these don’t match, make sure the git branch is same as the container’s build (or rebuild the container.)
Make Container from Different Branch
Now, let’s create a new docker image from a different branch and run that.
First, let’s switch to another branch with
git checkout and build
the Docker image with
docker build, this time tagging the image as:
With a different tag, now we can have both the
container and the
feature/new_version container at the same time.
docker images to Make sure we have the new Docker image
Verify New Container Version
Just like before, we run the container with
docker run, but this
rails_git_version_feature and a different port (3200)
And just like before, use
curl command to do a quick test for the
version, on port 3200:
We can also test the local version to see:
in another terminal:
I used Docker tags when building the image so there can be multiple Docker images at the same time.
Why not use this for the version?
One definitely can, and should!
I would recommend using Docker image tags as a way to manage the version behind the scenes from the deployment control center. The tag is never seen by the user from the other side of the server.
Having another way to validate the version helps when there’s a server issue.
We just covered building a Docker image for a Ruby on Rails app that incorporates the git version.
By having the git version within the Docker container, one can know exactly what version of the server is the container is running without digging into container.
This also multiple containers to have different versions by using tags, but also absolute verification of the server.