docker.scm 8.8 KB

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