Add ability to build image.
This commit is contained in:
parent
c5c5c040c1
commit
a7e72a9257
29
otherworld
29
otherworld
@ -1,5 +1,5 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from subprocess import call, check_output
|
from subprocess import call, check_call, check_output
|
||||||
from time import sleep
|
from time import sleep
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
@ -58,6 +58,19 @@ def docker_exec (container, command, user):
|
|||||||
def docker_rm (container):
|
def docker_rm (container):
|
||||||
call(["docker", "rm", "-f", container])
|
call(["docker", "rm", "-f", container])
|
||||||
|
|
||||||
|
def docker_build (build_path, options, image_name):
|
||||||
|
check_call(["docker", "build"] + options + ["-t", image_name, build_path])
|
||||||
|
return image_name
|
||||||
|
|
||||||
|
def docker_pull (image_name):
|
||||||
|
check_call(["docker", "pull", image_name])
|
||||||
|
|
||||||
|
def resolve_build_path (build_path):
|
||||||
|
if build_path is not None and build_path.startswith("~/"):
|
||||||
|
build_path = f"{os.environ['HOME']}/{build_path[1:]}"
|
||||||
|
|
||||||
|
return build_path
|
||||||
|
|
||||||
def expand_user_volumes (volumes):
|
def expand_user_volumes (volumes):
|
||||||
outer_home = os.environ["HOME"]
|
outer_home = os.environ["HOME"]
|
||||||
inner_home = outer_home
|
inner_home = outer_home
|
||||||
@ -84,10 +97,12 @@ def expand_user_volumes (volumes):
|
|||||||
user = os.environ["USER"]
|
user = os.environ["USER"]
|
||||||
image_name = os.environ.get("OW_IMAGE", IMAGE_NAME)
|
image_name = os.environ.get("OW_IMAGE", IMAGE_NAME)
|
||||||
container_name = os.environ.get("OW_CONTAINER", f"{CONTAINER_NAME}_{user}")
|
container_name = os.environ.get("OW_CONTAINER", f"{CONTAINER_NAME}_{user}")
|
||||||
|
build_path = None
|
||||||
|
|
||||||
command = sys.argv[1:]
|
command = sys.argv[1:]
|
||||||
command_user = user
|
command_user = user
|
||||||
user_volumes = USER_VOLUMES
|
user_volumes = USER_VOLUMES
|
||||||
|
pull = False
|
||||||
|
|
||||||
quiet = False
|
quiet = False
|
||||||
while len(command) > 0:
|
while len(command) > 0:
|
||||||
@ -109,9 +124,15 @@ while len(command) > 0:
|
|||||||
elif arg.startswith("--image="):
|
elif arg.startswith("--image="):
|
||||||
image_name = arg[len("--image="):]
|
image_name = arg[len("--image="):]
|
||||||
command = command[1:]
|
command = command[1:]
|
||||||
|
elif arg.startswith("--build="):
|
||||||
|
build_path = arg[len("--build="):]
|
||||||
|
command = command[1:]
|
||||||
elif arg.startswith("--volume="):
|
elif arg.startswith("--volume="):
|
||||||
user_volumes.append(arg[len("--volume="):])
|
user_volumes.append(arg[len("--volume="):])
|
||||||
command = command[1:]
|
command = command[1:]
|
||||||
|
elif arg == "--pull":
|
||||||
|
command = command[1:]
|
||||||
|
pull = True
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
|
||||||
@ -122,6 +143,12 @@ container = None
|
|||||||
try:
|
try:
|
||||||
container = docker_inspect(container_name)[0]
|
container = docker_inspect(container_name)[0]
|
||||||
except Exception:
|
except Exception:
|
||||||
|
build_path = resolve_build_path(build_path)
|
||||||
|
if build_path is not None:
|
||||||
|
image_name = docker_build(build_path, ["--pull"] if pull else [], f"image_{container_name}")
|
||||||
|
elif pull:
|
||||||
|
docker_pull(image_name)
|
||||||
|
|
||||||
if not quiet:
|
if not quiet:
|
||||||
print(f">> Creating container: {container_name} (using image: {image_name})")
|
print(f">> Creating container: {container_name} (using image: {image_name})")
|
||||||
docker_create(
|
docker_create(
|
||||||
|
2
test_image/Dockerfile
Normal file
2
test_image/Dockerfile
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
FROM debian:stable
|
||||||
|
RUN echo 'test' > /root/test
|
Loading…
x
Reference in New Issue
Block a user