File: docker.sh

package info (click to toggle)
docker.io 27.5.1%2Bdfsg4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,384 kB
  • sloc: sh: 5,847; makefile: 1,146; ansic: 664; python: 162; asm: 133
file content (102 lines) | stat: -rwxr-xr-x 2,040 bytes parent folder | download | duplicates (10)
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
#!/bin/bash

# Make it easy to build a docker image aimed at building the docker
# package, and the build the package, either as unprivileged user or
# as root. The idea with building as root is that it allows to run
# more unit tests.
#
# Copyright: Arnaud Rebillout <elboulangero@gmail.com>
# License: GPL-3+

set -e
set -u

DOCKER=docker
APT_PROXY=
IMAGE_TAG=${IMAGE_TAG:-debian/sid/docker-builder}
ACNG_PORT=${ACNG_PORT:-3142}

CMD=${1:-}
shift || :


## misc helpers

fail() {
    echo >&2 "$@"
    exit 1
}

user_in_docker_group() {
    id -Gn | grep -q '\bdocker\b'
}

apt_cacher_ng_running() {
    command -v nc >/dev/null 2>&1 || return 1
    nc -z localhost $1
}


## docker helpers

docker_build() {
    local opts=()

    [ "$APT_PROXY" ] && \
        opts+=("--build-arg" "http_proxy=$APT_PROXY")

    $DOCKER build \
        "${opts[@]}" \
        -t $IMAGE_TAG \
        .
}

docker_run_as_user() {
    $DOCKER run -it --rm \
        -u $(id -u):$(id -g) \
        -v /etc/group:/etc/group:ro \
        -v /etc/passwd:/etc/passwd:ro \
        -v $(pwd)/..:/usr/src/ \
        -w /usr/src/$(basename $(pwd)) \
        $IMAGE_TAG \
        "$@"
}

docker_run_as_root() {
    $DOCKER run -it --rm \
        --privileged \
        -v $(pwd)/..:/usr/src \
        -w /usr/src/$(basename $(pwd)) \
        $IMAGE_TAG \
        "$@"
}


## main

[ -d debian ] || \
    fail "No 'debian' directory. Please run from the root of the source tree"

if ! user_in_docker_group; then
    DOCKER='sudo docker'
    echo "You are not part of the docker group, running docker with '$DOCKER'"
fi

if apt_cacher_ng_running $ACNG_PORT; then
    APT_PROXY="http://172.17.0.1:$ACNG_PORT"
    echo "Detected local apt proxy, using $APT_PROXY as container proxy"
fi

case "$CMD" in
    (build)
        cd debian
        docker_build ;;
    (run-user)
        docker_run_as_user "$@" ;;
    (run-root)
        docker_run_as_root "$@" ;;
    (*)
        fail "Usage: $(basename $0) build | run-user [CMD] | run-root [CMD]" ;;
esac

# vim: et sts=4 sw=4