at 17.09-beta 104 lines 2.4 kB view raw
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 12rec { 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 nix = pullImage { 87 imageName = "nixos/nix"; 88 imageTag = "1.11"; 89 # this hash will need change if the tag is updated at docker hub 90 sha256 = "1gk4bq05vl3rj3mh4mlbl4iicgndmimlv8jvkhdk4hrv0r44bwr3"; 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}