docker 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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=$(basename $0)
  20. # modify these in /etc/default/$BASE (/etc/default/docker)
  21. DOCKER=/usr/bin/$BASE
  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. # see also init_is_upstart in /lib/lsb/init-functions (which isn't available in Ubuntu 12.04, or we'd use it)
  35. if [ -x /sbin/initctl ] && /sbin/initctl version 2>/dev/null | grep -q upstart; then
  36. log_failure_msg "$DOCKER_DESC is managed via upstart, try using service $BASE $1"
  37. exit 1
  38. fi
  39. # Check docker is present
  40. if [ ! -x $DOCKER ]; then
  41. log_failure_msg "$DOCKER not present or not executable"
  42. exit 1
  43. fi
  44. fail_unless_root() {
  45. if [ "$(id -u)" != '0' ]; then
  46. log_failure_msg "$DOCKER_DESC must be run as root"
  47. exit 1
  48. fi
  49. }
  50. cgroupfs_mount() {
  51. # see also https://github.com/tianon/cgroupfs-mount/blob/master/cgroupfs-mount
  52. if grep -v '^#' /etc/fstab | grep -q cgroup \
  53. || [ ! -e /proc/cgroups ] \
  54. || [ ! -d /sys/fs/cgroup ]; then
  55. return
  56. fi
  57. if ! mountpoint -q /sys/fs/cgroup; then
  58. mount -t tmpfs -o uid=0,gid=0,mode=0755 cgroup /sys/fs/cgroup
  59. fi
  60. (
  61. cd /sys/fs/cgroup
  62. for sys in $(awk '!/^#/ { if ($4 == 1) print $1 }' /proc/cgroups); do
  63. mkdir -p $sys
  64. if ! mountpoint -q $sys; then
  65. if ! mount -n -t cgroup -o $sys cgroup $sys; then
  66. rmdir $sys || true
  67. fi
  68. fi
  69. done
  70. )
  71. }
  72. case "$1" in
  73. start)
  74. fail_unless_root
  75. cgroupfs_mount
  76. touch "$DOCKER_LOGFILE"
  77. chgrp docker "$DOCKER_LOGFILE"
  78. ulimit -n 1048576
  79. if [ "$BASH" ]; then
  80. ulimit -u 1048576
  81. else
  82. ulimit -p 1048576
  83. fi
  84. log_begin_msg "Starting $DOCKER_DESC: $BASE"
  85. start-stop-daemon --start --background \
  86. --no-close \
  87. --exec "$DOCKER" \
  88. --pidfile "$DOCKER_SSD_PIDFILE" \
  89. --make-pidfile \
  90. -- \
  91. -d -p "$DOCKER_PIDFILE" \
  92. $DOCKER_OPTS \
  93. >> "$DOCKER_LOGFILE" 2>&1
  94. log_end_msg $?
  95. ;;
  96. stop)
  97. fail_unless_root
  98. log_begin_msg "Stopping $DOCKER_DESC: $BASE"
  99. start-stop-daemon --stop --pidfile "$DOCKER_SSD_PIDFILE"
  100. log_end_msg $?
  101. ;;
  102. restart)
  103. fail_unless_root
  104. docker_pid=`cat "$DOCKER_SSD_PIDFILE" 2>/dev/null`
  105. [ -n "$docker_pid" ] \
  106. && ps -p $docker_pid > /dev/null 2>&1 \
  107. && $0 stop
  108. $0 start
  109. ;;
  110. force-reload)
  111. fail_unless_root
  112. $0 restart
  113. ;;
  114. status)
  115. status_of_proc -p "$DOCKER_SSD_PIDFILE" "$DOCKER" "$DOCKER_DESC"
  116. ;;
  117. *)
  118. echo "Usage: $0 {start|stop|restart|status}"
  119. exit 1
  120. ;;
  121. esac