-
Notifications
You must be signed in to change notification settings - Fork 4
/
docker.py
44 lines (35 loc) · 1.84 KB
/
docker.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
import datetime, subprocess
def get_namespace():
return "st_"
def get_dockername():
""" Returns the docker namespace resp. username. """
return "precice"
def get_images():
cmd = r'docker image list --filter=reference="%s*" --format="{{.Repository}}"' % get_namespace()
print("EXECUTING:", cmd)
cp = subprocess.run(cmd, stdout = subprocess.PIPE, shell = True, check = True)
images = cp.stdout.decode().split("\n")
return [i for i in images if len(i) > 0] # Remove empty lines
def get_containers():
cmd = r'docker ps -a --filter=name="%s*" --format="{{.Names}}"' % get_namespace()
print("EXECUTING:", cmd)
cp = subprocess.run(cmd, stdout = subprocess.PIPE, shell = True, check = True)
images = cp.stdout.decode().split("\n")
return [i for i in images if len(i) > 0] # Remove empty lines
def build_image(tag, dockerfile="Dockerfile", build_args={}, force_rebuild=False, namespace=get_namespace(), docker_tag="latest", docker_login=True):
if force_rebuild:
build_args["CACHEBUST"] = datetime.datetime.now().isoformat() # A unique string for every invocation
cmd = ""
if docker_login:
cmd += "echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin && "
args = " ".join(["--build-arg " + a + "=" + b for a, b in build_args.items()])
cmd += f"docker build --network=host --file {dockerfile} --tag {namespace}{tag.lower()}:{docker_tag} {args} ."
print("EXECUTING:", cmd)
subprocess.run(cmd, shell=True, check=True)
def push_image(tag, namespace=get_namespace(), docker_tag="latest", docker_login=True):
cmd = ""
if docker_login:
cmd += "echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin && "
cmd += f"docker push {namespace}{tag.lower()}:{docker_tag}"
print("EXECUTING:", cmd)
subprocess.run(cmd, shell=True, check=True)