docker.scm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Danny Milosavljevic <dannym@scratchpost.org>
  3. ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
  4. ;;; Copyright © 2020, 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  5. ;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
  6. ;;; Copyright © 2020 Jesse Dowell <jessedowell@gmail.com>
  7. ;;; Copyright © 2021 Brice Waegeneire <brice@waegenei.re>
  8. ;;;
  9. ;;; This file is part of GNU Guix.
  10. ;;;
  11. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  12. ;;; under the terms of the GNU General Public License as published by
  13. ;;; the Free Software Foundation; either version 3 of the License, or (at
  14. ;;; your option) any later version.
  15. ;;;
  16. ;;; GNU Guix is distributed in the hope that it will be useful, but
  17. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;;; GNU General Public License for more details.
  20. ;;;
  21. ;;; You should have received a copy of the GNU General Public License
  22. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  23. (define-module (gnu services docker)
  24. #:use-module (gnu services)
  25. #:use-module (gnu services configuration)
  26. #:use-module (gnu services base)
  27. #:use-module (gnu services dbus)
  28. #:use-module (gnu services shepherd)
  29. #:use-module (gnu system setuid)
  30. #:use-module (gnu system shadow)
  31. #:use-module (gnu packages docker)
  32. #:use-module (gnu packages linux) ;singularity
  33. #:use-module (guix records)
  34. #:use-module (guix gexp)
  35. #:use-module (guix packages)
  36. #:export (docker-configuration
  37. docker-service-type
  38. singularity-service-type))
  39. (define-configuration docker-configuration
  40. (docker
  41. (package docker)
  42. "Docker daemon package.")
  43. (docker-cli
  44. (package docker-cli)
  45. "Docker client package.")
  46. (containerd
  47. (package containerd)
  48. "containerd package.")
  49. (proxy
  50. (package docker-libnetwork-cmd-proxy)
  51. "The proxy package to support inter-container and outside-container
  52. loop-back communications.")
  53. (enable-proxy?
  54. (boolean #t)
  55. "Enable or disable the user-land proxy (enabled by default).")
  56. (debug?
  57. (boolean #f)
  58. "Enable or disable debug output.")
  59. (enable-iptables?
  60. (boolean #t)
  61. "Enable addition of iptables rules (enabled by default).")
  62. (no-serialization))
  63. (define %docker-accounts
  64. (list (user-group (name "docker") (system? #t))))
  65. (define (%containerd-activation config)
  66. (let ((state-dir "/var/lib/containerd"))
  67. #~(begin
  68. (use-modules (guix build utils))
  69. (mkdir-p #$state-dir))))
  70. (define (%docker-activation config)
  71. (%containerd-activation config)
  72. (let ((state-dir "/var/lib/docker"))
  73. #~(begin
  74. (use-modules (guix build utils))
  75. (mkdir-p #$state-dir))))
  76. (define (containerd-shepherd-service config)
  77. (let* ((package (docker-configuration-containerd config))
  78. (debug? (docker-configuration-debug? config))
  79. (containerd (docker-configuration-containerd config)))
  80. (shepherd-service
  81. (documentation "containerd daemon.")
  82. (provision '(containerd))
  83. (start #~(make-forkexec-constructor
  84. (list (string-append #$package "/bin/containerd")
  85. #$@(if debug?
  86. '("--log-level=debug")
  87. '()))
  88. ;; For finding containerd-shim binary.
  89. #:environment-variables
  90. (list (string-append "PATH=" #$containerd "/bin"))
  91. #:log-file "/var/log/containerd.log"))
  92. (stop #~(make-kill-destructor)))))
  93. (define (docker-shepherd-service config)
  94. (let* ((docker (docker-configuration-docker config))
  95. (enable-proxy? (docker-configuration-enable-proxy? config))
  96. (enable-iptables? (docker-configuration-enable-iptables? config))
  97. (proxy (docker-configuration-proxy config))
  98. (debug? (docker-configuration-debug? config)))
  99. (shepherd-service
  100. (documentation "Docker daemon.")
  101. (provision '(dockerd))
  102. (requirement '(containerd
  103. dbus-system
  104. elogind
  105. file-system-/sys/fs/cgroup/blkio
  106. file-system-/sys/fs/cgroup/cpu
  107. file-system-/sys/fs/cgroup/cpuset
  108. file-system-/sys/fs/cgroup/devices
  109. file-system-/sys/fs/cgroup/memory
  110. file-system-/sys/fs/cgroup/pids
  111. networking
  112. udev))
  113. (start #~(make-forkexec-constructor
  114. (list (string-append #$docker "/bin/dockerd")
  115. "-p" "/var/run/docker.pid"
  116. #$@(if debug?
  117. '("--debug" "--log-level=debug")
  118. '())
  119. #$@(if enable-proxy?
  120. (list "--userland-proxy=true"
  121. #~(string-append
  122. "--userland-proxy-path=" #$proxy "/bin/proxy"))
  123. '("--userland-proxy=false"))
  124. (if #$enable-iptables?
  125. "--iptables"
  126. "--iptables=false"))
  127. #:pid-file "/var/run/docker.pid"
  128. #:log-file "/var/log/docker.log"))
  129. (stop #~(make-kill-destructor)))))
  130. (define docker-service-type
  131. (service-type (name 'docker)
  132. (description "Provide capability to run Docker application
  133. bundles in Docker containers.")
  134. (extensions
  135. (list
  136. ;; Make sure the 'docker' command is available.
  137. (service-extension profile-service-type
  138. (compose list docker-configuration-docker-cli))
  139. (service-extension activation-service-type
  140. %docker-activation)
  141. (service-extension shepherd-root-service-type
  142. (lambda (config)
  143. (list (containerd-shepherd-service config)
  144. (docker-shepherd-service config))))
  145. (service-extension account-service-type
  146. (const %docker-accounts))))
  147. (default-value (docker-configuration))))
  148. ;;;
  149. ;;; Singularity.
  150. ;;;
  151. (define %singularity-activation
  152. (with-imported-modules '((guix build utils))
  153. #~(begin
  154. (use-modules (guix build utils))
  155. (define %mount-directory
  156. "/var/singularity/mnt/")
  157. ;; Create the directories that Singularity 2.6 expects to find. Make
  158. ;; them #o755 like the 'install-data-hook' rule in 'Makefile.am' of
  159. ;; Singularity 2.6.1.
  160. (for-each (lambda (directory)
  161. (let ((directory (string-append %mount-directory
  162. directory)))
  163. (mkdir-p directory)
  164. (chmod directory #o755)))
  165. '("container" "final" "overlay" "session"))
  166. (chmod %mount-directory #o755))))
  167. (define (singularity-setuid-programs singularity)
  168. "Return the setuid-root programs that SINGULARITY needs."
  169. (define helpers
  170. ;; The helpers, under a meaningful name.
  171. (computed-file "singularity-setuid-helpers"
  172. #~(begin
  173. (mkdir #$output)
  174. (for-each (lambda (program)
  175. (symlink (string-append #$singularity
  176. "/libexec/singularity"
  177. "/bin/"
  178. program "-suid")
  179. (string-append #$output
  180. "/singularity-"
  181. program
  182. "-helper")))
  183. '("action" "mount" "start")))))
  184. (map file-like->setuid-program
  185. (list (file-append helpers "/singularity-action-helper")
  186. (file-append helpers "/singularity-mount-helper")
  187. (file-append helpers "/singularity-start-helper"))))
  188. (define singularity-service-type
  189. (service-type (name 'singularity)
  190. (description
  191. "Install the Singularity application bundle tool.")
  192. (extensions
  193. (list (service-extension setuid-program-service-type
  194. singularity-setuid-programs)
  195. (service-extension activation-service-type
  196. (const %singularity-activation))))
  197. (default-value singularity)))