wrapdocker 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/bin/bash
  2. # Adapted from https://github.com/jpetazzo/dind
  3. # First, make sure that cgroups are mounted correctly.
  4. CGROUP=/sys/fs/cgroup
  5. : {LOG:=stdio}
  6. [ -d $CGROUP ] ||
  7. mkdir $CGROUP
  8. mountpoint -q $CGROUP ||
  9. mount -n -t tmpfs -o uid=0,gid=0,mode=0755 cgroup $CGROUP || {
  10. echo "Could not make a tmpfs mount. Did you use --privileged?"
  11. exit 1
  12. }
  13. if [ -d /sys/kernel/security ] && ! mountpoint -q /sys/kernel/security
  14. then
  15. mount -t securityfs none /sys/kernel/security || {
  16. echo "Could not mount /sys/kernel/security."
  17. echo "AppArmor detection and --privileged mode might break."
  18. }
  19. fi
  20. # Mount the cgroup hierarchies exactly as they are in the parent system.
  21. for SUBSYS in $(cut -d: -f2 /proc/1/cgroup)
  22. do
  23. [ -d $CGROUP/$SUBSYS ] || mkdir $CGROUP/$SUBSYS
  24. mountpoint -q $CGROUP/$SUBSYS ||
  25. mount -n -t cgroup -o $SUBSYS cgroup $CGROUP/$SUBSYS
  26. # The two following sections address a bug which manifests itself
  27. # by a cryptic "lxc-start: no ns_cgroup option specified" when
  28. # trying to start containers withina container.
  29. # The bug seems to appear when the cgroup hierarchies are not
  30. # mounted on the exact same directories in the host, and in the
  31. # container.
  32. # Named, control-less cgroups are mounted with "-o name=foo"
  33. # (and appear as such under /proc/<pid>/cgroup) but are usually
  34. # mounted on a directory named "foo" (without the "name=" prefix).
  35. # Systemd and OpenRC (and possibly others) both create such a
  36. # cgroup. To avoid the aforementioned bug, we symlink "foo" to
  37. # "name=foo". This shouldn't have any adverse effect.
  38. echo $SUBSYS | grep -q ^name= && {
  39. NAME=$(echo $SUBSYS | sed s/^name=//)
  40. ln -s $SUBSYS $CGROUP/$NAME
  41. }
  42. # Likewise, on at least one system, it has been reported that
  43. # systemd would mount the CPU and CPU accounting controllers
  44. # (respectively "cpu" and "cpuacct") with "-o cpuacct,cpu"
  45. # but on a directory called "cpu,cpuacct" (note the inversion
  46. # in the order of the groups). This tries to work around it.
  47. [ $SUBSYS = cpuacct,cpu ] && ln -s $SUBSYS $CGROUP/cpu,cpuacct
  48. done
  49. # Note: as I write those lines, the LXC userland tools cannot setup
  50. # a "sub-container" properly if the "devices" cgroup is not in its
  51. # own hierarchy. Let's detect this and issue a warning.
  52. grep -q :devices: /proc/1/cgroup ||
  53. echo "WARNING: the 'devices' cgroup should be in its own hierarchy."
  54. grep -qw devices /proc/1/cgroup ||
  55. echo "WARNING: it looks like the 'devices' cgroup is not mounted."
  56. # Now, close extraneous file descriptors.
  57. pushd /proc/self/fd >/dev/null
  58. for FD in *
  59. do
  60. case "$FD" in
  61. # Keep stdin/stdout/stderr
  62. [012])
  63. ;;
  64. # Nuke everything else
  65. *)
  66. eval exec "$FD>&-"
  67. ;;
  68. esac
  69. done
  70. popd >/dev/null
  71. if [ "$DOCKER_VERSION" == "" ]; then
  72. DOCKER_VERSION="1.5.0"
  73. fi
  74. ln -s "/usr/local/bin/docker-$DOCKER_VERSION" "/usr/local/bin/docker"
  75. # If a pidfile is still around (for example after a container restart),
  76. # delete it so that docker can start.
  77. rm -rf /var/run/docker.pid
  78. docker -d $DOCKER_DAEMON_ARGS &>/var/log/docker.log &
  79. >&2 echo "Waiting for Docker to start..."
  80. while ! docker ps &>/dev/null; do
  81. sleep 1
  82. done
  83. exec "$@"