-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpush_docker_image.py
executable file
·69 lines (51 loc) · 2.26 KB
/
push_docker_image.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
# This file should not depend on any repo python files outside of the top-level directory.
# This script pushes a local docker image to Docker Hub. It assumes that you are currently logged
# in as the username in the Docker Hub image name. Currently, this is only intended to be run by
# user dshin.
from setup_common import get_image_label, DOCKER_HUB_IMAGE, LOCAL_DOCKER_IMAGE
import argparse
import subprocess
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument("-l", '--local-docker-image', default=LOCAL_DOCKER_IMAGE,
help='Local docker image name (default: %(default)s)')
parser.add_argument("-i", '--docker-hub-image', default=DOCKER_HUB_IMAGE,
help='Docker Hub image name, without tag (default: %(default)s)')
parser.add_argument("-t", '--tag',
help='Comma-separated tags to push to Docker Hub '
'(default: "{VERSION},latest", where VERSION is the version label '
'of the local image)')
return parser.parse_args()
def get_image_id(local_image):
result = subprocess.check_output(
["docker", "images", "-q", local_image],
stderr=subprocess.STDOUT,
text=True,
).strip()
if not result:
raise Exception(f"Image {local_image} not found.")
print(f'Found image {local_image} with id {result}')
return result
def docker_tag(image_id, remote_image, tag):
print(f'Tagging {image_id} as {remote_image}:{tag}...')
subprocess.run(['docker', 'tag', image_id, f'{remote_image}:{tag}'], check=True)
def docker_push(remote_image, tag):
print(f'Pushing {remote_image}:{tag}...')
subprocess.run(['docker', 'push', f'{remote_image}:{tag}'], check=True)
def main():
args = get_args()
tags = []
if args.tag:
tags = args.tag.split(',')
else:
version = get_image_label(args.local_docker_image, 'version')
tags.append(version)
tags.append('latest')
image_id = get_image_id(args.local_docker_image)
for tag in tags:
docker_tag(image_id, args.docker_hub_image, tag)
docker_push(args.docker_hub_image, tag)
print('✅ Successfully pushed docker image!')
if __name__ == '__main__':
main()