Created
July 28, 2017 09:09
-
-
Save mbrock/22cad954cb01dd872805db5cea6ea338 to your computer and use it in GitHub Desktop.
Nix service thing for Docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ config, pkgs, ... }: | |
let dockerService = { | |
name, | |
vhosts ? [], | |
public ? (if builtins.length vhosts > 0 then true else false), | |
image, | |
user ? "docker", | |
cmd ? "", | |
volumes ? [], | |
ports ? [], | |
environment ? [] | |
}: { | |
wantedBy = ["multi-user.target"]; | |
after = ["docker.service"]; | |
requires = ["docker.service"]; | |
serviceConfig = { | |
User = user; | |
ExecStartPre = '' | |
-${pkgs.docker}/bin/docker rm -f ${name} | |
''; | |
ExecStart = '' | |
${pkgs.docker}/bin/docker run \ | |
${builtins.concatStringsSep " " (map (p: "-p ${p}") ports)} \ | |
${builtins.concatStringsSep " " (map (v: "-v ${v}") volumes)} \ | |
${builtins.concatStringsSep " " (map (e: "-e ${e}") environment)} \ | |
${if public then | |
"-e VIRTUAL_HOST=${ | |
builtins.concatStringsSep "," | |
(vhosts ++ (if public then ["~^${name}\\..*"] else []))}" | |
else | |
""} \ | |
--name=${name} --user=${user} ${image} ${cmd} | |
''; | |
ExecStop = '' | |
${pkgs.docker}/bin/docker stop ${name} ; \ | |
${pkgs.docker}/bin/docker rm -f ${name} | |
''; | |
}; | |
}; | |
in { | |
# "example" = dockerService { | |
# name = "example"; | |
# image = "node"; | |
# public = true; | |
# ports = ["80"]; | |
# vhosts = ["api.example.com"]; | |
# cmd = "node /app/server.js"; | |
# volumes = ["/src/example:/app:ro"]; | |
# }; | |
## This image makes automatic reverse proxies based on the vhosts | |
# proxy = dockerService { | |
# name = "proxy"; | |
# image = "jwilder/nginx-proxy"; | |
# ports = ["80:80"]; | |
# volumes = ["/var/run/docker.sock:/tmp/docker.sock:ro"]; | |
# }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment