My first thought is to write a templating script that will replace your variable values to whatever is needed at the time. This values could be passed into the script when called or sourced from environment variables.
I'm thinking in particular of using python's string.Template module and using the delimiter $
rather than {
since {
might be used in your Dockerfile else where. $
is the default delimiter for string.Template
. Also I would name this template file with a different extension since it Docker won't be able to build it with the templated variables inside.
Let's say your Dockerfile.template
looks like this:
FROM $ARCH/ubuntu:17.10
...
you could then set and environment variable ARCH=ppc64le
in your session and run a short script like this:
import os
import string
with open('Dockerfile.template', 'r') as f:
template = string.Template(f.read())
with open('Dockerfile', 'w') as f:
args = {key: value.strip() for (key, value) in os.environ.items()}
f.write(template.substitute(**args))
These two lines read your templated file and create a string.Template
object. The default delimiter for string.Template
is $
so it complies with your template.
with open('Dockerfile.template', 'r') as f:
template = string.Template(f.read())
This just sets us up to create and write to your actual Dockerfile
with open('Dockerfile', 'r') as f:
This fancy line is where the environment variables come in to play. os.environ is a mapping object of your os's environment variables. os.environ.items()
unpacks each pair into its respective key:value tuple. value.strip()
just cleans up the value to make sure there's no weird whitespacing. All of this is packed into dictionary comprehension, where the variable key
is the name of the environment variable, and in this case the templated variable, and it maps the the value of this variable
args = {key: value.strip() for (key, value) in os.environ.items()}
lastly we have this line. template.substitute()
takes in a mapping object and tries to substitute all of the keys with its values. **args
expands the dictionary.
f.write(template.substitute(**args))