docker-v5-install.sh 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #!/usr/bin/env bash
  2. # Copyright (c) 2021-2023 tteck
  3. # Author: tteck (tteckster)
  4. # License: MIT
  5. # https://github.com/tteck/Proxmox/raw/main/LICENSE
  6. if [ "$VERBOSE" = "yes" ]; then set -x; STD=""; else STD="silent"; fi
  7. silent() { "$@" > /dev/null 2>&1; }
  8. if [ "$DISABLEIPV6" == "yes" ]; then echo "net.ipv6.conf.all.disable_ipv6 = 1" >>/etc/sysctl.conf; $STD sysctl -p; fi
  9. YW=$(echo "\033[33m")
  10. RD=$(echo "\033[01;31m")
  11. BL=$(echo "\033[36m")
  12. GN=$(echo "\033[1;92m")
  13. CL=$(echo "\033[m")
  14. RETRY_NUM=10
  15. RETRY_EVERY=3
  16. CM="${GN}✓${CL}"
  17. CROSS="${RD}✗${CL}"
  18. BFR="\\r\\033[K"
  19. HOLD="-"
  20. set -Eeuo pipefail
  21. trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
  22. function error_handler() {
  23. local exit_code="$?"
  24. local line_number="$1"
  25. local command="$2"
  26. local error_message="${RD}[ERROR]${CL} in line ${RD}$line_number${CL}: exit code ${RD}$exit_code${CL}: while executing command ${YW}$command${CL}"
  27. echo -e "\n$error_message\n"
  28. }
  29. function msg_info() {
  30. local msg="$1"
  31. echo -ne " ${HOLD} ${YW}${msg}..."
  32. }
  33. function msg_ok() {
  34. local msg="$1"
  35. echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
  36. }
  37. function msg_error() {
  38. local msg="$1"
  39. echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
  40. }
  41. msg_info "Setting up Container OS "
  42. sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen
  43. locale-gen >/dev/null
  44. for ((i=RETRY_NUM; i>0; i--)); do
  45. if [ "$(hostname -I)" != "" ]; then
  46. break
  47. fi
  48. echo 1>&2 -en "${CROSS}${RD} No Network! "
  49. sleep $RETRY_EVERY
  50. done
  51. if [ "$(hostname -I)" = "" ]; then
  52. echo 1>&2 -e "\n${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
  53. echo -e " 🖧 Check Network Settings"
  54. exit 1
  55. fi
  56. msg_ok "Set up Container OS"
  57. msg_ok "Network Connected: ${BL}$(hostname -I)"
  58. set +e
  59. if ping -c 1 -W 1 1.1.1.1 &> /dev/null; then msg_ok "Internet Connected"; else
  60. msg_error "Internet NOT Connected"
  61. read -r -p "Would you like to continue anyway? <y/N> " prompt
  62. if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
  63. echo -e " ⚠️ ${RD}Expect Issues Without Internet${CL}"
  64. else
  65. echo -e " 🖧 Check Network Settings"
  66. exit 1
  67. fi
  68. fi
  69. RESOLVEDIP=$(getent hosts github.com | awk '{ print $1 }')
  70. if [[ -z "$RESOLVEDIP" ]]; then msg_error "DNS Lookup Failure"; else msg_ok "DNS Resolved github.com to ${BL}$RESOLVEDIP${CL}"; fi
  71. set -e
  72. msg_info "Updating Container OS"
  73. $STD apt-get update
  74. $STD apt-get -y upgrade
  75. msg_ok "Updated Container OS"
  76. msg_info "Installing Dependencies"
  77. $STD apt-get install -y curl
  78. $STD apt-get install -y sudo
  79. $STD apt-get install -y mc
  80. msg_ok "Installed Dependencies"
  81. get_latest_release() {
  82. curl -sL https://api.github.com/repos/$1/releases/latest | grep '"tag_name":' | cut -d'"' -f4
  83. }
  84. DOCKER_LATEST_VERSION=$(get_latest_release "moby/moby")
  85. PORTAINER_LATEST_VERSION=$(get_latest_release "portainer/portainer")
  86. DOCKER_COMPOSE_LATEST_VERSION=$(get_latest_release "docker/compose")
  87. msg_info "Installing Docker $DOCKER_LATEST_VERSION"
  88. DOCKER_CONFIG_PATH='/etc/docker/daemon.json'
  89. mkdir -p $(dirname $DOCKER_CONFIG_PATH)
  90. if [ "$ST" == "yes" ]; then
  91. VER=$(curl -s https://api.github.com/repos/containers/fuse-overlayfs/releases/latest | grep "tag_name" | awk '{print substr($2, 2, length($2)-3) }')
  92. cd /usr/local/bin
  93. curl -sSL -o fuse-overlayfs https://github.com/containers/fuse-overlayfs/releases/download/$VER/fuse-overlayfs-x86_64
  94. chmod 755 /usr/local/bin/fuse-overlayfs
  95. cd ~
  96. echo -e '{\n "storage-driver": "fuse-overlayfs",\n "log-driver": "journald"\n}' > /etc/docker/daemon.json
  97. else
  98. echo -e '{\n "log-driver": "journald"\n}' > /etc/docker/daemon.json
  99. fi
  100. $STD sh <(curl -sSL https://get.docker.com)
  101. msg_ok "Installed Docker $DOCKER_LATEST_VERSION"
  102. read -r -p "Would you like to add Portainer? <y/N> " prompt
  103. if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
  104. PORTAINER="Y"
  105. else
  106. PORTAINER="N"
  107. fi
  108. if [[ $PORTAINER == "Y" ]]; then
  109. msg_info "Installing Portainer $PORTAINER_LATEST_VERSION"
  110. docker volume create portainer_data >/dev/null
  111. $STD docker run -d \
  112. -p 8000:8000 \
  113. -p 9000:9000 \
  114. --name=portainer \
  115. --restart=always \
  116. -v /var/run/docker.sock:/var/run/docker.sock \
  117. -v portainer_data:/data \
  118. portainer/portainer-ce:latest
  119. msg_ok "Installed Portainer $PORTAINER_LATEST_VERSION"
  120. fi
  121. read -r -p "Would you like to add Docker Compose? <y/N> " prompt
  122. if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]; then
  123. DOCKER_COMPOSE="Y"
  124. else
  125. DOCKER_COMPOSE="N"
  126. fi
  127. if [[ $DOCKER_COMPOSE == "Y" ]]; then
  128. msg_info "Installing Docker Compose $DOCKER_COMPOSE_LATEST_VERSION"
  129. DOCKER_CONFIG=${DOCKER_CONFIG:-$HOME/.docker}
  130. mkdir -p $DOCKER_CONFIG/cli-plugins
  131. curl -sSL https://github.com/docker/compose/releases/download/$DOCKER_COMPOSE_LATEST_VERSION/docker-compose-linux-x86_64 -o ~/.docker/cli-plugins/docker-compose
  132. chmod +x $DOCKER_CONFIG/cli-plugins/docker-compose
  133. msg_ok "Installed Docker Compose $DOCKER_COMPOSE_LATEST_VERSION"
  134. fi
  135. echo "export TERM='xterm-256color'" >>/root/.bashrc
  136. echo -e "$APPLICATION LXC provided by https://tteck.github.io/Proxmox/\n" > /etc/motd
  137. chmod -x /etc/update-motd.d/*
  138. if ! getent shadow root | grep -q "^root:[^\!*]"; then
  139. msg_info "Customizing Container"
  140. GETTY_OVERRIDE="/etc/systemd/system/container-getty@1.service.d/override.conf"
  141. mkdir -p $(dirname $GETTY_OVERRIDE)
  142. cat <<EOF >$GETTY_OVERRIDE
  143. [Service]
  144. ExecStart=
  145. ExecStart=-/sbin/agetty --autologin root --noclear --keep-baud tty%I 115200,38400,9600 \$TERM
  146. EOF
  147. systemctl daemon-reload
  148. systemctl restart $(basename $(dirname $GETTY_OVERRIDE) | sed 's/\.d//')
  149. msg_ok "Customized Container"
  150. fi
  151. if [[ "${SSH_ROOT}" == "yes" ]]; then sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config; systemctl restart sshd; fi
  152. msg_info "Cleaning up"
  153. $STD apt-get autoremove
  154. $STD apt-get autoclean
  155. msg_ok "Cleaned"