summary refs log tree commit diff
path: root/pkgs/build-support/docker/examples.nix
diff options
context:
space:
mode:
authorAllen Nelson <anelson@narrativescience.com>2016-10-03 12:07:33 -0500
committerAllen Nelson <anelson@narrativescience.com>2016-10-03 12:07:33 -0500
commitb9007c8d83f91e7e8614559af292de4950c616c1 (patch)
tree7002aa01265d1ab7360f7bbf180e8c84d716b8c9 /pkgs/build-support/docker/examples.nix
parent4abe579250242512973f3e32ae7560a7dd3b9656 (diff)
downloadnixpkgs-b9007c8d83f91e7e8614559af292de4950c616c1.tar
nixpkgs-b9007c8d83f91e7e8614559af292de4950c616c1.tar.gz
nixpkgs-b9007c8d83f91e7e8614559af292de4950c616c1.tar.bz2
nixpkgs-b9007c8d83f91e7e8614559af292de4950c616c1.tar.lz
nixpkgs-b9007c8d83f91e7e8614559af292de4950c616c1.tar.xz
nixpkgs-b9007c8d83f91e7e8614559af292de4950c616c1.tar.zst
nixpkgs-b9007c8d83f91e7e8614559af292de4950c616c1.zip
add examples file
Diffstat (limited to 'pkgs/build-support/docker/examples.nix')
-rw-r--r--pkgs/build-support/docker/examples.nix104
1 files changed, 104 insertions, 0 deletions
diff --git a/pkgs/build-support/docker/examples.nix b/pkgs/build-support/docker/examples.nix
new file mode 100644
index 00000000000..cce8f53834e
--- /dev/null
+++ b/pkgs/build-support/docker/examples.nix
@@ -0,0 +1,104 @@
+# 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
+
+{ pkgs, buildImage, pullImage, shadowSetup }:
+
+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
+  debian = pullImage {
+    imageName = "debian";
+    imageTag = "jessie";
+    # this hash will need change if the tag is updated at docker hub
+    sha256 = "18kd495lc2k35h03bpcbdjnix17nlqbwf6nmq3sb161blf0dk14q";
+  };
+
+  # 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
+    ];
+  };
+}