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