services: guix-publish: Add zstd compression by default.
[jackhill/guix/guix.git] / gnu / services / docker.scm
CommitLineData
8af4c335
DM
1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2018 Danny Milosavljevic <dannym@scratchpost.org>
2ef4d273 3;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
7c9be7b7 4;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
8422a67d 5;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
f0a09310 6;;; Copyright © 2020 Jesse Dowell <jessedowell@gmail.com>
8af4c335
DM
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
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 shadow)
30 #:use-module (gnu packages docker)
08814aec 31 #:use-module (gnu packages linux) ;singularity
8af4c335
DM
32 #:use-module (guix records)
33 #:use-module (guix gexp)
34 #:use-module (guix packages)
35
36 #:export (docker-configuration
08814aec
LC
37 docker-service-type
38 singularity-service-type))
8af4c335 39
7a31d93a
MC
40;;; We're not using serialize-configuration, but we must define this because
41;;; the define-configuration macro validates it exists.
42(define (serialize-boolean field-name val)
43 "")
44
8af4c335
DM
45(define-configuration docker-configuration
46 (docker
47 (package docker)
48 "Docker daemon package.")
f6dfe421
OP
49 (docker-cli
50 (package docker-cli)
51 "Docker client package.")
8af4c335
DM
52 (containerd
53 (package containerd)
7a31d93a
MC
54 "containerd package.")
55 (proxy
56 (package docker-libnetwork-cmd-proxy)
57 "The proxy package to support inter-container and outside-container
58loop-back communications.")
59 (enable-proxy?
60 (boolean #t)
7c9be7b7
MC
61 "Enable or disable the user-land proxy (enabled by default).")
62 (debug?
63 (boolean #f)
2b68a964
AA
64 "Enable or disable debug output.")
65 (enable-iptables?
66 (boolean #t)
67 "Enable addition of iptables rules (enabled by default)."))
8af4c335
DM
68
69(define %docker-accounts
70 (list (user-group (name "docker") (system? #t))))
71
72(define (%containerd-activation config)
73 (let ((state-dir "/var/lib/containerd"))
74 #~(begin
75 (use-modules (guix build utils))
76 (mkdir-p #$state-dir))))
77
78(define (%docker-activation config)
79 (%containerd-activation config)
80 (let ((state-dir "/var/lib/docker"))
81 #~(begin
82 (use-modules (guix build utils))
83 (mkdir-p #$state-dir))))
84
85(define (containerd-shepherd-service config)
7c9be7b7 86 (let* ((package (docker-configuration-containerd config))
6a5a477d
OP
87 (debug? (docker-configuration-debug? config))
88 (containerd (docker-configuration-containerd config)))
8af4c335
DM
89 (shepherd-service
90 (documentation "containerd daemon.")
91 (provision '(containerd))
92 (start #~(make-forkexec-constructor
7c9be7b7
MC
93 (list (string-append #$package "/bin/containerd")
94 #$@(if debug?
95 '("--log-level=debug")
96 '()))
6a5a477d
OP
97 ;; For finding containerd-shim binary.
98 #:environment-variables
99 (list (string-append "PATH=" #$containerd "/bin"))
f0bfd0fc 100 #:log-file "/var/log/containerd.log"))
8af4c335
DM
101 (stop #~(make-kill-destructor)))))
102
103(define (docker-shepherd-service config)
7a31d93a
MC
104 (let* ((docker (docker-configuration-docker config))
105 (enable-proxy? (docker-configuration-enable-proxy? config))
2b68a964 106 (enable-iptables? (docker-configuration-enable-iptables? config))
7c9be7b7
MC
107 (proxy (docker-configuration-proxy config))
108 (debug? (docker-configuration-debug? config)))
8af4c335
DM
109 (shepherd-service
110 (documentation "Docker daemon.")
111 (provision '(dockerd))
8b0c1744 112 (requirement '(containerd
1c84e68b
DM
113 dbus-system
114 elogind
8b0c1744
DM
115 file-system-/sys/fs/cgroup/blkio
116 file-system-/sys/fs/cgroup/cpu
117 file-system-/sys/fs/cgroup/cpuset
118 file-system-/sys/fs/cgroup/devices
119 file-system-/sys/fs/cgroup/memory
2ef4d273 120 file-system-/sys/fs/cgroup/pids
1c84e68b
DM
121 networking
122 udev))
8af4c335
DM
123 (start #~(make-forkexec-constructor
124 (list (string-append #$docker "/bin/dockerd")
7a31d93a 125 "-p" "/var/run/docker.pid"
7c9be7b7
MC
126 #$@(if debug?
127 '("--debug" "--log-level=debug")
128 '())
cd4bb173
OP
129 #$@(if enable-proxy?
130 (list "--userland-proxy=true"
131 #~(string-append
132 "--userland-proxy-path=" #$proxy "/bin/proxy"))
133 '("--userland-proxy=false"))
2b68a964
AA
134 (if #$enable-iptables?
135 "--iptables"
136 "--iptables=false"))
8af4c335
DM
137 #:pid-file "/var/run/docker.pid"
138 #:log-file "/var/log/docker.log"))
139 (stop #~(make-kill-destructor)))))
140
141(define docker-service-type
142 (service-type (name 'docker)
143 (description "Provide capability to run Docker application
144bundles in Docker containers.")
145 (extensions
146 (list
8422a67d
EF
147 ;; Make sure the 'docker' command is available.
148 (service-extension profile-service-type
f6dfe421 149 (compose list docker-configuration-docker-cli))
8af4c335
DM
150 (service-extension activation-service-type
151 %docker-activation)
152 (service-extension shepherd-root-service-type
d3a0e74d
DM
153 (lambda (config)
154 (list (containerd-shepherd-service config)
155 (docker-shepherd-service config))))
8af4c335
DM
156 (service-extension account-service-type
157 (const %docker-accounts))))
158 (default-value (docker-configuration))))
08814aec
LC
159
160\f
161;;;
162;;; Singularity.
163;;;
164
165(define %singularity-activation
166 (with-imported-modules '((guix build utils))
167 #~(begin
168 (use-modules (guix build utils))
169
170 (define %mount-directory
171 "/var/singularity/mnt/")
172
173 ;; Create the directories that Singularity 2.6 expects to find. Make
174 ;; them #o755 like the 'install-data-hook' rule in 'Makefile.am' of
175 ;; Singularity 2.6.1.
176 (for-each (lambda (directory)
177 (let ((directory (string-append %mount-directory
178 directory)))
179 (mkdir-p directory)
180 (chmod directory #o755)))
181 '("container" "final" "overlay" "session"))
182 (chmod %mount-directory #o755))))
183
184(define (singularity-setuid-programs singularity)
185 "Return the setuid-root programs that SINGULARITY needs."
186 (define helpers
187 ;; The helpers, under a meaningful name.
188 (computed-file "singularity-setuid-helpers"
189 #~(begin
190 (mkdir #$output)
191 (for-each (lambda (program)
192 (symlink (string-append #$singularity
193 "/libexec/singularity"
194 "/bin/"
195 program "-suid")
196 (string-append #$output
197 "/singularity-"
198 program
199 "-helper")))
200 '("action" "mount" "start")))))
201
202 (list (file-append helpers "/singularity-action-helper")
203 (file-append helpers "/singularity-mount-helper")
204 (file-append helpers "/singularity-start-helper")))
205
206(define singularity-service-type
207 (service-type (name 'singularity)
208 (description
209 "Install the Singularity application bundle tool.")
210 (extensions
211 (list (service-extension setuid-program-service-type
212 singularity-setuid-programs)
213 (service-extension activation-service-type
214 (const %singularity-activation))))
215 (default-value singularity)))