at 17.09-beta 5.5 kB view raw
1# Systemd services for docker. 2 3{ config, lib, pkgs, ... }: 4 5with lib; 6 7let 8 9 cfg = config.virtualisation.docker; 10 proxy_env = config.networking.proxy.envVars; 11 12in 13 14{ 15 ###### interface 16 17 options.virtualisation.docker = { 18 enable = 19 mkOption { 20 type = types.bool; 21 default = false; 22 description = 23 '' 24 This option enables docker, a daemon that manages 25 linux containers. Users in the "docker" group can interact with 26 the daemon (e.g. to start or stop containers) using the 27 <command>docker</command> command line tool. 28 ''; 29 }; 30 31 listenOptions = 32 mkOption { 33 type = types.listOf types.str; 34 default = ["/var/run/docker.sock"]; 35 description = 36 '' 37 A list of unix and tcp docker should listen to. The format follows 38 ListenStream as described in systemd.socket(5). 39 ''; 40 }; 41 42 enableOnBoot = 43 mkOption { 44 type = types.bool; 45 default = true; 46 description = 47 '' 48 When enabled dockerd is started on boot. This is required for 49 container, which are created with the 50 <literal>--restart=always</literal> flag, to work. If this option is 51 disabled, docker might be started on demand by socket activation. 52 ''; 53 }; 54 55 liveRestore = 56 mkOption { 57 type = types.bool; 58 default = true; 59 description = 60 '' 61 Allow dockerd to be restarted without affecting running container. 62 This option is incompatible with docker swarm. 63 ''; 64 }; 65 66 storageDriver = 67 mkOption { 68 type = types.nullOr (types.enum ["aufs" "btrfs" "devicemapper" "overlay" "overlay2" "zfs"]); 69 default = null; 70 description = 71 '' 72 This option determines which Docker storage driver to use. By default 73 it let's docker automatically choose preferred storage driver. 74 ''; 75 }; 76 77 logDriver = 78 mkOption { 79 type = types.enum ["none" "json-file" "syslog" "journald" "gelf" "fluentd" "awslogs" "splunk" "etwlogs" "gcplogs"]; 80 default = "journald"; 81 description = 82 '' 83 This option determines which Docker log driver to use. 84 ''; 85 }; 86 87 extraOptions = 88 mkOption { 89 type = types.separatedString " "; 90 default = ""; 91 description = 92 '' 93 The extra command-line options to pass to 94 <command>docker</command> daemon. 95 ''; 96 }; 97 98 autoPrune = { 99 enable = mkOption { 100 type = types.bool; 101 default = false; 102 description = '' 103 Whether to periodically prune Docker resources. If enabled, a 104 systemd timer will run <literal>docker system prune -f</literal> 105 as specified by the <literal>dates</literal> option. 106 ''; 107 }; 108 109 flags = mkOption { 110 type = types.listOf types.str; 111 default = []; 112 example = [ "--all" ]; 113 description = '' 114 Any additional flags passed to <command>docker system prune</command>. 115 ''; 116 }; 117 118 dates = mkOption { 119 default = "weekly"; 120 type = types.str; 121 description = '' 122 Specification (in the format described by 123 <citerefentry><refentrytitle>systemd.time</refentrytitle> 124 <manvolnum>7</manvolnum></citerefentry>) of the time at 125 which the prune will occur. 126 ''; 127 }; 128 }; 129 }; 130 131 ###### implementation 132 133 config = mkIf cfg.enable (mkMerge [{ 134 environment.systemPackages = [ pkgs.docker ]; 135 users.extraGroups.docker.gid = config.ids.gids.docker; 136 systemd.packages = [ pkgs.docker ]; 137 138 systemd.services.docker = { 139 wantedBy = optional cfg.enableOnBoot "multi-user.target"; 140 environment = proxy_env; 141 serviceConfig = { 142 ExecStart = [ 143 "" 144 '' 145 ${pkgs.docker}/bin/dockerd \ 146 --group=docker \ 147 --host=fd:// \ 148 --log-driver=${cfg.logDriver} \ 149 ${optionalString (cfg.storageDriver != null) "--storage-driver=${cfg.storageDriver}"} \ 150 ${optionalString cfg.liveRestore "--live-restore" } \ 151 ${cfg.extraOptions} 152 '']; 153 ExecReload=[ 154 "" 155 "${pkgs.procps}/bin/kill -s HUP $MAINPID" 156 ]; 157 }; 158 159 path = [ pkgs.kmod ] ++ (optional (cfg.storageDriver == "zfs") pkgs.zfs); 160 }; 161 162 systemd.sockets.docker = { 163 description = "Docker Socket for the API"; 164 wantedBy = [ "sockets.target" ]; 165 socketConfig = { 166 ListenStream = cfg.listenOptions; 167 SocketMode = "0660"; 168 SocketUser = "root"; 169 SocketGroup = "docker"; 170 }; 171 }; 172 173 174 systemd.services.docker-prune = { 175 description = "Prune docker resources"; 176 177 restartIfChanged = false; 178 unitConfig.X-StopOnRemoval = false; 179 180 serviceConfig.Type = "oneshot"; 181 182 script = '' 183 ${pkgs.docker}/bin/docker system prune -f ${toString cfg.autoPrune.flags} 184 ''; 185 186 startAt = optional cfg.autoPrune.enable cfg.autoPrune.dates; 187 }; 188 } 189 ]); 190 191 imports = [ 192 (mkRemovedOptionModule ["virtualisation" "docker" "socketActivation"] "This option was removed in favor of starting docker at boot") 193 ]; 194 195}