docker-lib.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Based on: https://github.com/concourse/docker-image-resource/blob/master/assets/common.sh
  2. DOCKER_LOG_FILE=${DOCKER_LOG_FILE:-/tmp/docker.log}
  3. SKIP_PRIVILEGED=${SKIP_PRIVILEGED:-false}
  4. STARTUP_TIMEOUT=${STARTUP_TIMEOUT:-120}
  5. sanitize_cgroups() {
  6. mkdir -p /sys/fs/cgroup
  7. mountpoint -q /sys/fs/cgroup || \
  8. mount -t tmpfs -o uid=0,gid=0,mode=0755 cgroup /sys/fs/cgroup
  9. mount -o remount,rw /sys/fs/cgroup
  10. sed -e 1d /proc/cgroups | while read sys hierarchy num enabled; do
  11. if [ "$enabled" != "1" ]; then
  12. # subsystem disabled; skip
  13. continue
  14. fi
  15. grouping="$(cat /proc/self/cgroup | cut -d: -f2 | grep "\\<$sys\\>")" || true
  16. if [ -z "$grouping" ]; then
  17. # subsystem not mounted anywhere; mount it on its own
  18. grouping="$sys"
  19. fi
  20. mountpoint="/sys/fs/cgroup/$grouping"
  21. mkdir -p "$mountpoint"
  22. # clear out existing mount to make sure new one is read-write
  23. if mountpoint -q "$mountpoint"; then
  24. umount "$mountpoint"
  25. fi
  26. mount -n -t cgroup -o "$grouping" cgroup "$mountpoint"
  27. if [ "$grouping" != "$sys" ]; then
  28. if [ -L "/sys/fs/cgroup/$sys" ]; then
  29. rm "/sys/fs/cgroup/$sys"
  30. fi
  31. ln -s "$mountpoint" "/sys/fs/cgroup/$sys"
  32. fi
  33. done
  34. if ! test -e /sys/fs/cgroup/systemd ; then
  35. mkdir /sys/fs/cgroup/systemd
  36. mount -t cgroup -o none,name=systemd none /sys/fs/cgroup/systemd
  37. fi
  38. }
  39. start_docker() {
  40. mkdir -p /var/log
  41. mkdir -p /var/run
  42. if [ "$SKIP_PRIVILEGED" = "false" ]; then
  43. sanitize_cgroups
  44. # check for /proc/sys being mounted readonly, as systemd does
  45. if grep '/proc/sys\s\+\w\+\s\+ro,' /proc/mounts >/dev/null; then
  46. mount -o remount,rw /proc/sys
  47. fi
  48. fi
  49. local mtu=$(cat /sys/class/net/$(ip route get 8.8.8.8|awk '{ print $5 }')/mtu)
  50. local server_args="--mtu ${mtu}"
  51. local registry=""
  52. server_args="${server_args}"
  53. for registry in $3; do
  54. server_args="${server_args} --insecure-registry ${registry}"
  55. done
  56. if [ -n "$4" ]; then
  57. server_args="${server_args} --registry-mirror $4"
  58. fi
  59. try_start() {
  60. dockerd --data-root /scratch/docker ${server_args} >$DOCKER_LOG_FILE 2>&1 &
  61. echo $! > /tmp/docker.pid
  62. sleep 1
  63. echo waiting for docker to come up...
  64. until docker info >/dev/null 2>&1; do
  65. sleep 1
  66. if ! kill -0 "$(cat /tmp/docker.pid)" 2>/dev/null; then
  67. return 1
  68. fi
  69. done
  70. }
  71. export server_args DOCKER_LOG_FILE
  72. declare -fx try_start
  73. if ! timeout ${STARTUP_TIMEOUT} bash -ce 'while true; do try_start && break; done'; then
  74. echo Docker failed to start within ${STARTUP_TIMEOUT} seconds.
  75. return 1
  76. fi
  77. }