Interactive online version Binder badge. Generated from tutorials/03-images/01-creating-a-docker-image.ipynb.

Creating a Docker image

In this tutorial we will create Docker images using Nix and the dockerTools functions in Nixpkgs.

Create a simple image

In the following example we create a Docker image that contains only Python.

[1]:
%%file default.nix

with import (fetchTarball "channel:nixos-20.03") {};

dockerTools.buildImage {
  name = "python";
  config = {
    Cmd = [
      "${python3}/bin/python3 -c 'print(\"hello\")'"
    ];
  };
}
Writing default.nix

Let’s build the image.

[2]:
! nix-build
these derivations will be built:
  /nix/store/9lisv1a19p0r81m00dijmh1xzakxv15n-python-config.json.drv
  /nix/store/pzl6mlzihdq4p9r4wscv37gc96lkx2mn-docker-layer-python.drv
  /nix/store/rjmvipf80cmmn15ib48mvf181vjcyjdx-runtime-deps.drv
  /nix/store/dif5sg0rgsc1w1qbp28szgflh903hdpl-docker-image-python.tar.gz.drv
these paths will be fetched (96.32 MiB download, 365.59 MiB unpacked):
  /nix/store/0nkl1p8qs3haxjw8f51nxcyzxghjnqcd-pigz-2.4
  /nix/store/0np7xb2g6kybsw13dh2g7jbrvp7my05m-jshon-20160111.2
  /nix/store/5dclw2cwpc032q52ga8i5mca35i9iyyr-tzdata-2019c
  /nix/store/5pg29xh02ayywh1k1ws6bi94xwp3mndh-perl5.30.1-Time-Duration-1.21
  /nix/store/6ixa8br0v4z3pp57nxlck5gr2jwv8qac-perl5.30.1-IPC-Run-20180523.0
  /nix/store/bvw0z15pgndx2s9am5rw3j7bncq6lbri-popt-1.16
  /nix/store/cdyc50j2xcb6bfnkkvjb2g93f12jx6i8-iana-etc-20190731
  /nix/store/das433x42rckw9dd6z8nhrizs7ykiawh-jq-1.6-lib
  /nix/store/g0lvwdrbdin362xx5nsy2lm70p1v7j9v-tarsum
  /nix/store/i2xqpxkbnyvgwvwmd595sbqg73g5ivy3-perl5.30.1-TimeDate-2.31
  /nix/store/kbkhv4z3ph27lp1hh0lm4z8jppn6895y-go-1.14.1
  /nix/store/kh9v9ldvwdpgs4qfwqpiwhdwch17miiq-jq-1.6-dev
  /nix/store/nb6y5b2x952pzbw2s5ya5j57x1mi54pk-perl5.30.1-IO-Tty-1.14
  /nix/store/nzabl7gh0l7zjjb02vagz276njblv11s-jansson-2.12
  /nix/store/qmq5f0iik25i0hflspzvpzz01p2r796g-moreutils-0.63
  /nix/store/r2wvgnr54vmwnjvzyqdixv8xbn362jgh-mailcap-2.1.48
  /nix/store/rdc04nsv5f6618bw6xsj2ngv284zcfjl-rsync-3.1.3
  /nix/store/ywpyrfrv2yr1qzgxx6m9f2pxqh0ns9mm-jq-1.6-bin
  /nix/store/zbpv6awbqrs6r3x4zyb9a14zjzpcmj23-onig-6.9.4
copying path '/nix/store/5pg29xh02ayywh1k1ws6bi94xwp3mndh-perl5.30.1-Time-Duration-1.21' from 'https://cache.nixos.org'...
copying path '/nix/store/i2xqpxkbnyvgwvwmd595sbqg73g5ivy3-perl5.30.1-TimeDate-2.31' from 'https://cache.nixos.org'...
copying path '/nix/store/cdyc50j2xcb6bfnkkvjb2g93f12jx6i8-iana-etc-20190731' from 'https://cache.nixos.org'...
copying path '/nix/store/nzabl7gh0l7zjjb02vagz276njblv11s-jansson-2.12' from 'https://cache.nixos.org'...
copying path '/nix/store/r2wvgnr54vmwnjvzyqdixv8xbn362jgh-mailcap-2.1.48' from 'https://cache.nixos.org'...
copying path '/nix/store/0np7xb2g6kybsw13dh2g7jbrvp7my05m-jshon-20160111.2' from 'https://cache.nixos.org'...
copying path '/nix/store/zbpv6awbqrs6r3x4zyb9a14zjzpcmj23-onig-6.9.4' from 'https://cache.nixos.org'...
copying path '/nix/store/nb6y5b2x952pzbw2s5ya5j57x1mi54pk-perl5.30.1-IO-Tty-1.14' from 'https://cache.nixos.org'...
copying path '/nix/store/0nkl1p8qs3haxjw8f51nxcyzxghjnqcd-pigz-2.4' from 'https://cache.nixos.org'...
copying path '/nix/store/6ixa8br0v4z3pp57nxlck5gr2jwv8qac-perl5.30.1-IPC-Run-20180523.0' from 'https://cache.nixos.org'...
copying path '/nix/store/das433x42rckw9dd6z8nhrizs7ykiawh-jq-1.6-lib' from 'https://cache.nixos.org'...
copying path '/nix/store/bvw0z15pgndx2s9am5rw3j7bncq6lbri-popt-1.16' from 'https://cache.nixos.org'...
copying path '/nix/store/qmq5f0iik25i0hflspzvpzz01p2r796g-moreutils-0.63' from 'https://cache.nixos.org'...
copying path '/nix/store/rdc04nsv5f6618bw6xsj2ngv284zcfjl-rsync-3.1.3' from 'https://cache.nixos.org'...
copying path '/nix/store/ywpyrfrv2yr1qzgxx6m9f2pxqh0ns9mm-jq-1.6-bin' from 'https://cache.nixos.org'...
copying path '/nix/store/5dclw2cwpc032q52ga8i5mca35i9iyyr-tzdata-2019c' from 'https://cache.nixos.org'...
copying path '/nix/store/kh9v9ldvwdpgs4qfwqpiwhdwch17miiq-jq-1.6-dev' from 'https://cache.nixos.org'...
building '/nix/store/9lisv1a19p0r81m00dijmh1xzakxv15n-python-config.json.drv'...
copying path '/nix/store/kbkhv4z3ph27lp1hh0lm4z8jppn6895y-go-1.14.1' from 'https://cache.nixos.org'...
copying path '/nix/store/g0lvwdrbdin362xx5nsy2lm70p1v7j9v-tarsum' from 'https://cache.nixos.org'...
building '/nix/store/pzl6mlzihdq4p9r4wscv37gc96lkx2mn-docker-layer-python.drv'...
No contents to add to layer.
Packing layer...
Computing layer checksum...
Finished building layer 'python'
building '/nix/store/rjmvipf80cmmn15ib48mvf181vjcyjdx-runtime-deps.drv'...
building '/nix/store/dif5sg0rgsc1w1qbp28szgflh903hdpl-docker-image-python.tar.gz.drv'...
Adding layer...
tar: Removing leading `/' from member names
Adding meta...
Cooking the image...
Finished.
/nix/store/xmsvz93hfxllzymif1gpc0h3yxqckwyy-docker-image-python.tar.gz

Now we have our Docker image in the store. Note how small it is

[3]:
! du -h $(nix-build)
32M     /nix/store/xmsvz93hfxllzymif1gpc0h3yxqckwyy-docker-image-python.tar.gz

To run the image as a container, just load it with docker load

docker load < $(nix-build)

It’s not shown here because the Docker daemon is not available.