Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tcnksm
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcnksm/6319aeb3cfc20cb9877d to your computer and use it in GitHub Desktop.
Save tcnksm/6319aeb3cfc20cb9877d to your computer and use it in GitHub Desktop.

no

$ docker run --restart=no busybox /bin/sh -c 'date; exit 1'
Wed Sep 17 08:13:15 UTC 2014
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                     PORTS               NAMES
e3389974b4ef        busybox:latest      "/bin/sh -c 'date; e   5 seconds ago       Exited (1) 4 seconds ago                       jolly_hoover

on-failure

$ docker run --restart=on-failure busybox /bin/sh -c 'date; exit 1' 
Wed Sep 17 08:15:38 UTC 2014
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                         PORTS               NAMES
4fd4e22ecb35        busybox:latest      "/bin/sh -c 'date; e   4 seconds ago       Restarting (1) 1 seconds ago                       trusting_wilson
$ docker log 4fd4e22ecb35
Wed Sep 17 08:15:38 UTC 2014
Wed Sep 17 08:15:39 UTC 2014
Wed Sep 17 08:15:39 UTC 2014
Wed Sep 17 08:15:41 UTC 2014
Wed Sep 17 08:15:42 UTC 2014
Wed Sep 17 08:15:46 UTC 2014
Wed Sep 17 08:15:53 UTC 2014
// resetMonitor resets the stateful fields on the containerMonitor based on the
 // previous runs success or failure.  Reguardless of success, if the container had
 // an execution time of more than 10s then reset the timer back to the default
 func (m *containerMonitor) resetMonitor(successful bool) {
     executionTime := time.Now().Sub(m.lastStartTime).Seconds()

     if executionTime > 10 {
         m.timeIncrement = defaultTimeIncrement
     } else {
         // otherwise we need to increment the amount of time we wait before restarting
         // the process.  We will build up by multiplying the increment by 2
         m.timeIncrement *= 2
     }

     // the container exited successfully so we need to reset the failure counter
     if successful {
         m.failureCount = 0
     } else {
         m.failureCount++
     }
 }

on-failure:5

docker run --restart=on-failure:5 busybox /bin/sh -c 'date; exit 1'
Wed Sep 17 08:51:05 UTC 2014

$ docker ps -a                                                                                       ➜  ~/src/github.com/tcnksm/docker-1.2
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                                  PORTS               NAMES
c5a86fc5e242        busybox:latest      "/bin/sh -c 'date; e   2 seconds ago       Restarting (1) Less than a second ago                       cocky_hawking

$ docker logs c5a86fc5e242
Wed Sep 17 08:51:05 UTC 2014
Wed Sep 17 08:51:06 UTC 2014
Wed Sep 17 08:51:07 UTC 2014
Wed Sep 17 08:51:08 UTC 2014
Wed Sep 17 08:51:10 UTC 2014

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                      PORTS               NAMES
c5a86fc5e242        busybox:latest      "/bin/sh -c 'date; e   29 seconds ago      Exited (1) 24 seconds ago                       cocky_hawking

exit 0だと再起動しない

$ docker run --restart=on-failure busybox /bin/sh -c 'date; exit 0'
Wed Sep 17 08:54:21 UTC 2014

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                     PORTS               NAMES
17849c6ee53b        busybox:latest      "/bin/sh -c 'date; e   4 seconds ago       Exited (0) 3 seconds ago                       grave_babbage

always

docker run --restart=always busybox /bin/sh -c 'date; exit 0'
Wed Sep 17 08:57:34 UTC 2014

docker run --restart=always busybox /bin/sh -c 'date; exit 1'
Wed Sep 17 08:57:37 UTC 2014

docker ps -a                                                                                       ➜  ~/src/github.com/tcnksm/docker-1.2
CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                         PORTS               NAMES
19df55aec365        busybox:latest      "/bin/sh -c 'date; e   4 seconds ago       Restarting (1) 1 seconds ago                       drunk_ritchie
a158cdffcaae        busybox:latest      "/bin/sh -c 'date; e   8 seconds ago       Restarting (0) 3 seconds ago                       naughty_mclean
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment