github.com/kaisenlinux/docker.io@v0.0.0-20230510090727-ea55db55fac7/debian/helpers/docker.sh (about)

     1  #!/bin/bash
     2  
     3  # Make it easy to build a docker image aimed at building the docker
     4  # package, and the build the package, either as unprivileged user or
     5  # as root. The idea with building as root is that it allows to run
     6  # more unit tests.
     7  #
     8  # Copyright: Arnaud Rebillout <elboulangero@gmail.com>
     9  # License: GPL-3+
    10  
    11  set -e
    12  set -u
    13  
    14  DOCKER=docker
    15  APT_PROXY=
    16  IMAGE_TAG=${IMAGE_TAG:-debian/sid/docker-builder}
    17  ACNG_PORT=${ACNG_PORT:-3142}
    18  
    19  CMD=${1:-}
    20  shift || :
    21  
    22  
    23  ## misc helpers
    24  
    25  fail() {
    26      echo >&2 "$@"
    27      exit 1
    28  }
    29  
    30  user_in_docker_group() {
    31      id -Gn | grep -q '\bdocker\b'
    32  }
    33  
    34  apt_cacher_ng_running() {
    35      command -v nc >/dev/null 2>&1 || return 1
    36      nc -z localhost $1
    37  }
    38  
    39  
    40  ## docker helpers
    41  
    42  docker_build() {
    43      local opts=()
    44  
    45      [ "$APT_PROXY" ] && \
    46          opts+=("--build-arg" "http_proxy=$APT_PROXY")
    47  
    48      $DOCKER build \
    49          "${opts[@]}" \
    50          -t $IMAGE_TAG \
    51          .
    52  }
    53  
    54  docker_run_as_user() {
    55      $DOCKER run -it --rm \
    56          -u $(id -u):$(id -g) \
    57          -v /etc/group:/etc/group:ro \
    58          -v /etc/passwd:/etc/passwd:ro \
    59          -v $(pwd)/..:/usr/src/ \
    60          -w /usr/src/$(basename $(pwd)) \
    61          $IMAGE_TAG \
    62          "$@"
    63  }
    64  
    65  docker_run_as_root() {
    66      $DOCKER run -it --rm \
    67          --privileged \
    68          -v $(pwd)/..:/usr/src \
    69          -w /usr/src/$(basename $(pwd)) \
    70          $IMAGE_TAG \
    71          "$@"
    72  }
    73  
    74  
    75  ## main
    76  
    77  [ -d debian ] || \
    78      fail "No 'debian' directory. Please run from the root of the source tree"
    79  
    80  if ! user_in_docker_group; then
    81      DOCKER='sudo docker'
    82      echo "You are not part of the docker group, running docker with '$DOCKER'"
    83  fi
    84  
    85  if apt_cacher_ng_running $ACNG_PORT; then
    86      APT_PROXY="http://172.17.0.1:$ACNG_PORT"
    87      echo "Detected local apt proxy, using $APT_PROXY as container proxy"
    88  fi
    89  
    90  case "$CMD" in
    91      (build)
    92          cd debian
    93          docker_build ;;
    94      (run-user)
    95          docker_run_as_user "$@" ;;
    96      (run-root)
    97          docker_run_as_root "$@" ;;
    98      (*)
    99          fail "Usage: $(basename $0) build | run-user [CMD] | run-root [CMD]" ;;
   100  esac
   101  
   102  # vim: et sts=4 sw=4