Docker - container - restart policy


Policies
  • always
  • unless-stopped
  • on-failure

Always
  1. Always restart
  2. Unless use command to stop it
  3. Restart when docker restart, even it has been stopped by command
Ex. docker container run -it {name} --restart always
Ex. After exit the shell, container will stop because the only one process was killed. But it will be restarted right away.
# Run container
Liaos-MBP:scg-control liaoisaac$ docker container run -it ubuntu /bin/bash

# There is only one process -> /bin/bash
root@1888da48ff01:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  1.3  0.1  18504  3352 pts/0    Ss   05:03   0:00 /bin/bash
root        11  0.0  0.1  34396  2912 pts/0    R+   05:03   0:00 ps aux
root@1888da48ff01:/# exit
exit

# Once exit, container will be stopped because there is only one process in container
Liaos-MBP:scg-control liaoisaac$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
Liaos-MBP:scg-control liaoisaac$

# Specify --restart always after “run"
Liaos-MBP:scg-control liaoisaac$ docker container run --restart always -it ubuntu /bin/bash
root@912e47da3142:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.5  0.1  18504  3444 pts/0    Ss   05:11   0:00 /bin/bash
root        10  0.0  0.1  34396  2808 pts/0    R+   05:12   0:00 ps aux
root@912e47da3142:/# exit
exit

# Once exit, container will be restarted right away
Liaos-MBP:scg-control liaoisaac$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
912e47da3142        ubuntu              "/bin/bash"         17 seconds ago      Up 2 seconds                            hungry_khorana
Liaos-MBP:scg-control liaoisaac$


Liaos-MBP:scg-control liaoisaac$ docker stop 912e47da3142
912e47da3142
Liaos-MBP:scg-control liaoisaac$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

# After restart docker in Mac, applications will be restarted
Liaos-MBP:scg-control liaoisaac$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
912e47da3142        ubuntu              "/bin/bash"         2 days ago          Up 13 seconds                           hungry_khoran

# After docker restart, container will be restarted again
# Following is ubuntu example
isaac@isaac-KVM:~$ service docker restart
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to restart 'docker.service'.
Authenticating as: isaac,,, (isaac)
Password:
==== AUTHENTICATION COMPLETE ===
isaac@isaac-KVM:~$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
e4403fafa5bb        ubuntu              "/bin/bash"         3 minutes ago       Up 5 seconds                            optimistic_leh                                             mann




unless-stopped
  1. Always restart unless it is stopped by command
  2. Wont restart when daemon restart
  3. Ex. — restart unless-stopped
# Start bash with unless-stopped
isaac@isaac-KVM:~$ docker container run --restart unless-stopped -it ubuntu /bin/bash
root@3be8ac8498a7:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  4.8  0.0  18504  3536 pts/0    Ss   17:53   0:00 /bin/bash
root        10  0.0  0.0  34396  2936 pts/0    R+   17:53   0:00 ps aux
root@3be8ac8498a7:/# exit
exit

# Kill the only one process, container stopped and restart automatically because restart policy is unless-stopped
isaac@isaac-KVM:~$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
3be8ac8498a7        ubuntu              "/bin/bash"         19 seconds ago      Up 7 seconds                            nostalgic_gagarin

# Use command to stop a container
isaac@isaac-KVM:~$ docker container stop 3be8ac8498a7
3be8ac8498a7
isaac@isaac-KVM:~$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

# Container is not started after docker restart because restart policy is unless-stopped
isaac@isaac-KVM:~$ sudo service docker restart
isaac@isaac-KVM:~$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
isaac@isaac-KVM:~$



on-failure
  1. Restart container when it's stopped with non-zero exit code
  2. Restart container when docker restart

沒有留言:

張貼留言

別名演算法 Alias Method

 題目 每個伺服器支援不同的 TPM (transaction per minute) 當 request 來的時候, 系統需要馬上根據 TPM 的能力隨機找到一個適合的 server. 雖然稱為 "隨機", 但還是需要有 TPM 作為權重. 解法 別名演算法...