dind 3.1 KB

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