Compare commits

..

2 Commits

Author SHA1 Message Date
77935b49e8 Add --env option that sets environment variables in the launched command (not in the container) 2020-08-19 02:48:47 -05:00
2aec5cfc3b 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.
2020-08-19 02:45:38 -05:00
2 changed files with 38 additions and 5 deletions

View File

@ -29,12 +29,18 @@ 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>
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 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.
### --env=<key>=<value>
Set the environment variable `key` to `value` in the command. **Note that this does not set environment variables in the container itself**.
## Environment variables
### OW_IMAGE
Image to create the container with (default: `debian:stable`)

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python3
from subprocess import call, check_call, check_output
from time import sleep
from itertools import chain
import os
import sys
import json
@ -33,6 +34,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]))
@ -55,8 +61,13 @@ def docker_create_user (container, user, uid=None):
def docker_start (container):
call(["docker", "start", container])
def docker_exec (container, command, user):
call(["docker", "exec", "--user", user, "-ti", container] + command)
def docker_exec (container, command, user, env={}):
call(["docker", "exec", "--user", user, "-ti"] + \
list(chain.from_iterable([["--env", f"{kv[0]}={kv[1]}"] for kv in env.items()])) + \
[container] + command)
def docker_commit (container, image_name):
call(["docker", "commit", container, image_name])
def docker_rm (container):
call(["docker", "rm", "-f", container])
@ -108,6 +119,8 @@ command_user = user
user_volumes = USER_VOLUMES
pull = False
command_env = {}
quiet = False
while len(command) > 0:
arg = command[0]
@ -119,12 +132,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:]
@ -134,6 +156,11 @@ while len(command) > 0:
elif arg.startswith("--volume="):
user_volumes.append(arg[len("--volume="):])
command = command[1:]
elif arg.startswith("--env="):
env_pair = arg[len("--env="):]
(key, value) = env_pair.split("=", 1)
command_env[key] = value
command = command[1:]
elif arg == "--pull":
command = command[1:]
pull = True
@ -149,7 +176,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)
@ -178,4 +205,4 @@ except Exception:
if not quiet:
print(f">> Welcome to {container_name} (IP {container['NetworkSettings']['IPAddress']})")
docker_exec(container_name, command, command_user)
docker_exec(container_name, command, command_user, command_env)