From 2aec5cfc3bbfe0b2f71db8cb7c89d73a22f87e47 Mon Sep 17 00:00:00 2001 From: Adrian Malacoda Date: Wed, 19 Aug 2020 02:45:38 -0500 Subject: [PATCH] Add ability to commit the otherworld container to an image. The image is named the same as in --build option. Also add an --image option (without argument) that uses this image. Not documented because I'm not sure I like this interface. --- README.md | 5 ++++- otherworld | 19 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 059ec31..9089cc6 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,11 @@ Runs command as root, instead of current user. ### --rm Removes the specified container. +### --commit +Commits the container to an image named `image_$CONTAINER_NAME`. + ### --build= -Path to build context to build the image from. This overrides `--image`/`OW_IMAGE`. +Path to build context to build the image from. This overrides `--image`/`OW_IMAGE`. The resulting image is named `image_$CONTAINER_NAME` ### --volume= Volume to map to the container. `~` is expanded automatically. If the directory exists in the user's home directory it will be created to ensure it has the correct ownership and permissions. diff --git a/otherworld b/otherworld index ad0b0c7..c1bda04 100644 --- a/otherworld +++ b/otherworld @@ -33,6 +33,11 @@ USER_VOLUMES = ["~/otherworld"] BACKGROUND_COMMAND = None DEFAULT_COMMAND = ["bash"] +GENERATED_IMAGE_NAME_TEMPLATE = "image_{container_name}" + +def get_generated_image_name (container_name): + return GENERATED_IMAGE_NAME_TEMPLATE.format(container_name=container_name) + def docker_inspect (container): return json.loads(check_output(["docker", "inspect", container])) @@ -58,6 +63,9 @@ def docker_start (container): def docker_exec (container, command, user): call(["docker", "exec", "--user", user, "-ti", container] + command) +def docker_commit (container, image_name): + call(["docker", "commit", container, image_name]) + def docker_rm (container): call(["docker", "rm", "-f", container]) @@ -119,12 +127,21 @@ while len(command) > 0: print(f">> Removing container: {container_name}") docker_rm(container_name) sys.exit(0) + elif arg == "--commit": + image_name = get_generated_image_name(container_name) + if not quiet: + print(f">> Committing container: {container_name} to image: {image_name}") + docker_commit(container_name, image_name) + sys.exit(0) elif arg == "--sudo": command_user = "root" command = command[1:] 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:] @@ -149,7 +166,7 @@ try: 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}") + image_name = docker_build(build_path, ["--pull"] if pull else [], get_generated_image_name(container_name)) elif pull: docker_pull(image_name)