From 6a266323bf039ef5b06b67193ff2ed987b81e96c Mon Sep 17 00:00:00 2001 From: Adrian Malacoda Date: Sat, 17 Oct 2020 22:54:27 -0500 Subject: [PATCH] tag generated container with `otherworldowner` label so we know which containers are ours --- otherworld | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/otherworld b/otherworld index a1c55dd..8b2782b 100755 --- a/otherworld +++ b/otherworld @@ -8,6 +8,7 @@ import json CONTAINER_NAME = "otherworld" DEFAULT_IMAGE_NAME = "debian:stable" +OTHERWORLD_LABEL = "otherworldowner" def create_x11_mapping (): home = os.environ["HOME"] @@ -29,7 +30,7 @@ def create_x11_mapping (): "--shm-size", "2g", "--privileged"] -DOCKER_CREATE_OPTIONS = ["-t"] + create_x11_mapping() +DOCKER_CREATE_OPTIONS = ["-t"] USER_VOLUMES = ["~/otherworld"] BACKGROUND_COMMAND = None DEFAULT_COMMAND = ["bash"] @@ -52,8 +53,14 @@ def get_generated_image_name (container_name): def docker_inspect (container): return json.loads(check_output(["docker", "inspect", container])) -def docker_create (image, container, options, command): - docker_command = ["docker", "create", "--name", container] + options + [image] +def docker_list_containers (filter_argument): + return check_output(["docker", "ps", "-a", "--filter", filter_argument, "--format", "{{.Names}}"]).decode().strip().split("\n") + +def docker_create (image, container, options, command, labels={}): + docker_command = ["docker", "create", "--name", container] + options + if labels: + docker_command = docker_command + list(chain.from_iterable([["--label", f"{kv[0]}={kv[1]}"] for kv in labels.items()])) + docker_command.append(image) if command: docker_command = docker_command + command call(docker_command) @@ -240,8 +247,8 @@ except Exception: docker_create( image_name, container_name, - DOCKER_CREATE_OPTIONS + expand_user_volumes(user_volumes), - BACKGROUND_COMMAND + DOCKER_CREATE_OPTIONS + create_x11_mapping() + expand_user_volumes(user_volumes), + BACKGROUND_COMMAND, { OTHERWORLD_LABEL: user } ) container = docker_inspect(container_name)[0]