docker.scm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 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. ;;; We're not using serialize-configuration, but we must define this because
  38. ;;; the define-configuration macro validates it exists.
  39. (define (serialize-boolean field-name val)
  40. "")
  41. (define-configuration docker-configuration
  42. (docker
  43. (package docker)
  44. "Docker daemon package.")
  45. (docker-cli
  46. (package docker-cli)
  47. "Docker client package.")
  48. (containerd
  49. (package containerd)
  50. "containerd package.")
  51. (proxy
  52. (package docker-libnetwork-cmd-proxy)
  53. "The proxy package to support inter-container and outside-container
  54. loop-back communications.")
  55. (enable-proxy?
  56. (boolean #t)
  57. "Enable or disable the user-land proxy (enabled by default).")
  58. (debug?
  59. (boolean #f)
  60. "Enable or disable debug output.")
  61. (enable-iptables?
  62. (boolean #t)
  63. "Enable addition of iptables rules (enabled by default)."))
  64. (define %docker-accounts
  65. (list (user-group (name "docker") (system? #t))))
  66. (define (%containerd-activation config)
  67. (let ((state-dir "/var/lib/containerd"))
  68. #~(begin
  69. (use-modules (guix build utils))
  70. (mkdir-p #$state-dir))))
  71. (define (%docker-activation config)
  72. (%containerd-activation config)
  73. (let ((state-dir "/var/lib/docker"))
  74. #~(begin
  75. (use-modules (guix build utils))
  76. (mkdir-p #$state-dir))))
  77. (define (containerd-shepherd-service config)
  78. (let* ((package (docker-configuration-containerd config))
  79. (debug? (docker-configuration-debug? config))
  80. (containerd (docker-configuration-containerd config)))
  81. (shepherd-service
  82. (documentation "containerd daemon.")
  83. (provision '(containerd))
  84. (start #~(make-forkexec-constructor
  85. (list (string-append #$package "/bin/containerd")
  86. #$@(if debug?
  87. '("--log-level=debug")
  88. '()))
  89. ;; For finding containerd-shim binary.
  90. #:environment-variables
  91. (list (string-append "PATH=" #$containerd "/bin"))
  92. #:log-file "/var/log/containerd.log"))
  93. (stop #~(make-kill-destructor)))))
  94. (define (docker-shepherd-service config)
  95. (let* ((docker (docker-configuration-docker config))
  96. (enable-proxy? (docker-configuration-enable-proxy? config))
  97. (enable-iptables? (docker-configuration-enable-iptables? config))
  98. (proxy (docker-configuration-proxy config))
  99. (debug? (docker-configuration-debug? config)))
  100. (shepherd-service
  101. (documentation "Docker daemon.")
  102. (provision '(dockerd))
  103. (requirement '(containerd
  104. dbus-system
  105. elogind
  106. file-system-/sys/fs/cgroup/blkio
  107. file-system-/sys/fs/cgroup/cpu
  108. file-system-/sys/fs/cgroup/cpuset
  109. file-system-/sys/fs/cgroup/devices
  110. file-system-/sys/fs/cgroup/memory
  111. file-system-/sys/fs/cgroup/pids
  112. networking
  113. udev))
  114. (start #~(make-forkexec-constructor
  115. (list (string-append #$docker "/bin/dockerd")
  116. "-p" "/var/run/docker.pid"
  117. #$@(if debug?
  118. '("--debug" "--log-level=debug")
  119. '())
  120. #$@(if enable-proxy?
  121. (list "--userland-proxy=true"
  122. #~(string-append
  123. "--userland-proxy-path=" #$proxy "/bin/proxy"))
  124. '("--userland-proxy=false"))
  125. (if #$enable-iptables?
  126. "--iptables"
  127. "--iptables=false"))
  128. #:pid-file "/var/run/docker.pid"
  129. #:log-file "/var/log/docker.log"))
  130. (stop #~(make-kill-destructor)))))
  131. (define docker-service-type
  132. (service-type (name 'docker)
  133. (description "Provide capability to run Docker application
  134. bundles in Docker containers.")
  135. (extensions
  136. (list
  137. ;; Make sure the 'docker' command is available.
  138. (service-extension profile-service-type
  139. (compose list docker-configuration-docker-cli))
  140. (service-extension activation-service-type
  141. %docker-activation)
  142. (service-extension shepherd-root-service-type
  143. (lambda (config)
  144. (list (containerd-shepherd-service config)
  145. (docker-shepherd-service config))))
  146. (service-extension account-service-type
  147. (const %docker-accounts))))
  148. (default-value (docker-configuration))))
  149. ;;;
  150. ;;; Singularity.
  151. ;;;
  152. (define %singularity-activation
  153. (with-imported-modules '((guix build utils))
  154. #~(begin
  155. (use-modules (guix build utils))
  156. (define %mount-directory
  157. "/var/singularity/mnt/")
  158. ;; Create the directories that Singularity 2.6 expects to find. Make
  159. ;; them #o755 like the 'install-data-hook' rule in 'Makefile.am' of
  160. ;; Singularity 2.6.1.
  161. (for-each (lambda (directory)
  162. (let ((directory (string-append %mount-directory
  163. directory)))
  164. (mkdir-p directory)
  165. (chmod directory #o755)))
  166. '("container" "final" "overlay" "session"))
  167. (chmod %mount-directory #o755))))
  168. (define (singularity-setuid-programs singularity)
  169. "Return the setuid-root programs that SINGULARITY needs."
  170. (define helpers
  171. ;; The helpers, under a meaningful name.
  172. (computed-file "singularity-setuid-helpers"
  173. #~(begin
  174. (mkdir #$output)
  175. (for-each (lambda (program)
  176. (symlink (string-append #$singularity
  177. "/libexec/singularity"
  178. "/bin/"
  179. program "-suid")
  180. (string-append #$output
  181. "/singularity-"
  182. program
  183. "-helper")))
  184. '("action" "mount" "start")))))
  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)))