Dynamic Dockerfile.erb

Sometimes you may need a little more dynamic control of your Dockerfile. For these cases, ufo supports dynamically creating a Dockerfile from a Dockerfile.erb. If Dockerfile.erb exists, ufo uses it to generate a Dockerfile as a part of the build process. These means that you should update the source Dockerfile.erb instead, as the Dockerfile will be overwritten. If Dockerfile.erb does not exist, then ufo will use the Dockerfile as-is.

Example

The Dockerfile.erb has access to variables defined in dockerfile_variables.yml. The variables should defined underneath an UFO_ENV key. Examples:

.ufo/settings/dockerfile_variables.yml:

---
development:
  base_image: 112233445566.dkr.ecr.us-west-1.amazonaws.com/demo/sinatr:base-2019-06-10T03-22-34-f91cdd350
production:
  base_image: 778899001122.dkr.ecr.us-west-1.amazonaws.com/demo/sinatr:base-2019-06-10T03-23-34-abccddxzy

Note, the base_image key is automatically updated by ufo docker base when Dockerfile.erb exists.

Here’s what the Dockerfile.erb looks like:

FROM <%= @base_image %>
# ...
CMD ["bin/web"]

When UFO_ENV=production, it’ll produced the following.

Dockerfile:

FROM 778899001122.dkr.ecr.us-west-1.amazonaws.com/demo/sinatr:base-2019-06-10T03-23-34-abccddxzy
# ...
CMD ["bin/web"]

The above example is demonstrates a good use-case. You may want a different FROM statement in your Dockerfile on a per-environment basis. In this case, we’re using different ECR repositories from different AWS accounts for development vs production. The FROM statement changes based on which AWS account you’re using.

General Steps

The generl steps are:

  1. Create a Dockerfile.erb with <%= @base_image %>
  2. Run: ufo docker base to generate dockerfile_variables.yml
  3. Run: ufo docker build to build a Dockerfile. Note, the ufo ship command also builds the Dockerfile.

Remember when using the Dockerfile.erb, the Dockerfile is generated and overwritten. So you should update the Dockerfile.erb.

Build Args

Why not use build args?

Ufo uses a yaml file so users will not have to remember to provide the build arg. It is also easy to update the dockerfile_variables.yml with the ufo docker base command.

Pro tip: Use the <- and -> arrow keys to move back and forward.

Edit this page

See a typo or an error? You can improve this page. This website is available on GitHub and contributions are encouraged and welcomed. We love pull requests from you!