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>
7 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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)
35 #:export (docker-configuration
37 singularity-service-type))
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)
44 (define-configuration docker-configuration
47 "Docker daemon package.")
50 "containerd package.")
52 (package docker-libnetwork-cmd-proxy)
53 "The proxy package to support inter-container and outside-container
54 loop-back communications.")
57 "Enable or disable the user-land proxy (enabled by default).")
60 "Enable or disable debug output.")
63 "Enable addition of iptables rules (enabled by default)."))
65 (define %docker-accounts
66 (list (user-group (name "docker") (system? #t))))
68 (define (%containerd-activation config)
69 (let ((state-dir "/var/lib/containerd"))
71 (use-modules (guix build utils))
72 (mkdir-p #$state-dir))))
74 (define (%docker-activation config)
75 (%containerd-activation config)
76 (let ((state-dir "/var/lib/docker"))
78 (use-modules (guix build utils))
79 (mkdir-p #$state-dir))))
81 (define (containerd-shepherd-service config)
82 (let* ((package (docker-configuration-containerd config))
83 (debug? (docker-configuration-debug? config)))
85 (documentation "containerd daemon.")
86 (provision '(containerd))
87 (start #~(make-forkexec-constructor
88 (list (string-append #$package "/bin/containerd")
90 '("--log-level=debug")
92 #:log-file "/var/log/containerd.log"))
93 (stop #~(make-kill-destructor)))))
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)))
102 (documentation "Docker daemon.")
103 (provision '(dockerd))
104 (requirement '(containerd
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
115 (start #~(make-forkexec-constructor
116 (list (string-append #$docker "/bin/dockerd")
117 "-p" "/var/run/docker.pid"
119 '("--debug" "--log-level=debug")
121 (if #$enable-proxy? "--userland-proxy" "")
122 "--userland-proxy-path" (string-append #$proxy
124 (if #$enable-iptables?
127 #:pid-file "/var/run/docker.pid"
128 #:log-file "/var/log/docker.log"))
129 (stop #~(make-kill-destructor)))))
131 (define docker-service-type
132 (service-type (name 'docker)
133 (description "Provide capability to run Docker application
134 bundles in Docker containers.")
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
142 (service-extension shepherd-root-service-type
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))))
155 (define %singularity-activation
156 (with-imported-modules '((guix build utils))
158 (use-modules (guix build utils))
160 (define %mount-directory
161 "/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
170 (chmod directory #o755)))
171 '("container" "final" "overlay" "session"))
172 (chmod %mount-directory #o755))))
174 (define (singularity-setuid-programs singularity)
175 "Return the setuid-root programs that SINGULARITY needs."
177 ;; The helpers, under a meaningful name.
178 (computed-file "singularity-setuid-helpers"
181 (for-each (lambda (program)
182 (symlink (string-append #$singularity
183 "/libexec/singularity"
186 (string-append #$output
190 '("action" "mount" "start")))))
192 (list (file-append helpers "/singularity-action-helper")
193 (file-append helpers "/singularity-mount-helper")
194 (file-append helpers "/singularity-start-helper")))
196 (define singularity-service-type
197 (service-type (name 'singularity)
199 "Install the Singularity application bundle tool.")
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)))