Created
June 22, 2017 11:01
-
-
Save sdbondi/a789c44070051696410a532fdb870677 to your computer and use it in GitHub Desktop.
Use JQ to parse environment variables from docker container and then transform them into arguments to be passing into the docker run command.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import json | |
from subprocess import check_output | |
def inspect(container): | |
return json.loads(check_output(["docker", "inspect", container])) | |
def prep_arg(string): | |
return string.replace(' ', '\\ ').replace('\t', '').replace('\'', '\\\''); | |
def transform_to_args(env): | |
filtered_env = filter((lambda x: not x.startswith('affinity:container')), env) | |
return reduce((lambda acc, string: acc + (" -e %s" % prep_arg(string))), filtered_env, ""); | |
def main(): | |
parser = argparse.ArgumentParser(description='Process some integers.') | |
parser.add_argument('docker-id', type=str, help='Docker container id') | |
args = vars(parser.parse_args()) | |
data = inspect(args['docker-id']) | |
print transform_to_args(data[0]['Config']['Env']) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment