{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating a Docker image\n", "\n", "In this tutorial we will create Docker images using Nix and the `dockerTools` functions in Nixpkgs." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Create a simple image\n", "\n", "In the following example we create a Docker image that contains only Python." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%file default.nix\n", "\n", "with import (fetchTarball \"channel:nixos-20.03\") {};\n", "\n", "dockerTools.buildImage {\n", " name = \"python\";\n", " config = {\n", " Cmd = [\n", " \"${python3}/bin/python3 -c 'print(\\\"hello\\\")'\"\n", " ];\n", " };\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's build the image." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "! nix-build" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we have our Docker image in the store. Note how small it is" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "! du -h $(nix-build)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To run the image as a container, just load it with `docker load`\n", "```sh\n", "docker load < $(nix-build)\n", "```\n", "It's not shown here because the Docker daemon is not available." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.5" } }, "nbformat": 4, "nbformat_minor": 2 }