install_docker.sh 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. touch /etc/docker/daemon.json
  51. cat >>/etc/docker/daemon.json <<EOL
  52. {
  53. "log-driver": "journald",
  54. "bip": "172.17.0.1/22",
  55. "default-address-pools": [
  56. {
  57. "base": "172.18.0.0/12",
  58. "size": 20
  59. }
  60. ]
  61. }
  62. EOL
  63. systemctl restart docker.service
  64. # Create directory for docker
  65. mkdir -p /opt/docker
  66. docker run hello-world