gnu: sequoia: Update to 0.17.0.
[jackhill/guix/guix.git] / gnu / services / docker.scm
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 ;;;
6 ;;; This file is part of GNU Guix.
7 ;;;
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
12 ;;;
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
17 ;;;
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21 (define-module (gnu services docker)
22 #:use-module (gnu services)
23 #:use-module (gnu services configuration)
24 #:use-module (gnu services base)
25 #:use-module (gnu services dbus)
26 #:use-module (gnu services shepherd)
27 #:use-module (gnu system shadow)
28 #:use-module (gnu packages docker)
29 #:use-module (gnu packages linux) ;singularity
30 #:use-module (guix records)
31 #:use-module (guix gexp)
32 #:use-module (guix packages)
33
34 #:export (docker-configuration
35 docker-service-type
36 singularity-service-type))
37
38 ;;; We're not using serialize-configuration, but we must define this because
39 ;;; the define-configuration macro validates it exists.
40 (define (serialize-boolean field-name val)
41 "")
42
43 (define-configuration docker-configuration
44 (docker
45 (package docker)
46 "Docker daemon package.")
47 (containerd
48 (package containerd)
49 "containerd package.")
50 (proxy
51 (package docker-libnetwork-cmd-proxy)
52 "The proxy package to support inter-container and outside-container
53 loop-back communications.")
54 (enable-proxy?
55 (boolean #t)
56 "Enable or disable the user-land proxy (enabled by default).")
57 (debug?
58 (boolean #f)
59 "Enable or disable debug output."))
60
61 (define %docker-accounts
62 (list (user-group (name "docker") (system? #t))))
63
64 (define (%containerd-activation config)
65 (let ((state-dir "/var/lib/containerd"))
66 #~(begin
67 (use-modules (guix build utils))
68 (mkdir-p #$state-dir))))
69
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
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
91 (define (docker-shepherd-service config)
92 (let* ((docker (docker-configuration-docker config))
93 (enable-proxy? (docker-configuration-enable-proxy? 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? "--userland-proxy" "")
117 "--userland-proxy-path" (string-append #$proxy
118 "/bin/proxy"))
119 #:pid-file "/var/run/docker.pid"
120 #:log-file "/var/log/docker.log"))
121 (stop #~(make-kill-destructor)))))
122
123 (define docker-service-type
124 (service-type (name 'docker)
125 (description "Provide capability to run Docker application
126 bundles in Docker containers.")
127 (extensions
128 (list
129 (service-extension activation-service-type
130 %docker-activation)
131 (service-extension shepherd-root-service-type
132 (lambda (config)
133 (list (containerd-shepherd-service config)
134 (docker-shepherd-service config))))
135 (service-extension account-service-type
136 (const %docker-accounts))))
137 (default-value (docker-configuration))))
138
139 \f
140 ;;;
141 ;;; Singularity.
142 ;;;
143
144 (define %singularity-activation
145 (with-imported-modules '((guix build utils))
146 #~(begin
147 (use-modules (guix build utils))
148
149 (define %mount-directory
150 "/var/singularity/mnt/")
151
152 ;; Create the directories that Singularity 2.6 expects to find. Make
153 ;; them #o755 like the 'install-data-hook' rule in 'Makefile.am' of
154 ;; Singularity 2.6.1.
155 (for-each (lambda (directory)
156 (let ((directory (string-append %mount-directory
157 directory)))
158 (mkdir-p directory)
159 (chmod directory #o755)))
160 '("container" "final" "overlay" "session"))
161 (chmod %mount-directory #o755))))
162
163 (define (singularity-setuid-programs singularity)
164 "Return the setuid-root programs that SINGULARITY needs."
165 (define helpers
166 ;; The helpers, under a meaningful name.
167 (computed-file "singularity-setuid-helpers"
168 #~(begin
169 (mkdir #$output)
170 (for-each (lambda (program)
171 (symlink (string-append #$singularity
172 "/libexec/singularity"
173 "/bin/"
174 program "-suid")
175 (string-append #$output
176 "/singularity-"
177 program
178 "-helper")))
179 '("action" "mount" "start")))))
180
181 (list (file-append helpers "/singularity-action-helper")
182 (file-append helpers "/singularity-mount-helper")
183 (file-append helpers "/singularity-start-helper")))
184
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)))