How to install a specific package version in Alpine and Docker?

I was building a docker image for a backend API application that I was working on, so that my colleague, who is a front-end guy can easily setup the docker container and get it up and running.

I chose Alpine Linux to build the Docker image bcos its light weight. I started adding dependencies to the Dockerfile and I came across a situation where I needed to use a specific version of the package.

So this was what I had in my Dockerfile:

RUN apk update && \
    apk upgrade && \
    apk add ruby

And I needed Ruby 2.2.4 version to be installed in the image.

With the above code, it installs the latest version of the Ruby which is 2.3.

After some googling, I figured out that we can specify the version and lock it down so that the package manager will use that specific version of the package.

So here is the same code now:

RUN apk update && \
    apk upgrade && \
    apk add ruby=2.2.4

Exploring the options, we can set a minimum or maximum version to any package using

apk add 'packagename<1.2.3-suffix'

or

apk add 'packagename>1.2.3-suffix'

Hope this helps someone or even me in future. :)