dind 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/bin/sh
  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@docker.com>
  5. # See the blog post: https://www.docker.com/blog/docker-can-now-run-within-docker/
  6. #
  7. # This script should be executed inside a docker container in privileged 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. #
  12. # Set the container env-var, so that AppArmor is enabled in the daemon and
  13. # containerd when running docker-in-docker.
  14. #
  15. # see: https://github.com/containerd/containerd/blob/787943dc1027a67f3b52631e084db0d4a6be2ccc/pkg/apparmor/apparmor_linux.go#L29-L45
  16. # see: https://github.com/moby/moby/commit/de191e86321f7d3136ff42ff75826b8107399497
  17. export container=docker
  18. # Allow AppArmor to work inside the container;
  19. #
  20. # aa-status
  21. # apparmor filesystem is not mounted.
  22. # apparmor module is loaded.
  23. #
  24. # mount -t securityfs none /sys/kernel/security
  25. #
  26. # aa-status
  27. # apparmor module is loaded.
  28. # 30 profiles are loaded.
  29. # 30 profiles are in enforce mode.
  30. # /snap/snapd/18357/usr/lib/snapd/snap-confine
  31. # ...
  32. #
  33. # Note: https://0xn3va.gitbook.io/cheat-sheets/container/escaping/sensitive-mounts#sys-kernel-security
  34. #
  35. # ## /sys/kernel/security
  36. #
  37. # In /sys/kernel/security mounted the securityfs interface, which allows
  38. # configuration of Linux Security Modules. This allows configuration of
  39. # AppArmor policies, and so access to this may allow a container to disable
  40. # its MAC system.
  41. #
  42. # Given that we're running privileged already, this should not be an issue.
  43. if [ -d /sys/kernel/security ] && ! mountpoint -q /sys/kernel/security; then
  44. mount -t securityfs none /sys/kernel/security || {
  45. echo >&2 'Could not mount /sys/kernel/security.'
  46. echo >&2 'AppArmor detection and --privileged mode might break.'
  47. }
  48. fi
  49. # Mount /tmp (conditionally)
  50. if ! mountpoint -q /tmp; then
  51. mount -t tmpfs none /tmp
  52. fi
  53. # cgroup v2: enable nesting
  54. if [ -f /sys/fs/cgroup/cgroup.controllers ]; then
  55. # move the processes from the root group to the /init group,
  56. # otherwise writing subtree_control fails with EBUSY.
  57. # An error during moving non-existent process (i.e., "cat") is ignored.
  58. mkdir -p /sys/fs/cgroup/init
  59. xargs -rn1 < /sys/fs/cgroup/cgroup.procs > /sys/fs/cgroup/init/cgroup.procs || :
  60. # enable controllers
  61. sed -e 's/ / +/g' -e 's/^/+/' < /sys/fs/cgroup/cgroup.controllers \
  62. > /sys/fs/cgroup/cgroup.subtree_control
  63. fi
  64. # Change mount propagation to shared to make the environment more similar to a
  65. # modern Linux system, e.g. with SystemD as PID 1.
  66. mount --make-rshared /
  67. if [ $# -gt 0 ]; then
  68. exec "$@"
  69. fi
  70. echo >&2 'ERROR: No command specified.'
  71. echo >&2 'You probably want to run hack/make.sh, or maybe a shell?'