Compare commits
2 Commits
8185ab8876
...
77935b49e8
Author | SHA1 | Date | |
---|---|---|---|
77935b49e8 | |||
2aec5cfc3b |
@ -29,12 +29,18 @@ 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.
|
||||||
|
|
||||||
|
### --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
|
## Environment variables
|
||||||
### OW_IMAGE
|
### OW_IMAGE
|
||||||
Image to create the container with (default: `debian:stable`)
|
Image to create the container with (default: `debian:stable`)
|
||||||
|
35
otherworld
35
otherworld
@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
from subprocess import call, check_call, check_output
|
from subprocess import call, check_call, check_output
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
from itertools import chain
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import json
|
import json
|
||||||
@ -33,6 +34,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]))
|
||||||
|
|
||||||
@ -55,8 +61,13 @@ def docker_create_user (container, user, uid=None):
|
|||||||
def docker_start (container):
|
def docker_start (container):
|
||||||
call(["docker", "start", container])
|
call(["docker", "start", container])
|
||||||
|
|
||||||
def docker_exec (container, command, user):
|
def docker_exec (container, command, user, env={}):
|
||||||
call(["docker", "exec", "--user", user, "-ti", container] + command)
|
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):
|
def docker_rm (container):
|
||||||
call(["docker", "rm", "-f", container])
|
call(["docker", "rm", "-f", container])
|
||||||
@ -108,6 +119,8 @@ command_user = user
|
|||||||
user_volumes = USER_VOLUMES
|
user_volumes = USER_VOLUMES
|
||||||
pull = False
|
pull = False
|
||||||
|
|
||||||
|
command_env = {}
|
||||||
|
|
||||||
quiet = False
|
quiet = False
|
||||||
while len(command) > 0:
|
while len(command) > 0:
|
||||||
arg = command[0]
|
arg = command[0]
|
||||||
@ -119,12 +132,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:]
|
||||||
@ -134,6 +156,11 @@ while len(command) > 0:
|
|||||||
elif arg.startswith("--volume="):
|
elif arg.startswith("--volume="):
|
||||||
user_volumes.append(arg[len("--volume="):])
|
user_volumes.append(arg[len("--volume="):])
|
||||||
command = command[1:]
|
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":
|
elif arg == "--pull":
|
||||||
command = command[1:]
|
command = command[1:]
|
||||||
pull = True
|
pull = True
|
||||||
@ -149,7 +176,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)
|
||||||
|
|
||||||
@ -178,4 +205,4 @@ except Exception:
|
|||||||
|
|
||||||
if not quiet:
|
if not quiet:
|
||||||
print(f">> Welcome to {container_name} (IP {container['NetworkSettings']['IPAddress']})")
|
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user