Advice for using docker container for CI build server environment

I have a build machine running Ubuntu 18. I have installed docker and I use a 32-bit ubuntu docker container to build my code in. Basically my dockerfile is setup based on ubuntu, and I use APT to install libraries and toolchains (like clang) I need to build my C++ code. I also use a multistage build to compile various libraries not available in APT.

I’d like to know what a good general practice would be to “version” my docker file, or rather, my build environment. I was planning on committing my Dockerfile with my code. And have my automated build framework do the following (it would do this for each commit detected on branches):

  1. Check if the Dockerfile has changed
  2. If the dockerfile changed, rebuild the image
  3. Build my code in a container based on the newly built image

This works great for my latest code, but sometimes I need to go back and hotfix earlier versions of my code. And depending on how far back I go, the latest build environment may have changed to the point where the code no longer builds. Given this, any time I upgrade tools or libraries in my Dockerfile, I need the ability to increment a version number for the resulting docker image. What is a good method of doing this that works well in version control? And how can I automate this so that depending on the version of the Dockerfile, that my build pipeline uses the correct (possibly earlier) version of the same docker image for the build?