dind-systemd 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/bin/bash
  2. set -e
  3. container=docker
  4. export container
  5. if [ $# -eq 0 ]; then
  6. echo >&2 'ERROR: No command specified. You probably want to run `journalctl -f`, or maybe `bash`?'
  7. exit 1
  8. fi
  9. if [ ! -t 0 ]; then
  10. echo >&2 'ERROR: TTY needs to be enabled (`docker run -t ...`).'
  11. exit 1
  12. fi
  13. # Change mount propagation to shared, which SystemD PID 1 would normally do
  14. # itself when started by the kernel. SystemD skips that when it detects it is
  15. # running in a container.
  16. mount --make-rshared /
  17. env > /etc/docker-entrypoint-env
  18. cat > /etc/systemd/system/docker-entrypoint.target << EOF
  19. [Unit]
  20. Description=the target for docker-entrypoint.service
  21. Requires=docker-entrypoint.service systemd-logind.service systemd-user-sessions.service
  22. EOF
  23. quoted_args="$(printf " %q" "${@}")"
  24. echo "${quoted_args}" > /etc/docker-entrypoint-cmd
  25. cat > /etc/systemd/system/docker-entrypoint.service << EOF
  26. [Unit]
  27. Description=docker-entrypoint.service
  28. [Service]
  29. ExecStart=/bin/bash -exc "source /etc/docker-entrypoint-cmd"
  30. # EXIT_STATUS is either an exit code integer or a signal name string, see systemd.exec(5)
  31. ExecStopPost=/bin/bash -ec "if echo \${EXIT_STATUS} | grep [A-Z] > /dev/null; then echo >&2 \"got signal \${EXIT_STATUS}\"; systemctl exit \$(( 128 + \$( kill -l \${EXIT_STATUS} ) )); else systemctl exit \${EXIT_STATUS}; fi"
  32. StandardInput=tty-force
  33. StandardOutput=inherit
  34. StandardError=inherit
  35. WorkingDirectory=$(pwd)
  36. EnvironmentFile=/etc/docker-entrypoint-env
  37. [Install]
  38. WantedBy=multi-user.target
  39. EOF
  40. systemctl mask systemd-firstboot.service systemd-udevd.service
  41. systemctl unmask systemd-logind
  42. systemctl enable docker-entrypoint.service
  43. systemd=
  44. if [ -x /lib/systemd/systemd ]; then
  45. systemd=/lib/systemd/systemd
  46. elif [ -x /usr/lib/systemd/systemd ]; then
  47. systemd=/usr/lib/systemd/systemd
  48. elif [ -x /sbin/init ]; then
  49. systemd=/sbin/init
  50. else
  51. echo >&2 'ERROR: systemd is not installed'
  52. exit 1
  53. fi
  54. systemd_args="--show-status=false --unit=docker-entrypoint.target"
  55. echo "$0: starting $systemd $systemd_args"
  56. exec $systemd $systemd_args