Skip to content

Commit a59db7f

Browse files
committed
Add initial version
1 parent 2d3639f commit a59db7f

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-0
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# EditorConfig is awesome: https://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
[*]
7+
indent_style = space
8+
indent_size = 2
9+
end_of_line = lf
10+
charset = utf-8
11+
trim_trailing_whitespace = true
12+
insert_final_newline = true

.shellcheckrc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
enable=add-default-case
2+
enable=avoid-nullary-conditions
3+
enable=check-extra-masked-returns
4+
enable=check-set-e-suppressed
5+
enable=check-unassigned-uppercase
6+
enable=deprecate-which
7+
enable=quote-safe-variables
8+
enable=require-double-brackets
9+
enable=require-variable-braces

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
FROM felipecrs/fixdockergid:latest
2+
3+
USER root
4+
5+
ARG DOCKER_PATH="/usr/bin/docker"
6+
RUN mv -f "${DOCKER_PATH}" "${DOCKER_PATH}.orig"
7+
COPY docker "${DOCKER_PATH}"
8+
9+
USER rootless
10+
11+
# Create fixtures
12+
RUN echo test | tee /home/rootless/only-inside-container

docker

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#!/bin/bash
2+
3+
set -euxo pipefail
4+
5+
function get_container_id() {
6+
local cpuset_output
7+
cpuset_output=$(head -1 /proc/self/cpuset)
8+
basename "${cpuset_output}"
9+
}
10+
11+
function get_container_root_dir() {
12+
local container_id="${1}"
13+
14+
docker.orig inspect --format '{{.GraphDriver.Data.MergedDir}}' "${container_id}"
15+
}
16+
17+
function set_container_volumes() {
18+
local container_id="${1}"
19+
20+
local docker_output
21+
docker_output=$(
22+
docker.orig inspect \
23+
--format '{{range .Mounts}}{{printf "%s:%s\n" .Source .Destination}}{{end}}' \
24+
"${container_id}"
25+
)
26+
27+
readarray -t container_volumes <<<"${docker_output}"
28+
}
29+
30+
container_id="$(get_container_id)"
31+
container_root_dir="$(get_container_root_dir "${container_id}")"
32+
set_container_volumes "${container_id}"
33+
readonly container_id container_root_dir container_volumes
34+
35+
function fix_volume_arg() {
36+
local source="${volume_arg%%":"*}"
37+
local destination="${volume_arg#*":"}"
38+
39+
# Fix relative paths as docker needs absolute paths
40+
# if [[ "${source}" == "./"* ]]; then
41+
# source="${PWD}/${source#"./"}"
42+
# fi
43+
44+
if [[ "${source}" == "/"* ]]; then
45+
for container_volume in "${container_volumes[@]}"; do
46+
local container_volume_source="${container_volume%%":"*}"
47+
local container_volume_destination="${container_volume#*":"}"
48+
49+
if [[ "${source}" == "${container_volume_destination}" ]]; then
50+
volume_arg="${container_volume_source}:${destination}"
51+
return
52+
fi
53+
54+
if [[ "${source}" == "${container_volume_destination}/"* ]]; then
55+
volume_arg="${container_volume_source}${source#"${container_volume_destination}"}:${destination}"
56+
return
57+
fi
58+
59+
# Check if there is some container_volume mounted within the source
60+
# Example:
61+
# First container mounts --volume "${PWD}/testfile:/home/rootless/testfile"
62+
# Second container mounts --volume /home/rootless:/wd
63+
# Then the second container should also mount --volume "${PWD}/testfile:/wd/testfile" (extra_arg)
64+
if [[ "${container_volume_destination}" == "${source}/"* ]]; then
65+
# Convert /home/rootless/testfile (container_volume_destination) to /wd/testfile (destination path)
66+
extra_args+=(--volume "${container_volume_source}:${destination}/${container_volume_destination#"${source}/"}")
67+
fi
68+
done
69+
70+
volume_arg="${container_root_dir}${source}:${destination}"
71+
return
72+
fi
73+
74+
volume_arg="${source}:${destination}"
75+
}
76+
77+
original_args=("$@")
78+
fixed_args=()
79+
80+
extra_args=()
81+
fix_next_arg=false
82+
for arg in "${original_args[@]}"; do
83+
if [[ "${fix_next_arg}" == true ]]; then
84+
fix_next_arg=false
85+
volume_arg="${arg}"
86+
fix_volume_arg
87+
arg="${volume_arg}"
88+
elif [[ "${arg}" == "-v" || "${arg}" == "--volume" ]]; then
89+
fix_next_arg=true
90+
elif [[ "${arg}" == "-v="* || "${arg}" == "--volume="* ]]; then
91+
dot_dot_volume="${arg%%"="*}"
92+
volume_arg="${arg#*"="}"
93+
fix_volume_arg
94+
arg="${dot_dot_volume}=${volume_arg}"
95+
fi
96+
97+
fixed_args+=("${arg}" "${extra_args[@]}")
98+
extra_args=()
99+
done
100+
101+
exec docker.orig "${fixed_args[@]}"

test.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
3+
set -euxo pipefail
4+
5+
image_id="$(docker build --quiet .)"
6+
7+
# shellcheck disable=SC2312
8+
docker_args=(docker run --rm --user "$(id -u):$(id -g)" --volume /var/run/docker.sock:/var/run/docker.sock)
9+
10+
# Check if docker on docker is working
11+
"${docker_args[@]}" "${image_id}" docker version >/dev/null
12+
13+
# Check if mounting an volume from the container gets fixed
14+
"${docker_args[@]}" "${image_id}" docker run --rm --volume /home/rootless/only-inside-container:/only-inside-container ubuntu:latest grep "^test$" /only-inside-container >/dev/null
15+
"${docker_args[@]}" "${image_id}" docker run --rm --volume=/home/rootless/only-inside-container:/only-inside-container ubuntu:latest grep "^test$" /only-inside-container >/dev/null
16+
17+
# Check if mounting a volume which is already a volume gets fixed
18+
"${docker_args[@]}" --volume "${PWD}:/wd" "${image_id}" docker run --rm --volume /wd:/wd ubuntu:latest grep "^test$" /wd/testfile >/dev/null
19+
20+
# Same as above but for a file within the volume
21+
"${docker_args[@]}" --volume "${PWD}:/wd" "${image_id}" docker run --rm --volume /wd/testfile:/wd/testfile ubuntu:latest grep "^test$" /wd/testfile >/dev/null
22+
23+
# Volumes inside volumes
24+
"${docker_args[@]}" --volume "${PWD}/testfile:/home/rootless/testfile" "${image_id}" docker run --rm --volume /home/rootless:/wd ubuntu:latest grep "^test$" /wd/testfile >/dev/null
25+
"${docker_args[@]}" --volume "${PWD}/testfile:/home/rootless/testfile" --volume "${PWD}/testfile:/home/rootless/testfile2" "${image_id}" docker run --rm --volume /home/rootless:/wd ubuntu:latest bash -c 'grep "^test$" /wd/testfile && grep "^test$" /wd/testfile2 && grep "^test$" /wd/only-inside-container' >/dev/null

testfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test

0 commit comments

Comments
 (0)