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.
This commit is contained in:
Adrian Kuschelyagi Malacoda 2020-08-19 02:45:38 -05:00
parent 8185ab8876
commit 2aec5cfc3b
2 changed files with 22 additions and 2 deletions

View File

@ -29,8 +29,11 @@ Runs command as root, instead of current user.
### --rm ### --rm
Removes the specified container. Removes the specified container.
### --commit
Commits the container to an image named `image_$CONTAINER_NAME`.
### --build=<path> ### --build=<path>
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> ### --volume=<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. 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.

View File

@ -33,6 +33,11 @@ USER_VOLUMES = ["~/otherworld"]
BACKGROUND_COMMAND = None BACKGROUND_COMMAND = None
DEFAULT_COMMAND = ["bash"] 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): def docker_inspect (container):
return json.loads(check_output(["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): def docker_exec (container, command, user):
call(["docker", "exec", "--user", user, "-ti", container] + command) call(["docker", "exec", "--user", user, "-ti", container] + command)
def docker_commit (container, image_name):
call(["docker", "commit", container, image_name])
def docker_rm (container): def docker_rm (container):
call(["docker", "rm", "-f", container]) call(["docker", "rm", "-f", container])
@ -119,12 +127,21 @@ while len(command) > 0:
print(f">> Removing container: {container_name}") print(f">> Removing container: {container_name}")
docker_rm(container_name) docker_rm(container_name)
sys.exit(0) 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": elif arg == "--sudo":
command_user = "root" command_user = "root"
command = command[1:] command = command[1:]
elif arg.startswith("--container="): elif arg.startswith("--container="):
container_name = arg[len("--container="):] container_name = arg[len("--container="):]
command = command[1:] command = command[1:]
elif arg.startswith("--image"):
image_name = get_generated_image_name(container_name)
command = command[1:]
elif arg.startswith("--image="): elif arg.startswith("--image="):
image_name = arg[len("--image="):] image_name = arg[len("--image="):]
command = command[1:] command = command[1:]
@ -149,7 +166,7 @@ try:
except Exception: except Exception:
build_path = resolve_build_path(build_path) build_path = resolve_build_path(build_path)
if build_path is not None: 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: elif pull:
docker_pull(image_name) docker_pull(image_name)