install_docker.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/bash -e
  2. ## Install Docker
  3. # https://docs.docker.com/engine/install/debian/
  4. # Install dependencies
  5. apt-get update && apt-get install --yes \
  6. ca-certificates \
  7. curl \
  8. gnupg \
  9. # Add trust and docker apt repo
  10. mkdir -m 0755 -p /etc/apt/keyrings
  11. curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  12. chmod a+r /etc/apt/keyrings/docker.gpg
  13. echo \
  14. "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
  15. "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  16. tee /etc/apt/sources.list.d/docker.list > /dev/null
  17. # Install docker
  18. apt-get update && apt-get install --yes \
  19. docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  20. # allow admin to use the docker command
  21. usermod --append --groups docker admin
  22. #----------
  23. # 1) Setup journald as log driver so we don't need to care separetly for log rotation
  24. #
  25. # 2) Change Docker Address pools
  26. # Docker default address address pools:
  27. # Vanilla
  28. # 172.17.0.0/12 split in /16 (size) => 16 networks each 65536 hosts
  29. # 192.168.0.0/16 split in /24 (size) => 256 networks each 256 hosts
  30. # More details: # https://straz.to/2021-09-08-docker-address-pools/
  31. # Reasons to change
  32. # - Short: The configuration is not suitable for our home server
  33. # - 192.168.0.0/16 is frequently used in the home environment
  34. # - 172.17.0.0/12 size doesn't offer enough networks and we never have that many hosts
  35. # Resulting configuraiton
  36. # 172.16.0.0/12 split in /22 (size) => 1024 networks each 1024 hosts
  37. # nnnn nnnn . nnnn NNNN . NNNN NNhh . hhhh hhhh
  38. # 12 2^10 22 2^10 32
  39. # But, we start with 172.18.0.0 and
  40. # (1) leave 172.16.0.0/16 untouched
  41. # (2.1) assign 172.17.0.0/22 to the default bridge
  42. # (2.2) use the remaing networks for special purposes 172.17.4.0, 172.17.8.0, 172.17.12.0, ...
  43. # => (1)+(2) takes each 64 /22-networks, resulting in 896 networks (1024-128)
  44. #
  45. # Tools: https://www.site24x7.com/tools/ipv4-subnetcalculator.html
  46. # Reference: RFC 1918 name: 172.16.0.0 - 172.31.255.255
  47. #
  48. # Check configuration: docker info | grep Base
  49. # Check configuration: docker network inspect bridge | grep Subnet
  50. cat >/etc/docker/daemon.json <<EOL
  51. {
  52. "log-driver": "journald",
  53. "bip": "172.17.0.1/22",
  54. "default-address-pools": [
  55. {
  56. "base": "172.18.0.0/12",
  57. "size": 20
  58. }
  59. ]
  60. }
  61. EOL
  62. systemctl restart docker.service
  63. # Create directory for docker
  64. mkdir -p /opt/docker
  65. docker run hello-world