Skip to content

Commit a7b6fb8

Browse files
committed
added userdocker attach ... sub-command, allowing to re-attach to own containers
1 parent cc1874d commit a7b6fb8

6 files changed

Lines changed: 81 additions & 14 deletions

File tree

CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ New features:
2121
user_home.
2222
- userdocker version command will include userdocker version information.
2323
- Use of DOCKER_HOST env var will raise an ERROR.
24+
- Users can now re-attach to their previously started containers (in case of
25+
connection loss for example).
2426
- Improved support for nvidia-docker's NV_GPU env var, which is now checked
2527
against admin config options:
2628

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ Sample Usage:
5050
# (docker run) run a debian image with user (read-only) mounted home
5151
sudo userdocker run -it --rm -v $HOME:$HOME:ro debian bash
5252
53+
# (docker attach) re-attach to own container after connection loss
54+
sudo userdocker attach 438c7648e76b
55+
5356
# (docker ps) list running containers
5457
sudo userdocker ps
5558

userdocker/config/default.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
# The following allows you to specify which docker top level commands a user can
5757
# run at all (still restricted by the following settings):
5858
ALLOWED_SUBCOMMANDS = [
59+
'attach', # allows users to re-attach to _their_ containers
5960
'dockviz', # tree visualization of images
6061
'images',
6162
'load', # see RUN_PULL as well
@@ -78,6 +79,9 @@
7879
# The following arguments are available to the user for the given command:
7980
# (aliases are supported as tuples below, but not in ARGS_ALWAYS)
8081
ARGS_AVAILABLE = {
82+
'attach': [
83+
'--no-stdin',
84+
],
8185
'images': [
8286
('-a', '--all'),
8387
'--digests',
@@ -173,13 +177,6 @@
173177
]
174178
}
175179

176-
# By default userdocker will set USERDOCKER and USERDOCKER_USER ENV vars to the
177-
# container. They are used by userdocker internally and might in future be used
178-
# to allow users to interact with their previously started containers for
179-
# example. Only set to False if you know what you're doing and need a "cleaner"
180-
# env for some reason.
181-
ENV_VARS_SET_USERDOCKER_META_INFO = True
182-
183180

184181
# nvidia docker specific settings
185182
# The following settings allow to restrict the way users can use nvidia GPUs

userdocker/subcommands/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22

3+
from .attach import *
34
from .dockviz import *
45
from .images import *
56
from .ps import *

userdocker/subcommands/attach.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import json
4+
import logging
5+
6+
from ..config import uid
7+
from ..helpers.cmd import init_cmd
8+
from ..helpers.exceptions import UserDockerException
9+
from ..helpers.execute import exec_cmd
10+
from ..helpers.logger import logger
11+
from ..helpers.parser import init_subcommand_parser
12+
13+
14+
def parser_attach(parser):
15+
sub_parser = init_subcommand_parser(parser, 'attach')
16+
17+
sub_parser.add_argument(
18+
"--detach-keys",
19+
help="Override the key sequence for detaching a container",
20+
)
21+
22+
sub_parser.add_argument(
23+
"container",
24+
help="container's ID or name to attach to"
25+
)
26+
27+
28+
def exec_cmd_attach(args):
29+
cmd = init_cmd(args)
30+
31+
if args.detach_keys:
32+
cmd += ['--detach-keys', args.detach_keys]
33+
34+
container = args.container
35+
container_env = exec_cmd(
36+
[
37+
args.executor_path, 'inspect',
38+
'--format', '{{json .Config.Env}}',
39+
container
40+
],
41+
return_status=False,
42+
loglvl=logging.DEBUG,
43+
)
44+
if not container_env:
45+
raise UserDockerException(
46+
'ERROR: could not find container %s' % container
47+
)
48+
userdocker_uid_env = [
49+
env for env in json.loads(container_env)
50+
if env.startswith('USERDOCKER_UID')
51+
]
52+
if not userdocker_uid_env:
53+
raise UserDockerException(
54+
'ERROR: could not find USERDOCKER_UID env var in container %s'
55+
% container
56+
)
57+
userdocker_uid = int(userdocker_uid_env[0].split('USERDOCKER_UID=')[1])
58+
logger.debug(
59+
"Container %s was started by user id %d", container, userdocker_uid)
60+
if uid != userdocker_uid:
61+
raise UserDockerException(
62+
'ERROR: container %s was started by %d, but you are %d. Permission '
63+
'denied!' % (container, userdocker_uid, uid)
64+
)
65+
66+
exec_cmd(cmd, dry_run=args.dry_run)

userdocker/subcommands/run.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from ..config import CAPS_DROP
1313
from ..config import ENV_VARS
1414
from ..config import ENV_VARS_EXT
15-
from ..config import ENV_VARS_SET_USERDOCKER_META_INFO
1615
from ..config import NV_ALLOWED_GPUS
1716
from ..config import NV_DEFAULT_GPU_COUNT_RESERVATION
1817
from ..config import NV_MAX_GPU_COUNT_RESERVATION
@@ -214,12 +213,11 @@ def exec_cmd_run(args):
214213
prepare_nvidia_docker_run(args)
215214

216215
env_vars = ENV_VARS + ENV_VARS_EXT.get(args.executor, [])
217-
if ENV_VARS_SET_USERDOCKER_META_INFO:
218-
env_vars += [
219-
"USERDOCKER=%s" % __version__,
220-
"USERDOCKER_USER=%s" % user_name,
221-
"USERDOCKER_UID=%d" % uid,
222-
]
216+
env_vars += [
217+
"USERDOCKER=%s" % __version__,
218+
"USERDOCKER_USER=%s" % user_name,
219+
"USERDOCKER_UID=%d" % uid,
220+
]
223221
for env_var in env_vars:
224222
cmd += ['-e', env_var]
225223

0 commit comments

Comments
 (0)