docker 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: docker
  4. # Required-Start: $syslog $remote_fs
  5. # Required-Stop: $syslog $remote_fs
  6. # Should-Start: cgroupfs-mount cgroup-lite
  7. # Should-Stop: cgroupfs-mount cgroup-lite
  8. # Default-Start: 2 3 4 5
  9. # Default-Stop: 0 1 6
  10. # Short-Description: Create lightweight, portable, self-sufficient containers.
  11. # Description:
  12. # Docker is an open-source project to easily create lightweight, portable,
  13. # self-sufficient containers from any application. The same container that a
  14. # developer builds and tests on a laptop can run at scale, in production, on
  15. # VMs, bare metal, OpenStack clusters, public clouds and more.
  16. ### END INIT INFO
  17. export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
  18. BASE=$(basename $0)
  19. # modify these in /etc/default/$BASE (/etc/default/docker)
  20. DOCKER=/usr/bin/$BASE
  21. # This is the pid file managed by docker itself
  22. DOCKER_PIDFILE=/var/run/$BASE.pid
  23. # This is the pid file created/managed by start-stop-daemon
  24. DOCKER_SSD_PIDFILE=/var/run/$BASE-ssd.pid
  25. DOCKER_LOGFILE=/var/log/$BASE.log
  26. DOCKER_OPTS=
  27. DOCKER_DESC="Docker"
  28. # Get lsb functions
  29. . /lib/lsb/init-functions
  30. if [ -f /etc/default/$BASE ]; then
  31. . /etc/default/$BASE
  32. fi
  33. # see also init_is_upstart in /lib/lsb/init-functions (which isn't available in Ubuntu 12.04, or we'd use it)
  34. if [ -x /sbin/initctl ] && /sbin/initctl version 2>/dev/null | grep -q upstart; then
  35. log_failure_msg "$DOCKER_DESC is managed via upstart, try using service $BASE $1"
  36. exit 1
  37. fi
  38. # Check docker is present
  39. if [ ! -x $DOCKER ]; then
  40. log_failure_msg "$DOCKER not present or not executable"
  41. exit 1
  42. fi
  43. fail_unless_root() {
  44. if [ "$(id -u)" != '0' ]; then
  45. log_failure_msg "$DOCKER_DESC must be run as root"
  46. exit 1
  47. fi
  48. }
  49. cgroupfs_mount() {
  50. # see also https://github.com/tianon/cgroupfs-mount/blob/master/cgroupfs-mount
  51. if grep -v '^#' /etc/fstab | grep -q cgroup \
  52. || [ ! -e /proc/cgroups ] \
  53. || [ ! -d /sys/fs/cgroup ]; then
  54. return
  55. fi
  56. if ! mountpoint -q /sys/fs/cgroup; then
  57. mount -t tmpfs -o uid=0,gid=0,mode=0755 cgroup /sys/fs/cgroup
  58. fi
  59. (
  60. cd /sys/fs/cgroup
  61. for sys in $(awk '!/^#/ { if ($4 == 1) print $1 }' /proc/cgroups); do
  62. mkdir -p $sys
  63. if ! mountpoint -q $sys; then
  64. if ! mount -n -t cgroup -o $sys cgroup $sys; then
  65. rmdir $sys || true
  66. fi
  67. fi
  68. done
  69. )
  70. }
  71. case "$1" in
  72. start)
  73. fail_unless_root
  74. cgroupfs_mount
  75. touch "$DOCKER_LOGFILE"
  76. chgrp docker "$DOCKER_LOGFILE"
  77. log_begin_msg "Starting $DOCKER_DESC: $BASE"
  78. start-stop-daemon --start --background \
  79. --no-close \
  80. --exec "$DOCKER" \
  81. --pidfile "$DOCKER_SSD_PIDFILE" \
  82. --make-pidfile \
  83. -- \
  84. -d -p "$DOCKER_PIDFILE" \
  85. $DOCKER_OPTS \
  86. >> "$DOCKER_LOGFILE" 2>&1
  87. log_end_msg $?
  88. ;;
  89. stop)
  90. fail_unless_root
  91. log_begin_msg "Stopping $DOCKER_DESC: $BASE"
  92. start-stop-daemon --stop --pidfile "$DOCKER_SSD_PIDFILE"
  93. log_end_msg $?
  94. ;;
  95. restart)
  96. fail_unless_root
  97. docker_pid=`cat "$DOCKER_SSD_PIDFILE" 2>/dev/null`
  98. [ -n "$docker_pid" ] \
  99. && ps -p $docker_pid > /dev/null 2>&1 \
  100. && $0 stop
  101. $0 start
  102. ;;
  103. force-reload)
  104. fail_unless_root
  105. $0 restart
  106. ;;
  107. status)
  108. status_of_proc -p "$DOCKER_SSD_PIDFILE" "$DOCKER" docker
  109. ;;
  110. *)
  111. echo "Usage: $0 {start|stop|restart|status}"
  112. exit 1
  113. ;;
  114. esac
  115. exit 0