dind 3.0 KB

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