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. # 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. touch /etc/docker/daemon.json
  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. install --owner=root --group=docker --mode 775 --directory test /opt/docker
  65. docker run hello-world