docker.scm 9.2 KB

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