docker.scm 8.7 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 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. (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. #:log-file "/var/log/containerd.log"))
  89. (stop #~(make-kill-destructor)))))
  90. (define (docker-shepherd-service config)
  91. (let* ((docker (docker-configuration-docker config))
  92. (enable-proxy? (docker-configuration-enable-proxy? config))
  93. (enable-iptables? (docker-configuration-enable-iptables? config))
  94. (proxy (docker-configuration-proxy config))
  95. (debug? (docker-configuration-debug? config)))
  96. (shepherd-service
  97. (documentation "Docker daemon.")
  98. (provision '(dockerd))
  99. (requirement '(containerd
  100. dbus-system
  101. elogind
  102. file-system-/sys/fs/cgroup/blkio
  103. file-system-/sys/fs/cgroup/cpu
  104. file-system-/sys/fs/cgroup/cpuset
  105. file-system-/sys/fs/cgroup/devices
  106. file-system-/sys/fs/cgroup/memory
  107. file-system-/sys/fs/cgroup/pids
  108. networking
  109. udev))
  110. (start #~(make-forkexec-constructor
  111. (list (string-append #$docker "/bin/dockerd")
  112. "-p" "/var/run/docker.pid"
  113. #$@(if debug?
  114. '("--debug" "--log-level=debug")
  115. '())
  116. #$@(if enable-proxy?
  117. (list "--userland-proxy=true"
  118. #~(string-append
  119. "--userland-proxy-path=" #$proxy "/bin/proxy"))
  120. '("--userland-proxy=false"))
  121. (if #$enable-iptables?
  122. "--iptables"
  123. "--iptables=false"))
  124. #:pid-file "/var/run/docker.pid"
  125. #:log-file "/var/log/docker.log"))
  126. (stop #~(make-kill-destructor)))))
  127. (define docker-service-type
  128. (service-type (name 'docker)
  129. (description "Provide capability to run Docker application
  130. bundles in Docker containers.")
  131. (extensions
  132. (list
  133. ;; Make sure the 'docker' command is available.
  134. (service-extension profile-service-type
  135. (compose list docker-configuration-docker-cli))
  136. (service-extension activation-service-type
  137. %docker-activation)
  138. (service-extension shepherd-root-service-type
  139. (lambda (config)
  140. (list (containerd-shepherd-service config)
  141. (docker-shepherd-service config))))
  142. (service-extension account-service-type
  143. (const %docker-accounts))))
  144. (default-value (docker-configuration))))
  145. ;;;
  146. ;;; Singularity.
  147. ;;;
  148. (define %singularity-activation
  149. (with-imported-modules '((guix build utils))
  150. #~(begin
  151. (use-modules (guix build utils))
  152. (define %mount-directory
  153. "/var/singularity/mnt/")
  154. ;; Create the directories that Singularity 2.6 expects to find. Make
  155. ;; them #o755 like the 'install-data-hook' rule in 'Makefile.am' of
  156. ;; Singularity 2.6.1.
  157. (for-each (lambda (directory)
  158. (let ((directory (string-append %mount-directory
  159. directory)))
  160. (mkdir-p directory)
  161. (chmod directory #o755)))
  162. '("container" "final" "overlay" "session"))
  163. (chmod %mount-directory #o755))))
  164. (define (singularity-setuid-programs singularity)
  165. "Return the setuid-root programs that SINGULARITY needs."
  166. (define helpers
  167. ;; The helpers, under a meaningful name.
  168. (computed-file "singularity-setuid-helpers"
  169. #~(begin
  170. (mkdir #$output)
  171. (for-each (lambda (program)
  172. (symlink (string-append #$singularity
  173. "/libexec/singularity"
  174. "/bin/"
  175. program "-suid")
  176. (string-append #$output
  177. "/singularity-"
  178. program
  179. "-helper")))
  180. '("action" "mount" "start")))))
  181. (list (file-append helpers "/singularity-action-helper")
  182. (file-append helpers "/singularity-mount-helper")
  183. (file-append helpers "/singularity-start-helper")))
  184. (define singularity-service-type
  185. (service-type (name 'singularity)
  186. (description
  187. "Install the Singularity application bundle tool.")
  188. (extensions
  189. (list (service-extension setuid-program-service-type
  190. singularity-setuid-programs)
  191. (service-extension activation-service-type
  192. (const %singularity-activation))))
  193. (default-value singularity)))