-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdocker.py
More file actions
55 lines (49 loc) · 1.96 KB
/
docker.py
File metadata and controls
55 lines (49 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os.path
import shutil
import subprocess
import tempfile
def publish_release(src_image, config, target_image):
"""
Publish a new release as a Docker image
Given a source image and dictionary of last-mile configuration,
create a target Docker image on the registry.
For example publish_release('registry.local:5000/gabrtv/myapp:<sha>',
{'ENVVAR': 'values'},
'registry.local:5000/gabrtv/myapp:v23',
results in a new Docker image at 'registry.local:5000/gabrtv/myapp:v23' which
contains the new configuration as ENV entries.
"""
# write out dockerfile
dockerfile = _build_dockerfile(src_image, config)
tempdir = tempfile.mkdtemp()
dockerfile_path = os.path.join(tempdir, 'Dockerfile')
with open(dockerfile_path, 'w') as f:
f.write(dockerfile)
try:
# pull the source image to ensure we have latest
p = subprocess.Popen(['docker', 'pull', src_image])
rc = p.wait()
if rc != 0:
raise RuntimeError('Failed to pull source image')
# build the new image with last-mile configuration
p = subprocess.Popen(['docker', 'build', '-t', target_image, tempdir])
rc = p.wait()
if rc != 0:
raise RuntimeError('Failed to build release image')
# push the target image
p = subprocess.Popen(['docker', 'push', target_image])
rc = p.wait()
if rc != 0:
raise RuntimeError('Failed to push release image')
finally:
shutil.rmtree(tempdir)
# cleanup the temporary image
p = subprocess.Popen(['docker', 'rmi', '-f', target_image])
rc = p.wait()
if rc != 0:
print('warning: failed to delete temporary images')
def _build_dockerfile(image, config):
dockerfile = ["FROM "+image]
for k, v in config.items():
dockerfile.append("ENV {} {}".format(k.upper(), v))
return '\n'.join(dockerfile)