34 lines
1.4 KiB
Nix
34 lines
1.4 KiB
Nix
{pkgs, imageName, image, tag, extraRunConfig, containerName? "${imageName}-${tag}", wants ? [], after ? [], serviceType ? "oneshot"}:
|
|
|
|
let
|
|
imageId= "${imageName}:${tag}";
|
|
dockerBin = "${pkgs.docker}/bin/docker";
|
|
loggerBin = "${pkgs.logger}/bin/logger";
|
|
in
|
|
{
|
|
systemd.services."docker-load-run-${containerName}-container" = {
|
|
description = "Docker load and run ${containerName}-container";
|
|
wantedBy = [ "multi-user.target" ];
|
|
wants = [ "docker.service" "local-fs.target" ] ++ wants;
|
|
after = [ "docker.service" "local-fs.target" ] ++ after;
|
|
|
|
script = ''
|
|
if [[ "$(${dockerBin} images -aq ${imageId} 2> /dev/null)" == "" ]]; then
|
|
${dockerBin} load < ${image}
|
|
else
|
|
${loggerBin} -pdaemon.warning "an image with name ${imageId} already exists. Please use an other name or rename/remove the existing image, if you want use the new one."
|
|
fi
|
|
|
|
if [[ "$(${dockerBin} ps -qaf "name=${containerName}" 2> /dev/null)" == "" ]]; then
|
|
${dockerBin} run -d --name ${containerName} ${extraRunConfig} ${imageId}
|
|
else
|
|
${loggerBin} -pdaemon.warning "a container with name ${containerName} already exists. Please use an other name or rename/remove the existing containerName, if you want use the new one."
|
|
fi
|
|
'';
|
|
|
|
serviceConfig = {
|
|
Type = serviceType;
|
|
};
|
|
};
|
|
}
|