Policies:
- always
- unless-stopped
- on-failure
Always
- Always restart
- Unless use command to stop it
- 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
- Always restart unless it is stopped by command
- Wont restart when daemon restart
- 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
- Restart container when it's stopped with non-zero exit code
- Restart container when docker restart
沒有留言:
張貼留言