$ docker run --restart=no busybox /bin/sh -c 'date; exit 1'Wed Sep 17 08:13:15 UTC 2014
123
$ 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
これは終了ステータスがnon-zeroの場合に再起動し続ける.
12
$ docker run --restart=on-failure busybox /bin/sh -c 'date; exit 1'Wed Sep 17 08:15:38 UTC 2014
123
$ 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
ログを見ると,バックグランドで再起動し続けてるのがわかる.
12345678
$ 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
on-failure:X
on-failureは再起動の回数を制限することができる.例えば5回にしてみる.
12
$ docker run --restart=on-failure:5 busybox /bin/sh -c 'date; exit 1'Wed Sep 17 08:51:05 UTC 2014
123
$ docker ps -a
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
ログを見る.
123456
$ 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
5回再起動した後にステータスを見るとコンテナが終了しているのが確認できる.
123
$ 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
always
終了コードが何であっても起動し続ける.
12
$ docker run --restart=always busybox /bin/sh -c 'date; exit 0'Wed Sep 17 08:57:34 UTC 2014
12
$ docker run --restart=always busybox /bin/sh -c 'date; exit 1'Wed Sep 17 08:57:37 UTC 2014
1234
$ docker ps -a
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_ritchiea158cdffcaae busybox:latest "/bin/sh -c 'date; e 8 seconds ago Restarting (0) 3 seconds ago naughty_mclean
// 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 defaultfunc(m*containerMonitor)resetMonitor(successfulbool){executionTime:=time.Now().Sub(m.lastStartTime).Seconds()ifexecutionTime>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 2m.timeIncrement*=2}// the container exited successfully so we need to reset the failure counterifsuccessful{m.failureCount=0}else{m.failureCount++}}