docker-v5-install.sh 5.6 KB

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