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