github.com/jandre/docker@v1.7.0/contrib/init/sysvinit-debian/docker (about)

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