git.iko.soy/typeable/nixpkgs
on master help sign in
add examples file
commitb9007c8d83f91e7e8614559af292de4950c616c1 authorAllen Nelson <anelson@narrativescience.com> dateMon 3 Oct 2016, 12:07 (10 years ago) parent4abe579
2 files changed+109
pkgs/build-support/docker/default.nix+5 pkgs/build-support/docker/examples.nix+104
pkgs/build-support/docker/default.nix+5
@@ -7,6 +7,7 @@
77 go,
88 jshon,
99 lib,
10+ pkgs,
1011 pigz,
1112 runCommand,
1213 shadow,
@@ -23,6 +24,10 @@
2324
2425rec {
2526
27+ examples = import ./examples.nix {
28+ inherit pkgs buildImage pullImage shadowSetup;
29+ };
30+
2631 pullImage = callPackage ./pull.nix {};
2732
2833 # We need to sum layer.tar, not a directory, hence tarsum instead of nix-hash.
pkgs/build-support/docker/examples.nixnew file+104
@@ -0,0 +1,104 @@
1+# Examples of using the docker tools to build packages.
2+#
3+# This file defines several docker images. In order to use an image,
4+# build its derivation with `nix-build`, and then load the result with
5+# `docker load`. For example:
6+#
7+# $ nix-build '<nixpkgs>' -A dockerTools.examples.redis
8+# $ docker load < result
9+
10+{ pkgs, buildImage, pullImage, shadowSetup }:
11+
12+rec {
13+ # 1. basic example
14+ bash = buildImage {
15+ name = "bash";
16+ contents = pkgs.bashInteractive;
17+ };
18+
19+ # 2. service example, layered on another image
20+ redis = buildImage {
21+ name = "redis";
22+ tag = "latest";
23+
24+ # for example's sake, we can layer redis on top of bash or debian
25+ fromImage = bash;
26+ # fromImage = debian;
27+
28+ contents = pkgs.redis;
29+ runAsRoot = ''
30+ mkdir -p /data
31+ '';
32+
33+ config = {
34+ Cmd = [ "/bin/redis-server" ];
35+ WorkingDir = "/data";
36+ Volumes = {
37+ "/data" = {};
38+ };
39+ };
40+ };
41+
42+ # 3. another service example
43+ nginx = let
44+ nginxPort = "80";
45+ nginxConf = pkgs.writeText "nginx.conf" ''
46+ user nginx nginx;
47+ daemon off;
48+ error_log /dev/stdout info;
49+ pid /dev/null;
50+ events {}
51+ http {
52+ access_log /dev/stdout;
53+ server {
54+ listen ${nginxPort};
55+ index index.html;
56+ location / {
57+ root ${nginxWebRoot};
58+ }
59+ }
60+ }
61+ '';
62+ nginxWebRoot = pkgs.writeTextDir "index.html" ''
63+ <html><body><h1>Hello from NGINX</h1></body></html>
64+ '';
65+ in
66+ buildImage {
67+ name = "nginx-container";
68+ contents = pkgs.nginx;
69+
70+ runAsRoot = ''
71+ #!${pkgs.stdenv.shell}
72+ ${shadowSetup}
73+ groupadd --system nginx
74+ useradd --system --gid nginx nginx
75+ '';
76+
77+ config = {
78+ Cmd = [ "nginx" "-c" nginxConf ];
79+ ExposedPorts = {
80+ "${nginxPort}/tcp" = {};
81+ };
82+ };
83+ };
84+
85+ # 4. example of pulling an image. could be used as a base for other images
86+ debian = pullImage {
87+ imageName = "debian";
88+ imageTag = "jessie";
89+ # this hash will need change if the tag is updated at docker hub
90+ sha256 = "18kd495lc2k35h03bpcbdjnix17nlqbwf6nmq3sb161blf0dk14q";
91+ };
92+
93+ # 5. example of multiple contents, emacs and vi happily coexisting
94+ editors = buildImage {
95+ name = "editors";
96+ contents = [
97+ pkgs.coreutils
98+ pkgs.bash
99+ pkgs.emacs
100+ pkgs.vim
101+ pkgs.nano
102+ ];
103+ };
104+}