unofficial mirror of guix-patches@gnu.org 
 help / color / mirror / code / Atom feed
blob f3a347981f0d09371f429a8d54118f083e60fc4f 18253 bytes (raw)
name: gnu/services/docker.scm 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
 
;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2018 Danny Milosavljevic <dannym@scratchpost.org>
;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
;;; Copyright © 2020, 2021 Maxim Cournoyer <maxim.cournoyer@gmail.com>
;;; Copyright © 2020 Efraim Flashner <efraim@flashner.co.il>
;;; Copyright © 2020 Jesse Dowell <jessedowell@gmail.com>
;;; Copyright © 2021 Brice Waegeneire <brice@waegenei.re>
;;; Copyright © 2022 Maya Tomasek <maya.omase@disroot.org>
;;;
;;; This file is part of GNU Guix.
;;;
;;; GNU Guix is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; GNU Guix is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

(define-module (magi system docker)
  #:use-module (srfi srfi-1)
  #:use-module (ice-9 format)
  #:use-module (gnu services)
  #:use-module (gnu services configuration)
  #:use-module (gnu services base)
  #:use-module (gnu services dbus)
  #:use-module (gnu services shepherd)
  #:use-module (gnu system setuid)
  #:use-module (gnu system shadow)
  #:use-module (gnu packages docker)
  #:use-module (gnu packages linux)               ;singularity
  #:use-module (guix records)
  #:use-module (guix gexp)
  #:use-module (guix packages)

  #:export (docker-configuration
            docker-container
            docker-service-type
            singularity-service-type))

(define (pair-of-strings? val)
  (and (pair? val)
       (string? (car val))
       (string? (cdr val))))

(define (list-of-pair-of-strings? val)
  (list-of pair-of-strings?))

(define-configuration/no-serialization docker-container
  (name
   (symbol '())
   "Name of the docker container. Will be used to denote service to Shepherd and must be unique!
We recommend, that the name of the container is prefixed with @code{docker-}.")
  (documentation
   (string "")
   "Documentation on the docker container (optional). It will be used for the shepherd service.")
  (image-name
   (string #f)
   "A name of the image that will be used. (Note that the existence of the image
is not guaranteed by this daemon.)")
  (volumes
   (list-of-pair-of-strings '())
   "A list of volume bindings. In (HOST-PATH CONTAINER-PATH) format.")
  (ports
   (list-of-pair-of-strings '())
   "A list of port bindings. In (HOST-PORT CONTAINER-PORT) or (HOST-PORT CONTAINER-PORT OPTIONS) format.
For example, both port bindings are valid:

@lisp
(ports '((\"2222\" \"22\") (\"21\" \"21\" \"tcp\")))
@end lisp")
  (environments
   (list-of-pair-of-strings '())
   "A list of environment variables, inside the container environment, in (VARIABLE VALUE) format.")
  (network
   (string "none")
   "Network type.

Available types are:
@table @code
@c Copied from https://docs.docker.com/network/

@item none

The default option. For this container, disable all networking. Usually used in
conjunction with a custom network driver. none is not available for swarm services.

@item bridge

Bridge networks are usually used when your applications run in standalone
containers that need to communicate.

@item host

For standalone containers, remove network isolation between the container and the Docker host, 
and use the host’s networking directly.

@item overlay

Overlay networks connect multiple Docker daemons together and enable swarm services to
communicate with each other. You can also use overlay networks to facilitate
communication between a swarm service and a standalone container, or between
two standalone containers on different Docker daemons. This strategy removes
the need to do OS-level routing between these containers.

@item ipvlan

IPvlan networks give users total control over both IPv4 and IPv6 addressing.
The VLAN driver builds on top of that in giving operators complete control of
layer 2 VLAN tagging and even IPvlan L3 routing for users interested in underlay
network integration.

@item macvlan

Macvlan networks allow you to assign a MAC address to a container, making it appear
as a physical device on your network. The Docker daemon routes traffic to containers
by their MAC addresses. Using the macvlan driver is sometimes the best choice when
dealing with legacy applications that expect to be directly connected to the physical
network, rather than routed through the Docker host’s network stack.

@end table")
  (additional-arguments
   (list-of-strings '())
   "Additional arguments to the docker command line interface.")
  (container-command
   (list-of-strings '())
   "Command to send into the container.")
  (pid-file-timeout
   (number 5)
   "If the docker container does not show up in @code{docker ps} as @code{running} in less than pid-file-timeout seconds, the container is considered as failing to start.

Note that some containers take a really long time to start, so you should adjust it accordingly."))

(define (serialize-volumes config)
  "Serialize list of pairs into flat list of @code{(\"-v\" \"HOST_PATH:CONTAINER_PATH\" ...)}"
  (append-map
   (lambda (volume-bind)
     (list "-v" (apply format #f "~a:~a~^:~a" volume-bind)))
   (docker-container-volumes config)))

(define (serialize-ports config)
  "Serialize list of either pairs, or lists into flat list of
@code{(\"-p\" \"NUMBER:NUMBER\" \"-p\" \"NUMBER:NUMBER/PROTOCOL\" ...)}"
  (append-map
   (lambda (port-bind)
     (list "-p" (apply format #f "~a:~a~^/~a" port-bind)))
   (docker-container-ports config)))

(define (serialize-environments config)
  "Serialize list of pairs into flat list of @code{(\"-e\" \"VAR=val\" \"-e\" \"VAR=val\" ...)}."
  (append-map
   (lambda (env-bind)
     (list "-e" (apply format #f "~a=~a" env-bind)))
   (docker-container-environments config)))

(define (docker-container-startup-script docker-cli container-name cid-file config)
  "Return a program file, that executes the startup sequence of the @code{docker-container-shepherd-service}."
  (let* ((image-name (docker-container-image-name config))
         (volumes (serialize-volumes config))
         (ports (serialize-ports config))
         (envs (serialize-environments config))
         (network (docker-container-network config))
         (additional-arguments (docker-container-additional-arguments config))
         (container-command (docker-container-container-command config)))
    (with-imported-modules
     '((guix build utils))
     (program-file
      (string-append "start-" container-name "-container")
      #~(let ((docker (string-append #$docker-cli "/bin/docker")))
          (use-modules (guix build utils))
          ;; These two commands should fail
          ;; they are there as a failsafe to
          ;; prevent contamination from unremoved containers
          (system* docker "stop" #$container-name)
          (system* docker "rm" #$container-name)
          (apply invoke `(,docker
                           "run"
                           ,(string-append "--name=" #$container-name)
                           ;; Automatically remove the container when stopping
                           ;; If you want persistent data, you need to use
                           ;; volume binds or other methods.
                           "--rm"
                           ,(string-append "--network=" #$network)
                           ;; Write to a cid file the container id, this allows
                           ;; for shepherd to manage container even when the process
                           ;; itself gets detached from the container
                           "--cidfile" #$cid-file
                           #$@volumes
                           #$@ports
                           #$@envs
                           #$@additional-arguments
                           ,#$image-name
                           #$@container-command)))))))

(define (docker-container-shepherd-service docker-cli config)
  "Return a shepherd-service that runs CONTAINER."
  (let* ((container-name (symbol->string (docker-container-name config)))
         (cid-file (string-append "/var/run/docker/" container-name ".pid"))
         (pid-file-timeout (docker-container-pid-file-timeout config)))
    (shepherd-service
     (provision (list (docker-container-name config)))
     (requirement `(dockerd))
     (documentation (docker-container-documentation config))
     (start #~(apply make-forkexec-constructor
                     `(,(list #$(docker-container-startup-script docker-cli container-name cid-file config))
                       ;; Watch the cid-file instead of the docker run command, as the daemon can
                       ;; still be running even when the command terminates
                       #:pid-file #$cid-file
                       #:pid-file-timeout #$pid-file-timeout)))
     (stop #~(lambda _
               (invoke
                (string-append #$docker-cli "/bin/docker")
                "stop"
                #$container-name)
               ;; Shepherd expects the stop command to return #f if it succeeds
               ;; docker stop should always succeed
               #f)))))

(define (list-of-docker-containers? val)
  (list-of docker-container?))

(define-configuration docker-configuration
  (docker
   (file-like docker)
   "Docker daemon package.")
  (docker-cli
   (file-like docker-cli)
   "Docker client package.")
  (containerd
   (file-like containerd)
   "containerd package.")
  (proxy
   (file-like docker-libnetwork-cmd-proxy)
   "The proxy package to support inter-container and outside-container
loop-back communications.")
  (enable-proxy?
   (boolean #t)
   "Enable or disable the user-land proxy (enabled by default).")
  (debug?
   (boolean #f)
   "Enable or disable debug output.")
  (enable-iptables?
   (boolean #t)
   "Enable addition of iptables rules (enabled by default).")
  (environment-variables
   (list '())
   "Environment variables to set for dockerd")
  (containers
   (list-of-docker-containers '())
   "List of docker containers to run as shepherd services.")
  (no-serialization))

(define (docker-container-shepherd-services config)
  "Return shepherd services for all containers inside config."
  (let ((docker-cli (docker-configuration-docker-cli config)))
    (map
     (lambda (container)
       (docker-container-shepherd-service
        docker-cli
        container))
     (docker-configuration-containers config))))

(define %docker-accounts
  (list (user-group (name "docker") (system? #t))))

(define (%containerd-activation config)
  (let ((state-dir "/var/lib/containerd"))
    #~(begin
        (use-modules (guix build utils))
        (mkdir-p #$state-dir))))

(define (%docker-activation config)
  (%containerd-activation config)
  (let ((state-dir "/var/lib/docker"))
    #~(begin
        (use-modules (guix build utils))
        (mkdir-p #$state-dir))))

(define (containerd-shepherd-service config)
  (let* ((package (docker-configuration-containerd config))
         (debug? (docker-configuration-debug? config))
         (containerd (docker-configuration-containerd config)))
    (shepherd-service
     (documentation "containerd daemon.")
     (provision '(containerd))
     (start #~(make-forkexec-constructor
               (list (string-append #$package "/bin/containerd")
                     #$@(if debug?
                            '("--log-level=debug")
                            '()))
               ;; For finding containerd-shim binary.
               #:environment-variables
               (list (string-append "PATH=" #$containerd "/bin"))
               #:pid-file "/run/containerd/containerd.pid"
               #:pid-file-timeout 300
               #:log-file "/var/log/containerd.log"))
     (stop #~(make-kill-destructor)))))

(define (docker-shepherd-service config)
  (let* ((docker (docker-configuration-docker config))
         (enable-proxy? (docker-configuration-enable-proxy? config))
         (enable-iptables? (docker-configuration-enable-iptables? config))
         (environment-variables (docker-configuration-environment-variables config))
         (proxy (docker-configuration-proxy config))
         (debug? (docker-configuration-debug? config)))
    (shepherd-service
           (documentation "Docker daemon.")
           (provision '(dockerd))
           (requirement '(containerd
                          dbus-system
                          elogind
                          file-system-/sys/fs/cgroup/blkio
                          file-system-/sys/fs/cgroup/cpu
                          file-system-/sys/fs/cgroup/cpuset
                          file-system-/sys/fs/cgroup/devices
                          file-system-/sys/fs/cgroup/memory
                          file-system-/sys/fs/cgroup/pids
                          networking
                          udev))
           (start #~(make-forkexec-constructor
                     (list (string-append #$docker "/bin/dockerd")
                           "-p" "/var/run/docker.pid"
                           #$@(if debug?
                                  '("--debug" "--log-level=debug")
                                  '())
                           #$@(if enable-proxy?
                                  (list "--userland-proxy=true"
                                        #~(string-append
                                           "--userland-proxy-path=" #$proxy "/bin/proxy"))
                                  '("--userland-proxy=false"))
                           (if #$enable-iptables?
                               "--iptables"
                               "--iptables=false")
                           "--containerd" "/run/containerd/containerd.sock")
                     #:environment-variables
                     (list #$@environment-variables)
                     #:pid-file "/var/run/docker.pid"
                     #:log-file "/var/log/docker.log"))
           (stop #~(make-kill-destructor)))))

(define docker-service-type
  (service-type (name 'docker)
                (description "Provide capability to run Docker application
bundles in Docker containers and optionally wrap those containers in shepherd services.")
                (extensions
                 (list
                  ;; Make sure the 'docker' command is available.
                  (service-extension profile-service-type
                                     (compose list docker-configuration-docker-cli))
                  (service-extension activation-service-type
                                     %docker-activation)
                  (service-extension shepherd-root-service-type
                                     (lambda (config)
                                       (cons* (containerd-shepherd-service config)
                                              (docker-shepherd-service config)
                                              (docker-container-shepherd-services config))))
                  (service-extension account-service-type
                                     (const %docker-accounts))))
                (compose concatenate)
                (extend (lambda (config containers)
                          (docker-configuration
                           (inherit config)
                           (containers (append containers (docker-configuration-containers config))))))
                (default-value (docker-configuration))))

\f
;;;
;;; Singularity.
;;;

(define %singularity-activation
  (with-imported-modules '((guix build utils))
    #~(begin
        (use-modules (guix build utils))

        (define %mount-directory
          "/var/singularity/mnt/")

        ;; Create the directories that Singularity 2.6 expects to find.  Make
        ;; them #o755 like the 'install-data-hook' rule in 'Makefile.am' of
        ;; Singularity 2.6.1.
        (for-each (lambda (directory)
                    (let ((directory (string-append %mount-directory
                                                    directory)))
                      (mkdir-p directory)
                      (chmod directory #o755)))
                  '("container" "final" "overlay" "session"))
        (chmod %mount-directory #o755))))

(define (singularity-setuid-programs singularity)
  "Return the setuid-root programs that SINGULARITY needs."
  (define helpers
    ;; The helpers, under a meaningful name.
    (computed-file "singularity-setuid-helpers"
                   #~(begin
                       (mkdir #$output)
                       (for-each (lambda (program)
                                   (symlink (string-append #$singularity
                                                           "/libexec/singularity"
                                                           "/bin/"
                                                           program "-suid")
                                            (string-append #$output
                                                           "/singularity-"
                                                           program
                                                           "-helper")))
                                 '("action" "mount" "start")))))

  (map file-like->setuid-program
       (list (file-append helpers "/singularity-action-helper")
             (file-append helpers "/singularity-mount-helper")
             (file-append helpers "/singularity-start-helper"))))

(define singularity-service-type
  (service-type (name 'singularity)
                (description
                 "Install the Singularity application bundle tool.")
                (extensions
                 (list (service-extension setuid-program-service-type
                                          singularity-setuid-programs)
                       (service-extension activation-service-type
                                          (const %singularity-activation))))
                (default-value singularity)))

debug log:

solving f3a347981f ...
found f3a347981f in https://yhetil.org/guix-patches/87mtae9d0t.fsf@disroot.org/
found 741bab5a8c in https://git.savannah.gnu.org/cgit/guix.git
preparing index
index prepared:
100644 741bab5a8c2e64ec6536e9e3ad8f3827d4d9a9ee	gnu/services/docker.scm

applying [1/1] https://yhetil.org/guix-patches/87mtae9d0t.fsf@disroot.org/
diff --git a/gnu/services/docker.scm b/gnu/services/docker.scm
index 741bab5a8c..f3a347981f 100644

1:86: trailing whitespace.
For standalone containers, remove network isolation between the container and the Docker host, 
Checking patch gnu/services/docker.scm...
Applied patch gnu/services/docker.scm cleanly.
warning: 1 line adds whitespace errors.

index at:
100644 f3a347981f0d09371f429a8d54118f083e60fc4f	gnu/services/docker.scm

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).