2016-10-03 19:07:33 +02:00
|
|
|
# Examples of using the docker tools to build packages.
|
|
|
|
#
|
|
|
|
# This file defines several docker images. In order to use an image,
|
|
|
|
# build its derivation with `nix-build`, and then load the result with
|
|
|
|
# `docker load`. For example:
|
|
|
|
#
|
|
|
|
# $ nix-build '<nixpkgs>' -A dockerTools.examples.redis
|
|
|
|
# $ docker load < result
|
|
|
|
|
2017-08-25 11:47:28 +02:00
|
|
|
{ pkgs, buildImage, pullImage, shadowSetup, buildImageWithNixDb }:
|
2016-10-03 19:07:33 +02:00
|
|
|
|
|
|
|
rec {
|
|
|
|
# 1. basic example
|
|
|
|
bash = buildImage {
|
|
|
|
name = "bash";
|
|
|
|
contents = pkgs.bashInteractive;
|
|
|
|
};
|
|
|
|
|
|
|
|
# 2. service example, layered on another image
|
|
|
|
redis = buildImage {
|
|
|
|
name = "redis";
|
|
|
|
tag = "latest";
|
|
|
|
|
|
|
|
# for example's sake, we can layer redis on top of bash or debian
|
|
|
|
fromImage = bash;
|
|
|
|
# fromImage = debian;
|
|
|
|
|
|
|
|
contents = pkgs.redis;
|
|
|
|
runAsRoot = ''
|
|
|
|
mkdir -p /data
|
|
|
|
'';
|
|
|
|
|
|
|
|
config = {
|
|
|
|
Cmd = [ "/bin/redis-server" ];
|
|
|
|
WorkingDir = "/data";
|
|
|
|
Volumes = {
|
|
|
|
"/data" = {};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# 3. another service example
|
|
|
|
nginx = let
|
|
|
|
nginxPort = "80";
|
|
|
|
nginxConf = pkgs.writeText "nginx.conf" ''
|
|
|
|
user nginx nginx;
|
|
|
|
daemon off;
|
|
|
|
error_log /dev/stdout info;
|
|
|
|
pid /dev/null;
|
|
|
|
events {}
|
|
|
|
http {
|
|
|
|
access_log /dev/stdout;
|
|
|
|
server {
|
|
|
|
listen ${nginxPort};
|
|
|
|
index index.html;
|
|
|
|
location / {
|
|
|
|
root ${nginxWebRoot};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
nginxWebRoot = pkgs.writeTextDir "index.html" ''
|
|
|
|
<html><body><h1>Hello from NGINX</h1></body></html>
|
|
|
|
'';
|
|
|
|
in
|
|
|
|
buildImage {
|
|
|
|
name = "nginx-container";
|
|
|
|
contents = pkgs.nginx;
|
|
|
|
|
|
|
|
runAsRoot = ''
|
|
|
|
#!${pkgs.stdenv.shell}
|
|
|
|
${shadowSetup}
|
|
|
|
groupadd --system nginx
|
|
|
|
useradd --system --gid nginx nginx
|
|
|
|
'';
|
|
|
|
|
|
|
|
config = {
|
|
|
|
Cmd = [ "nginx" "-c" nginxConf ];
|
|
|
|
ExposedPorts = {
|
|
|
|
"${nginxPort}/tcp" = {};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
# 4. example of pulling an image. could be used as a base for other images
|
2017-08-25 11:47:28 +02:00
|
|
|
nixFromDockerHub = pullImage {
|
2017-05-26 18:06:24 +02:00
|
|
|
imageName = "nixos/nix";
|
2018-04-03 10:26:03 +02:00
|
|
|
imageDigest = "sha256:20d9485b25ecfd89204e843a962c1bd70e9cc6858d65d7f5fadc340246e2116b";
|
|
|
|
sha256 = "0mqjy3zq2v6rrhizgb9nvhczl87lcfphq9601wcprdika2jz7qh8";
|
|
|
|
finalImageTag = "1.11";
|
2017-05-26 18:06:24 +02:00
|
|
|
};
|
2016-10-03 19:07:33 +02:00
|
|
|
|
|
|
|
# 5. example of multiple contents, emacs and vi happily coexisting
|
|
|
|
editors = buildImage {
|
|
|
|
name = "editors";
|
|
|
|
contents = [
|
|
|
|
pkgs.coreutils
|
|
|
|
pkgs.bash
|
|
|
|
pkgs.emacs
|
|
|
|
pkgs.vim
|
|
|
|
pkgs.nano
|
|
|
|
];
|
|
|
|
};
|
2017-08-25 11:47:28 +02:00
|
|
|
|
2018-01-01 09:13:40 +01:00
|
|
|
# 6. nix example to play with the container nix store
|
2017-08-25 11:47:28 +02:00
|
|
|
# docker run -it --rm nix nix-store -qR $(nix-build '<nixpkgs>' -A nix)
|
|
|
|
nix = buildImageWithNixDb {
|
|
|
|
name = "nix";
|
|
|
|
contents = [
|
2017-09-25 09:39:15 +02:00
|
|
|
# nix-store uses cat program to display results as specified by
|
|
|
|
# the image env variable NIX_PAGER.
|
|
|
|
pkgs.coreutils
|
2017-08-25 11:47:28 +02:00
|
|
|
pkgs.nix
|
|
|
|
];
|
2017-09-25 09:39:15 +02:00
|
|
|
config = {
|
|
|
|
Env = [ "NIX_PAGER=cat" ];
|
|
|
|
};
|
2017-08-25 11:47:28 +02:00
|
|
|
};
|
2018-02-01 16:13:05 +01:00
|
|
|
|
|
|
|
# 7. example of adding something on top of an image pull by our
|
|
|
|
# dockerTools chain.
|
|
|
|
onTopOfPulledImage = buildImage {
|
|
|
|
name = "onTopOfPulledImage";
|
|
|
|
fromImage = nixFromDockerHub;
|
|
|
|
contents = [ pkgs.hello ];
|
|
|
|
};
|
2018-05-23 02:25:04 +02:00
|
|
|
|
|
|
|
# 8. regression test for erroneous use of eval and string expansion.
|
|
|
|
# See issue #34779 and PR #40947 for details.
|
|
|
|
runAsRootExtraCommands = pkgs.dockerTools.buildImage {
|
|
|
|
name = "runAsRootExtraCommands";
|
|
|
|
contents = [ pkgs.coreutils ];
|
|
|
|
# The parens here are to create problematic bash to embed and eval. In case
|
|
|
|
# this is *embedded* into the script (with nix expansion) the initial quotes
|
|
|
|
# will close the string and the following parens are unexpected
|
|
|
|
runAsRoot = ''echo "(runAsRoot)" > runAsRoot'';
|
|
|
|
extraCommands = ''echo "(extraCommand)" > extraCommands'';
|
|
|
|
};
|
2016-10-03 19:07:33 +02:00
|
|
|
}
|