Skip to content

Commit e1ee7c1

Browse files
committed
introduce host_docker label to expose docker socket in containers
passes docker socket, group, and envars through to container
1 parent c164379 commit e1ee7c1

3 files changed

Lines changed: 64 additions & 19 deletions

File tree

lib.d/v1-runtime.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ v1-runtime(){
1818
# org.dockerland.dex.docker_home=~ (user's actual home)
1919
# org.dockerland.dex.docker_volumes=/etc/hosts:/etc/hosts:ro
2020
# org.dockerland.dex.docker_workspace=/ (host root as /dex/workspace)
21+
# org.dockerland.dex.host_docker=rw (expose host's docker socket and passthru docker vars)
2122
# org.dockerland.dex.host_paths=rw (rw mount host HOME and CWD)
2223
# org.dockerland.dex.host_users=ro (ro mount host /etc/passwd|group)
2324
# org.dockerland.dex.window=yes (applies window/X11 flags)
@@ -29,12 +30,13 @@ v1-runtime(){
2930
__docker_home=$DEX_IMAGE_NAME-$__tag
3031
__docker_workspace=$DEX_HOST_PWD
3132
__docker_volumes=
33+
__host_docker=
3234
__host_paths="ro"
3335
__host_users=
3436
__window=
3537

3638
# augment defaults with image meta
37-
for label in api docker_devices docker_envars docker_flags docker_groups docker_home docker_workspace docker_volumes host_paths host_users window ; do
39+
for label in api docker_devices docker_envars docker_flags docker_groups docker_home docker_workspace docker_volumes host_docker host_paths host_users window ; do
3840
# @TODO reduce this to a single docker inspect command
3941
val=$(__local_docker inspect --format "{{ index .Config.Labels \"org.dockerland.dex.$label\" }}" $__image)
4042
[ -z "$val" ] && continue
@@ -139,6 +141,15 @@ v1-runtime(){
139141
__docker_volumes+=" /etc/passwd:/etc/passwd:$__host_users /etc/group:/etc/group:$__host_users"
140142
esac
141143

144+
# map host docker socket and passthru docker vars
145+
case $(echo "$__host_docker" | awk '{print tolower($0)}') in rw|ro)
146+
docker_socket=/var/run/docker.sock
147+
docker_group=$(if [[ "$OSTYPE" == darwin* ]] || [[ "$OSTYPE" == macos* ]]; then stat -f '%Dg' $docker_socket ; else stat -c '%g' $docker_socket ; fi)
148+
__docker_volumes+=" $docker_socket:/var/run/docker.sock:$__host_docker $DOCKER_CERT_PATH $MACHINE_STORAGE_PATH"
149+
__docker_flags+=" --group-add=$docker_group"
150+
__docker_envars+=" DOCKER_* MACHINE_STORAGE_PATH"
151+
esac
152+
142153
# mount specicified devices (only if they exist)
143154
for path in $__docker_devices; do
144155
[ "${path:0:5}" = "/dev/" ] || path="/dev/$path"

tests/bats/06-runtime.bats

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ teardown(){
101101
[[ "$output" == *"TEST_B=test"* ]]
102102
}
103103

104-
105104
@test "runtime sets a unique home by default (DEX_HOME/homes/<image>-<tag>)" {
106105
rm -rf $DEX_HOME/homes/debian-latest
107106

@@ -110,22 +109,6 @@ teardown(){
110109
[ -d $DEX_HOME/homes/debian-latest ]
111110
}
112111

113-
@test "runtime ro-mounts host paths to coax common absolute path resolutions" {
114-
cd $TMPDIR
115-
$DEX run imgtest/debian ls $TMPDIR
116-
117-
run $DEX run imgtest/labels:disable-host_paths ls $TMPDIR
118-
[ $status -eq 2 ]
119-
}
120-
121-
@test "runtime respects ro-mounting of host users/groups" {
122-
run $DEX run imgtest/debian whoami
123-
[ $status -eq 1 ]
124-
125-
run $DEX run imgtest/labels:enable-host_users whoami
126-
[ $status -eq 0 ]
127-
}
128-
129112
@test "runtime respects docker_envars label" {
130113
# imgtest/labels image ::
131114
# LABEL org.dockerland.dex.docker_envars="BATS_TESTVAR"
@@ -145,7 +128,6 @@ teardown(){
145128
[ $status -eq 0 ]
146129
}
147130

148-
149131
@test "runtime expands ~ as real \$HOME in labels" {
150132
# imgtest/labels:home image ::
151133
# LABEL org.dockerland.dex.docker_home="~"
@@ -227,6 +209,40 @@ teardown(){
227209
[ $status -eq 1 ]
228210
}
229211

212+
@test "runtime ro-mounts host paths to coax common absolute path resolutions" {
213+
cd $TMPDIR
214+
$DEX run imgtest/debian ls $TMPDIR
215+
216+
run $DEX run imgtest/labels:disable-host_paths ls $TMPDIR
217+
[ $status -eq 2 ]
218+
}
219+
220+
@test "runtime respects host_users label for ro-mounting of host users/groups" {
221+
run $DEX run imgtest/debian whoami
222+
[ $status -eq 1 ]
223+
224+
run $DEX run imgtest/labels:enable-host_users whoami
225+
[ $status -eq 0 ]
226+
}
227+
228+
@test "runtime respects host_docker label for passthrough of host docker socket and vars" {
229+
# test if host docker is [NOT!] exposed by default
230+
run $DEX run imgtest/debian ls -l /var/run/docker.sock
231+
[ $status -eq 2 ]
232+
233+
run $DEX run imgtest/labels:enable-host_docker ls -l /var/run/docker.sock
234+
[ $status -eq 0 ]
235+
236+
# test polling of host docker
237+
run $DEX run imgtest/labels:enable-host_docker docker info
238+
[ $status -eq 0 ]
239+
240+
# test DOCKER_ envar passthrough
241+
run DOCKER_TEST="test" $DEX run imgtest/labels:enable-host_docker
242+
[[ $output == *"DOCKER_TEST=test"* ]]
243+
}
244+
245+
230246
@test "runtime suppresses tty flags when container output is piped" {
231247
# imgtest/labels image ::
232248
# LABEL dockerland.dex.docker_flags="--tty -e TESTVAR=TEST"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM alpine:3.4
2+
3+
RUN apk add --no-cache \
4+
docker
5+
6+
#
7+
# v1 dex-api
8+
#
9+
10+
LABEL \
11+
org.dockerland.dex.api="v1" \
12+
org.dockerland.dex.host_docker=rw
13+
14+
#
15+
# debian image
16+
#
17+
18+
CMD echo "ALPINE_RELEASE=$(cat /etc/alpine-release)" ; printenv

0 commit comments

Comments
 (0)