You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.1 KiB

  1. #!/bin/sh
  2. #
  3. # Usage:
  4. # restart-container.sh <container_name_1> <container_name_2> <container_name_n...>
  5. #
  6. # The design idea was derived from docker.sh by acmesh-official
  7. #
  8. # https://github.com/acmesh-official/acme.sh/blob/5b8d7a3/deploy/docker.sh
  9. #
  10. DOCKER_DAEMON_SOCKET="/var/run/docker.sock"
  11. _get_container_id_by_name() {
  12. local container_name="$1"
  13. curl --silent --unix-socket "$DOCKER_DAEMON_SOCKET" -X GET "http://localhost/containers/json?filters=%7B%22name%22%3A%5B%22${container_name}%22%5D%7D" |
  14. tr '{,' '\n' |
  15. grep -i '"id":' |
  16. head -n 1 |
  17. cut -d '"' -f 4
  18. }
  19. _restart_container_by_id() {
  20. local container_name="$1"
  21. local container_id="$(_get_container_id_by_name $container_name)"
  22. curl --silent --unix-socket "$DOCKER_DAEMON_SOCKET" -X POST http://localhost/containers/${container_id}/restart
  23. }
  24. _restart_container_by_name() {
  25. local container_name="$1"
  26. curl --silent --unix-socket "$DOCKER_DAEMON_SOCKET" -X POST http://localhost/containers/${container_name}/restart
  27. }
  28. for container_name in "$@"; do
  29. _restart_container_by_name "$container_name"
  30. done