install_docker.sh 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. # Add trust and docker apt repo
  11. mkdir -m 0755 -p /etc/apt/keyrings
  12. curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  13. chmod a+r /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. "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  17. tee /etc/apt/sources.list.d/docker.list > /dev/null
  18. # Install docker
  19. apt-get update && apt-get install --yes \
  20. docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
  21. # allow admin to use the docker command
  22. usermod --append --groups docker admin
  23. #----------
  24. # 1) Setup journald as log driver so we don't need to care separetly for log rotation
  25. #
  26. # 2) Change Docker Address pools
  27. # Docker default address address pools:
  28. # Vanilla
  29. # 172.17.0.0/12 split in /16 (size) => 16 networks each 65536 hosts
  30. # 192.168.0.0/16 split in /24 (size) => 256 networks each 256 hosts
  31. # More details: # https://straz.to/2021-09-08-docker-address-pools/
  32. # Reasons to change
  33. # - Short: The configuration is not suitable for our home server
  34. # - 192.168.0.0/16 is frequently used in the home environment
  35. # - 172.17.0.0/12 size doesn't offer enough networks and we never have that many hosts
  36. # Resulting configuraiton
  37. # 172.16.0.0/12 split in /22 (size) => 1024 networks each 1024 hosts
  38. # nnnn nnnn . nnnn NNNN . NNNN NNhh . hhhh hhhh
  39. # 12 2^10 22 2^10 32
  40. # But, we start with 172.18.0.0 and
  41. # (1) leave 172.16.0.0/16 untouched
  42. # (2.1) assign 172.17.0.0/22 to the default bridge
  43. # (2.2) use the remaing networks for special purposes 172.17.4.0, 172.17.8.0, 172.17.12.0, ...
  44. # => (1)+(2) takes each 64 /22-networks, resulting in 896 networks (1024-128)
  45. #
  46. # Tools: https://www.site24x7.com/tools/ipv4-subnetcalculator.html
  47. # Reference: RFC 1918 name: 172.16.0.0 - 172.31.255.255
  48. #
  49. # Check configuration: docker info | grep Base
  50. # Check configuration: docker network inspect bridge | grep Subnet
  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