1cce767675417c323a665bf352cbba1cab76f314
[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 ;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
6 ;;;
7 ;;; This file is part of GNU Guix.
8 ;;;
9 ;;; GNU Guix is free software; you can redistribute it and/or modify it
10 ;;; under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 3 of the License, or (at
12 ;;; your option) any later version.
13 ;;;
14 ;;; GNU Guix is distributed in the hope that it will be useful, but
15 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
18 ;;;
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21
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
35 #:export (docker-configuration
36 docker-service-type
37 singularity-service-type))
38
39 ;;; We're not using serialize-configuration, but we must define this because
40 ;;; the define-configuration macro validates it exists.
41 (define (serialize-boolean field-name val)
42 "")
43
44 (define-configuration docker-configuration
45 (docker
46 (package docker)
47 "Docker daemon 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
65 (define %docker-accounts
66 (list (user-group (name "docker") (system? #t))))
67
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
74 (define (%docker-activation config)
75 (%containerd-activation config)
76 (let ((state-dir "/var/lib/docker"))
77 #~(begin
78 (use-modules (guix build utils))
79 (mkdir-p #$state-dir))))
80
81 (define (containerd-shepherd-service config)
82 (let* ((package (docker-configuration-containerd config))
83 (debug? (docker-configuration-debug? config)))
84 (shepherd-service
85 (documentation "containerd daemon.")
86 (provision '(containerd))
87 (start #~(make-forkexec-constructor
88 (list (string-append #$package "/bin/containerd")
89 #$@(if debug?
90 '("--log-level=debug")
91 '()))
92 #:log-file "/var/log/containerd.log"))
93 (stop #~(make-kill-destructor)))))
94
95 (define (docker-shepherd-service config)
96 (let* ((docker (docker-configuration-docker config))
97 (enable-proxy? (docker-configuration-enable-proxy? config))
98 (enable-iptables? (docker-configuration-enable-iptables? config))
99 (proxy (docker-configuration-proxy config))
100 (debug? (docker-configuration-debug? config)))
101 (shepherd-service
102 (documentation "Docker daemon.")
103 (provision '(dockerd))
104 (requirement '(containerd
105 dbus-system
106 elogind
107 file-system-/sys/fs/cgroup/blkio
108 file-system-/sys/fs/cgroup/cpu
109 file-system-/sys/fs/cgroup/cpuset
110 file-system-/sys/fs/cgroup/devices
111 file-system-/sys/fs/cgroup/memory
112 file-system-/sys/fs/cgroup/pids
113 networking
114 udev))
115 (start #~(make-forkexec-constructor
116 (list (string-append #$docker "/bin/dockerd")
117 "-p" "/var/run/docker.pid"
118 #$@(if debug?
119 '("--debug" "--log-level=debug")
120 '())
121 (if #$enable-proxy? "--userland-proxy" "")
122 "--userland-proxy-path" (string-append #$proxy
123 "/bin/proxy")
124 (if #$enable-iptables?
125 "--iptables"
126 "--iptables=false"))
127 #:pid-file "/var/run/docker.pid"
128 #:log-file "/var/log/docker.log"))
129 (stop #~(make-kill-destructor)))))
130
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-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 \f
151 ;;;
152 ;;; Singularity.
153 ;;;
154
155 (define %singularity-activation
156 (with-imported-modules '((guix build utils))
157 #~(begin
158 (use-modules (guix build utils))
159
160 (define %mount-directory
161 "/var/singularity/mnt/")
162
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
174 (define (singularity-setuid-programs singularity)
175 "Return the setuid-root programs that SINGULARITY needs."
176 (define helpers
177 ;; The helpers, under a meaningful name.
178 (computed-file "singularity-setuid-helpers"
179 #~(begin
180 (mkdir #$output)
181 (for-each (lambda (program)
182 (symlink (string-append #$singularity
183 "/libexec/singularity"
184 "/bin/"
185 program "-suid")
186 (string-append #$output
187 "/singularity-"
188 program
189 "-helper")))
190 '("action" "mount" "start")))))
191
192 (list (file-append helpers "/singularity-action-helper")
193 (file-append helpers "/singularity-mount-helper")
194 (file-append helpers "/singularity-start-helper")))
195
196 (define singularity-service-type
197 (service-type (name 'singularity)
198 (description
199 "Install the Singularity application bundle tool.")
200 (extensions
201 (list (service-extension setuid-program-service-type
202 singularity-setuid-programs)
203 (service-extension activation-service-type
204 (const %singularity-activation))))
205 (default-value singularity)))