docker 3.6 KB

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