How does the onbuild directory work for the official Rails docker image?

docker, ruby-on-rails

Solution

Note that both the base Rails Dockerfile and the onbuild Dockerfile start from the same image: `ruby:2.1.5`. The `onbuild` image isn't called by the root directory Dockerfile--it's a separate image that's a little more "deployment-ready" than the base Rails image.

For example, look at the base Rails image. It's got all the commands to set up a Rails app environment. However, it doesn't have anything to put your actual code inside the image to run. You would need to have a Dockerfile which starts with that image but then includes additional commands to put your code in.

Now look at the Rails onbuild image. You could put a Dockerfile in your root rails directory that only said `FROM rails:onbuild`. When you build this image, it will add your Gemfile, run `bundle install`, and add your source code.

The real point of the `ONBUILD` command is that it enables you to use an image as a base image but still execute certain commands that it couldn't execute before. In the Rails example, the `onbuild` image doesn't know your specific code. However, you can use the `onbuild` image as a base (i.e. `FROM rails:onbuild`), and it would run the `onbuild` commands before executing the new commands you list in your own Dockerfile.

Problem

I have been working on building a docker image for our team that is using a version of Ruby on Rails that is too old to be supported by the official Docker image. In the process of backporting the official Rails image, I am looking at the Dockerfile from the repository used to build the official image and I do not understand how it all fits together. Specifically, how does the Dockerfile in the the onbuild directory get invoked? There is no explicit call in the root directory's Dockerfile. I have read the documentation for ONBUILD and could not find an answer.

Original source