Skip to content

Instantly share code, notes, and snippets.

@matejc
Last active February 16, 2016 11:30
Show Gist options
  • Save matejc/6ea73e0f39d84ad5a568 to your computer and use it in GitHub Desktop.
Save matejc/6ea73e0f39d84ad5a568 to your computer and use it in GitHub Desktop.
systemd integration for docker
{ config, lib, pkgs, ... }:
with lib;
let
cfg = config.services.dockerctl;
dataDir = cfg.dataDir;
containers = filterAttrs (n: container: container.enable) cfg.containers;
runServiceFun = name: container:
let
extraRunOptions = toString container.extraRunOptions;
in
{
description = "Docker container ${name}";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" "docker.service" ];
requires = [ "docker.service" ];
path = [ pkgs.docker ];
serviceConfig = {
ExecStart = "${pkgs.systemd-docker}/bin/systemd-docker run --rm --name=${name} ${extraRunOptions} ${container.image}";
ExecStop = "${pkgs.docker}/bin/docker kill ${name}";
TimeoutStopSec = "10";
Type = "notify";
NotifyAccess = "all";
};
};
runServices = mapAttrs' (n: v: nameValuePair ("dockerctl-" + n) (runServiceFun n v)) containers;
in
{
options = {
services.dockerctl = {
enable = mkOption {
default = false;
type = types.bool;
description = ''
Enable DockerCtl.
'';
};
containers = mkOption {
type = types.attrsOf types.optionSet;
default = {};
description = "Set of containers.";
options = singleton ({ name, config, ... }:
{
options = {
enable = mkOption {
type = types.bool;
default = true;
description = ''
Whether this container should be enabled.
'';
};
extraRunOptions = mkOption {
type = types.listOf types.str;
default = [ ];
example = [ "-p 8080:80" ];
description = ''
Extra run options (https://docs.docker.com/engine/reference/commandline/run/).
'';
};
image = mkOption {
type = types.str;
description = ''
Docker image: NAME[:TAG] or [REGISTRY_HOST[:REGISTRY_PORT]/]NAME[:TAG]
'';
};
};
});
};
};
};
config = mkIf cfg.enable ({
systemd.services = runServices;
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment