install_docker.sh 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/bash
  2. docker_file_dir="$(pwd)/docker"
  3. function get_docker_install() {
  4. (cd "${docker_file_dir}" && chmod +x docker_off-line_install.sh && ./docker_off-line_install.sh -f docker-26.1.0.tgz >/dev/null 2>&1 && systemctl start docker >/dev/null 2>&1 && systemctl enable docker >/dev/null 2>&1)
  5. }
  6. function check_docker_status() {
  7. [ ! -z "$(docker ps 2>/dev/null)" ]
  8. }
  9. function check_compose_status() {
  10. [ ! -z "$(docker compose 2>/dev/null)" ]
  11. }
  12. function check_docker_compose_status() {
  13. [ ! -z "$(docker-compose 2>/dev/null)" ]
  14. }
  15. function get_compose_install() {
  16. echo "try to install docker compose service"
  17. (DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker} && mkdir -p $DOCKER_CONFIG/cli-plugins && cp "${docker_file_dir}/docker-compose" $DOCKER_CONFIG/cli-plugins/docker-compose && chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose)
  18. (check_compose_status || get_docker_compose_install)
  19. }
  20. function get_docker_compose_install() {
  21. (cp "${docker_file_dir}/docker-compose" /usr/local/bin/ && chmod +x /usr/local/bin/docker-compose && ln -sf /usr/local/bin/docker-compose /usr/bin/docker-compose)
  22. (check_docker_compose_status || echo "Please install docker compose yourself")
  23. }
  24. function check_docker_compose_install() {
  25. check_compose_status || (check_docker_compose_status || get_compose_install)
  26. echo "The docker compose service is running normally"
  27. }
  28. function check_docker_install() {
  29. # check if "docker.tar.gz" exists, if not, echo an error message
  30. [ ! -f "${docker_file_dir}/docker.tar.gz" ] && echo "Error: ${docker_file_dir}/docker.tar.gz does not exist" && exit 1
  31. # decompress "docker.tar.gz" into the current directory
  32. tar -zxf "docker.tar.gz" --directory "$(pwd)"
  33. if [ -x "$(command -v 'docker')" ]; then
  34. check_docker_status || (systemctl start docker 2>/dev/null || service docker start 2>/dev/null)
  35. check_docker_status || (get_docker_install )
  36. check_docker_status && echo "The docker service is running normally" || echo "Please install docker yourself"
  37. else
  38. echo "It is detected that the system does not have docker service installed, try to install docker service"
  39. get_docker_install
  40. check_docker_status && echo "The docker service is running normally" || echo "Please install docker yourself"
  41. fi
  42. }
  43. # check if the script is run as root
  44. if [ "$(id -u)" != "0" ]; then
  45. echo "Error: This script must be run as root" 1>&2
  46. exit 1
  47. fi
  48. # check if the script is run in the current directory
  49. if [ "$(pwd)" != "$(dirname "$0")" ]; then
  50. echo "Error: This script must be run in the current directory" 1>&2
  51. exit 1
  52. fi
  53. # run the script
  54. check_docker_install
  55. check_docker_compose_install