set_docker_mirror.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/env bash
  2. # Licensed to the LF AI & Data foundation under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. # Use Internal docker mirror to solve https://www.docker.com/increase-rate-limits
  18. # Exit immediately for non zero status
  19. set -e
  20. # Use nexus as docker mirror registry
  21. MIRROR_URL="${MIRROR_URL:-http://nexus-nexus-repository-manager-docker-5000.nexus:5000}"
  22. #MIRROR_URL="http://nexus-nexus-repository-manager-docker-5000.nexus:5000"
  23. # Add registry mirror config into docker daemon
  24. set_daemon_json_file(){
  25. DOCKER_DAEMON_JSON_FILE="/etc/docker/daemon.json"
  26. mkdir -p "/etc/docker"
  27. echo "{\"registry-mirrors\": [\"${MIRROR_URL}\"],\"insecure-registries\":[\"${MIRROR_URL}\"]}" | tee ${DOCKER_DAEMON_JSON_FILE}
  28. }
  29. restart_docker () {
  30. echo "set-mirror.sh] service docker start"
  31. docker ps -aq | xargs -r docker rm -f || true
  32. service docker stop || true
  33. service docker start
  34. while true; do
  35. # docker ps -q should only work if the daemon is ready
  36. docker info > /dev/null 2>&1 && break
  37. if [[ ${WAIT_N} -lt 5 ]]; then
  38. WAIT_N=$((WAIT_N+1))
  39. echo " set-mirror.sh] Waiting for Docker to be ready, sleeping for ${WAIT_N} seconds ..."
  40. sleep ${WAIT_N}
  41. else
  42. echo "set-mirror.sh] [SETUP] Reached maximum attempts, not waiting any longer ..."
  43. break
  44. fi
  45. done
  46. echo "Show Docker Info With mirror url"
  47. docker info
  48. }
  49. set_mirror(){
  50. set_daemon_json_file
  51. restart_docker
  52. echo "Success."
  53. exit 0
  54. }
  55. set_mirror