-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·138 lines (120 loc) · 4.57 KB
/
run.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env bash
DOCKER_REPOSITORY_NAME="rubensa"
DOCKER_IMAGE_NAME="ubuntu-tini-x11"
DOCKER_IMAGE_TAG="latest"
# Get current user UID
USER_ID=$(id -u)
# Get current user main GUID
GROUP_ID=$(id -g)
prepare_docker_timezone() {
# https://www.waysquare.com/how-to-change-docker-timezone/
ENV_VARS+=" --env=TZ=$(cat /etc/timezone)"
}
prepare_docker_user_and_group() {
RUNNER+=" --user=${USER_ID}:${GROUP_ID}"
}
prepare_docker_from_docker() {
MOUNTS+=" --mount type=bind,source=/var/run/docker.sock,target=/var/run/docker-host.sock"
}
prepare_docker_dbus_host_sharing() {
# To access DBus you ned to start a container without an AppArmor profile
SECURITY+=" --security-opt apparmor:unconfined"
# https://github.com/mviereck/x11docker/wiki/How-to-connect-container-to-DBus-from-host
# User DBus
MOUNTS+=" --mount type=bind,source=${XDG_RUNTIME_DIR}/bus,target=${XDG_RUNTIME_DIR}/bus"
# System DBus
MOUNTS+=" --mount type=bind,source=/run/dbus/system_bus_socket,target=/run/dbus/system_bus_socket"
# User DBus unix socket
# Prevent "gio:" "operation not supported" when running "xdg-open https://rubensa.eu.org"
ENV_VARS+=" --env=DBUS_SESSION_BUS_ADDRESS=/dev/null"
}
prepare_docker_xdg_runtime_dir_host_sharing() {
# XDG_RUNTIME_DIR defines the base directory relative to which user-specific non-essential runtime files and other file objects (such as sockets, named pipes, ...) should be stored.
MOUNTS+=" --mount type=bind,source=${XDG_RUNTIME_DIR},target=${XDG_RUNTIME_DIR}"
# XDG_RUNTIME_DIR
ENV_VARS+=" --env=XDG_RUNTIME_DIR=${XDG_RUNTIME_DIR}"
}
prepare_docker_sound_host_sharing() {
# Sound device (ALSA - Advanced Linux Sound Architecture - support)
[ -d /dev/snd ] && DEVICES+=" --device /dev/snd"
# Pulseaudio unix socket (needs XDG_RUNTIME_DIR support)
MOUNTS+=" --mount type=bind,source=${XDG_RUNTIME_DIR}/pulse,target=${XDG_RUNTIME_DIR}/pulse,readonly"
# https://github.com/TheBiggerGuy/docker-pulseaudio-example/issues/1
ENV_VARS+=" --env=PULSE_SERVER=unix:${XDG_RUNTIME_DIR}/pulse/native"
RUNNER_GROUPS+=" --group-add audio"
}
prepare_docker_webcam_host_sharing() {
# Allow webcam access
for device in /dev/video*
do
if [[ -c $device ]]; then
DEVICES+=" --device $device"
fi
done
RUNNER_GROUPS+=" --group-add video"
}
prepare_docker_gpu_host_sharing() {
# GPU support (Direct Rendering Manager)
[ -d /dev/dri ] && DEVICES+=" --device /dev/dri"
# VGA Arbiter
[ -c /dev/vga_arbiter ] && DEVICES+=" --device /dev/vga_arbiter"
# Allow nvidia devices access
for device in /dev/nvidia*
do
if [[ -c $device ]]; then
DEVICES+=" --device $device"
fi
done
}
prepare_docker_printer_host_sharing() {
# CUPS (https://github.com/mviereck/x11docker/wiki/CUPS-printer-in-container)
MOUNTS+=" --mount type=bind,source=/run/cups/cups.sock,target=/run/cups/cups.sock"
ENV_VARS+=" --env CUPS_SERVER=/run/cups/cups.sock"
}
prepare_docker_ipc_host_sharing() {
# Allow shared memory to avoid RAM access failures and rendering glitches due to X extension MIT-SHM
EXTRA+=" --ipc=host"
}
prepare_docker_x11_host_sharing() {
# X11 Unix-domain socket
MOUNTS+=" --mount type=bind,source=/tmp/.X11-unix,target=/tmp/.X11-unix"
ENV_VARS+=" --env=DISPLAY=${DISPLAY}"
# Credentials in cookies used by xauth for authentication of X sessions
MOUNTS+=" --mount type=bind,source=${XAUTHORITY},target=${XAUTHORITY}"
ENV_VARS+=" --env=XAUTHORITY=${XAUTHORITY}"
}
prepare_docker_hostname_host_sharing() {
# Using host hostname allows gnome-shell windows grouping
EXTRA+=" --hostname `hostname`"
}
prepare_docker_nvidia_drivers_install() {
# NVidia propietary drivers are needed on host for this to work
if [ `command -v nvidia-smi` ]; then
NVIDIA_VERSION=$(nvidia-smi --query-gpu=driver_version --format=csv,noheader)
# On run, if you specify NVIDIA_VERSION the nvidia specified drivers version are installed
ENV_VARS+=" --env=NVIDIA_VERSION=${NVIDIA_VERSION}"
fi
}
prepare_docker_timezone
prepare_docker_user_and_group
prepare_docker_from_docker
prepare_docker_dbus_host_sharing
prepare_docker_xdg_runtime_dir_host_sharing
prepare_docker_sound_host_sharing
prepare_docker_webcam_host_sharing
prepare_docker_gpu_host_sharing
prepare_docker_printer_host_sharing
prepare_docker_ipc_host_sharing
prepare_docker_x11_host_sharing
prepare_docker_hostname_host_sharing
prepare_docker_nvidia_drivers_install
docker run --rm -it \
--name "${DOCKER_IMAGE_NAME}" \
${SECURITY} \
${ENV_VARS} \
${DEVICES} \
${MOUNTS} \
${EXTRA} \
${RUNNER} \
${RUNNER_GROUPS} \
"${DOCKER_REPOSITORY_NAME}/${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}" "$@"