From d0adb3b0ed637cd15697a69460ff8079287a39f2 Mon Sep 17 00:00:00 2001 From: Adrian Malacoda Date: Thu, 20 Aug 2020 02:37:44 -0500 Subject: [PATCH] Make default image name `image_{CONTAINER_NAME}`, and use `debian:stable` as fallback. --- README.md | 2 +- otherworld | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) mode change 100644 => 100755 otherworld diff --git a/README.md b/README.md index 8d8bb84..0084c4a 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ These are arguments that go between `otherworld` and the command. Name to assign to container (default: `otherworld_$USER`) ### --image= -Image to create the container with (default: `debian:stable`) +Image to create the container with (default: `image_$CONTAINER_NAME`, or `debian:stable` if that image does not exist) ### --sudo Runs command as root, instead of current user. diff --git a/otherworld b/otherworld old mode 100644 new mode 100755 index eb7f0ec..4114ff7 --- a/otherworld +++ b/otherworld @@ -7,7 +7,7 @@ import sys import json CONTAINER_NAME = "otherworld" -IMAGE_NAME = "debian:stable" +DEFAULT_IMAGE_NAME = "debian:stable" def create_x11_mapping (): home = os.environ["HOME"] @@ -110,7 +110,7 @@ def expand_user_volumes (volumes): user = os.environ["USER"] uid = int(check_output(["id", "-u"]).strip()) -image_name = os.environ.get("OW_IMAGE", IMAGE_NAME) +image_name = os.environ.get("OW_IMAGE", None) container_name = os.environ.get("OW_CONTAINER", f"{CONTAINER_NAME}_{user}") build_path = None @@ -144,9 +144,6 @@ while len(command) > 0: elif arg.startswith("--container="): container_name = arg[len("--container="):] command = command[1:] - elif arg.startswith("--image"): - image_name = get_generated_image_name(container_name) - command = command[1:] elif arg.startswith("--image="): image_name = arg[len("--image="):] command = command[1:] @@ -177,8 +174,20 @@ 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 [], get_generated_image_name(container_name)) - elif pull: - docker_pull(image_name) + else: + if image_name is None: + image_name = get_generated_image_name(container_name) + try: + docker_inspect(image_name) + if not quiet: + print(f">> Using default otherworld image {image_name}") + except Exception: + if not quiet: + print(f">> Default otherworld image {image_name} not found, falling back to {DEFAULT_IMAGE_NAME}") + image_name = DEFAULT_IMAGE_NAME + + if pull: + docker_pull(image_name) if not quiet: print(f">> Creating container: {container_name} (using image: {image_name})")