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