install_docker.sh 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #!/bin/bash -e
  2. ## Install Docker
  3. # https://docs.docker.com/engine/install/debian/
  4. sudo -i
  5. # Install dependencies
  6. apt-get update && apt-get install --yes \
  7. ca-certificates \
  8. curl \
  9. gnupg \
  10. lsb-release
  11. # Add trust and docker apt repo
  12. mkdir -m 0755 -p /etc/apt/keyrings
  13. curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  14. echo \
  15. "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
  16. $(lsb_release -cs) stable" | 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