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 ;;; Copyright © 2020 Jesse Dowell <jessedowell@gmail.com>
8 ;;; This file is part of GNU Guix.
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.
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.
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/>.
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)
31 #:use-module (gnu packages linux) ;singularity
32 #:use-module (guix records)
33 #:use-module (guix gexp)
34 #:use-module (guix packages)
36 #:export (docker-configuration
38 singularity-service-type))
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)
45 (define-configuration docker-configuration
48 "Docker daemon package.")
51 "Docker client package.")
54 "containerd package.")
56 (package docker-libnetwork-cmd-proxy)
57 "The proxy package to support inter-container and outside-container
58 loop-back communications.")
61 "Enable or disable the user-land proxy (enabled by default).")
64 "Enable or disable debug output.")
67 "Enable addition of iptables rules (enabled by default)."))
69 (define %docker-accounts
70 (list (user-group (name "docker") (system? #t))))
72 (define (%containerd-activation config)
73 (let ((state-dir "/var/lib/containerd"))
75 (use-modules (guix build utils))
76 (mkdir-p #$state-dir))))
78 (define (%docker-activation config)
79 (%containerd-activation config)
80 (let ((state-dir "/var/lib/docker"))
82 (use-modules (guix build utils))
83 (mkdir-p #$state-dir))))
85 (define (containerd-shepherd-service config)
86 (let* ((package (docker-configuration-containerd config))
87 (debug? (docker-configuration-debug? config))
88 (containerd (docker-configuration-containerd config)))
90 (documentation "containerd daemon.")
91 (provision '(containerd))
92 (start #~(make-forkexec-constructor
93 (list (string-append #$package "/bin/containerd")
95 '("--log-level=debug")
97 ;; For finding containerd-shim binary.
98 #:environment-variables
99 (list (string-append "PATH=" #$containerd "/bin"))
100 #:log-file "/var/log/containerd.log"))
101 (stop #~(make-kill-destructor)))))
103 (define (docker-shepherd-service config)
104 (let* ((docker (docker-configuration-docker config))
105 (enable-proxy? (docker-configuration-enable-proxy? config))
106 (enable-iptables? (docker-configuration-enable-iptables? config))
107 (proxy (docker-configuration-proxy config))
108 (debug? (docker-configuration-debug? config)))
110 (documentation "Docker daemon.")
111 (provision '(dockerd))
112 (requirement '(containerd
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
120 file-system-/sys/fs/cgroup/pids
123 (start #~(make-forkexec-constructor
124 (list (string-append #$docker "/bin/dockerd")
125 "-p" "/var/run/docker.pid"
127 '("--debug" "--log-level=debug")
130 (list "--userland-proxy=true"
132 "--userland-proxy-path=" #$proxy "/bin/proxy"))
133 '("--userland-proxy=false"))
134 (if #$enable-iptables?
137 #:pid-file "/var/run/docker.pid"
138 #:log-file "/var/log/docker.log"))
139 (stop #~(make-kill-destructor)))))
141 (define docker-service-type
142 (service-type (name 'docker)
143 (description "Provide capability to run Docker application
144 bundles in Docker containers.")
147 ;; Make sure the 'docker' command is available.
148 (service-extension profile-service-type
149 (compose list docker-configuration-docker-cli))
150 (service-extension activation-service-type
152 (service-extension shepherd-root-service-type
154 (list (containerd-shepherd-service config)
155 (docker-shepherd-service config))))
156 (service-extension account-service-type
157 (const %docker-accounts))))
158 (default-value (docker-configuration))))
165 (define %singularity-activation
166 (with-imported-modules '((guix build utils))
168 (use-modules (guix build utils))
170 (define %mount-directory
171 "/var/singularity/mnt/")
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
180 (chmod directory #o755)))
181 '("container" "final" "overlay" "session"))
182 (chmod %mount-directory #o755))))
184 (define (singularity-setuid-programs singularity)
185 "Return the setuid-root programs that SINGULARITY needs."
187 ;; The helpers, under a meaningful name.
188 (computed-file "singularity-setuid-helpers"
191 (for-each (lambda (program)
192 (symlink (string-append #$singularity
193 "/libexec/singularity"
196 (string-append #$output
200 '("action" "mount" "start")))))
202 (list (file-append helpers "/singularity-action-helper")
203 (file-append helpers "/singularity-mount-helper")
204 (file-append helpers "/singularity-start-helper")))
206 (define singularity-service-type
207 (service-type (name 'singularity)
209 "Install the Singularity application bundle tool.")
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)))