If you haven’t heard of fig and are using Docker, you need to check it out. Basically Fig is a tool that allows users to quickly create development environments using Docker. Fig alleviates the complexity and tediousness of having to manually bring containers up and down, stitch them together and basically orchestrate a Docker environment. On top of this, Fig offers some other cool functionality, for example, the ability to scale up applications. I am excited to see what happens with the project because it was recently merged in to the Docker project. My guess is that there will be many new features and additions to either Docker if Fig gets rolled in to the Docker core project. For now, you can check out Fig here if you haven’t heard of it and are interested in learning more.
One issue that I have run in to is that there is currently not a great way to handle a large number of environment variables in fig. In Docker there is an option that allows a user to pass in an environment variable file with the –env file <filename> flag. To do the same with Fig in its current form, you are forced to list out each individual environment variable in your configuration which can quickly become tedious and confusing.
There is a PR out for adding in the ability to pass an environment variable file in to fig via the env-file option in a fig.yml file. This approach to me is much easier than adding each environmental variable separately to the configuration with the environment option as well as having to update the fig.yml configuration if any of the values ever change. I know that functionality like this will get merged eventually but until then I have been using the PR as a workaround to solve this issue, I think that this is also a good opportunity to show people how to get a project working manually with custom changes. Luckily the fix isn’t difficult.
This post will assume that you have git, python and pip installed. If you don’t have these tools installed go ahead and get that done. The first step is to clone the fig project on github on to your local machine, see above for the link to the PR.
git clone [email protected]:docker/fig.git
Jump in to the fig project you just cloned and edit the service.py file. This is the file that handles the processing of environment variables. There are a few sections that need to be updated in the config. Check the PR to be sure, but at the time of the writing, the following code should be added.
Line 55
- supported_options = DOCKER_CONFIG_KEYS + ['build', 'expose'] + supported_options = DOCKER_CONFIG_KEYS + ['build', 'expose', 'env-file']
Line 318
+ def _get_environment(self): + env = {} + + if 'env-file' in self.options: + env = env_vars_from_file(self.options['env-file']) + + if 'environment' in self.options: + if isinstance(self.options['environment'], list): + env.update(dict(split_env(e) for e in self.options['environment'])) + else: + env.update(self.options['environment']) + + return dict(resolve_env(k, v) for k, v in env.iteritems()) +
LIne 352
- if 'environment' in container_options: - if isinstance(container_options['environment'], list): - container_options['environment'] = dict(split_env(e) for e in container_options['environment']) - container_options['environment'] = dict(resolve_env(k, v) for k, v in container_options['environment'].iteritems()) + container_options['environment'] = self._get_environment()
Line 518
+ + +def env_vars_from_file(filename): + """ + Read in a line delimited file of environment variables. + """ + env = {} + for line in open(filename, 'r'): + line = line.strip() + if line and not line.startswith('#') and '=' in line: + k, v = line.split('=', 1) + env[k] = v + return env
That should be it. Now you should be able to install fig with the new changes. Make sure you are in the root fig directory that contains the setup.py file.
sudo python setup.py develop
Now you should be able to edit your fig.yml file to reflect the changes that have been added to fig via env-file. Here is what a sample configuration might look like.
testcontainer: image: username/testcontainer ports: - "8080:8080" links: - "mongodb:mongodb" env-file: "/home/username/test_vars"
Notice that nothing else changed. But instead of having to list out environment variables one at a time we can simply read in a file. I have found this to be very useful for my workflow, I hope others can either adapt this or use this as well. I have a feeling this will get merged in to fig at some point but for now this workaround works.