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