install_docker.sh 2.5 KB

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