Created
May 6, 2017 23:00
-
-
Save tnarihi/9108d6c706cdbed39e9b1f510921794f to your computer and use it in GitHub Desktop.
Docker build with template
This file contains hidden or 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 | |
""" | |
<usage> ./dockerbuild.in <dockerfile.in> [dockerfile.in variables...] [docker build options...] | |
dockerfile.in variables can be passed in a format of "var:=value". | |
For example, if Dockerfile.in is the following: | |
``` | |
FROM ubuntu:{ubuntu_version} | |
RUN apt-get update | |
``` | |
`dockerfile.in`s option should be like: | |
``` | |
./dockerfile.in Dockerfile.in ubuntu_version:=16.04 [docker build options...] | |
``` | |
, then it will generates the following Dockerfile, and build it. | |
``` | |
FROM ubuntu:16.04 | |
RUN apt-get update | |
``` | |
""" | |
import sys | |
import tempfile | |
import subprocess | |
import os | |
import shutil | |
def main(): | |
av = sys.argv[2:] | |
dockerin = sys.argv[1] | |
rest = [] | |
var = {} | |
for v in av: | |
var_val = v.split(":=") | |
if len(var_val) == 2: | |
var[var_val[0]] = var_val[1] | |
continue | |
rest.append(v) | |
str_dockerfile = open(dockerin, 'r').read().format(**var) | |
name_dockerfile = None | |
dockerdir = tempfile.mkdtemp() | |
dockerfile = os.path.join(dockerdir, 'Dockerfile') | |
with open(dockerfile, 'w') as fd: | |
fd.write(str_dockerfile) | |
print "Generated docker file --------------------------------------------------------" | |
print str_dockerfile | |
print "------------------------------------------------------------------------------" | |
cmd = ['docker', 'build', dockerdir] + rest | |
print "Run docker build with the following commands ---------------------------------" | |
print cmd | |
print "------------------------------------------------------------------------------" | |
p = subprocess.Popen(cmd) | |
p.communicate() | |
shutil.rmtree(dockerdir) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment