docker-v5-install.sh 5.7 KB

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