Docker: Difference between revisions

From NixOS Wiki
imported>Kadimisetty
imported>Raboof
(mention running docker containers with virtualisation.oci-containers - still missing further docs though :))
Line 1: Line 1:
[https://docker.com Docker] is a utility to pack, ship and run any application as a lightweight container.  
[https://docker.com Docker] is a utility to pack, ship and run any application as a lightweight container.  
= Docker on NixOS =


== Installation ==
== Installation ==
Line 43: Line 45:


The <code>setSocketVariable</code> option sets the <code>DOCKER_HOST</code> variable to the rootless Docker instance for normal users by default.
The <code>setSocketVariable</code> option sets the <code>DOCKER_HOST</code> variable to the rootless Docker instance for normal users by default.
== docker images as systemd services ==
To make sure some docker images are running as systemd services, you can use 'oci-containers':
<syntaxHighlight lang=nix>
virtualisation.oci-containers.containers = [
  ...
];
</syntaxHighlight>
=  Creating images =


== Building a docker image with nixpkgs ==
== Building a docker image with nixpkgs ==
Line 147: Line 161:
</syntaxHighlight>
</syntaxHighlight>


== Using Nix in containers ==
= Using Nix in containers =


While [https://nixos.org/manual/nixpkgs/stable/#sec-pkgs-dockerTools dockerTools] allows to build lightweight containers, it requires <code>nix</code> to be installed on the host system. An alternative are docker images with nix preinstalled:
While [https://nixos.org/manual/nixpkgs/stable/#sec-pkgs-dockerTools dockerTools] allows to build lightweight containers, it requires <code>nix</code> to be installed on the host system. An alternative are docker images with nix preinstalled:
Line 154: Line 168:
* [https://hub.docker.com/r/nixpkgs/nix nixpkgs/nix] (built from https://github.com/nix-community/docker-nixpkgs)
* [https://hub.docker.com/r/nixpkgs/nix nixpkgs/nix] (built from https://github.com/nix-community/docker-nixpkgs)


== See also ==
= See also =


[[Workgroup:Container]]
[[Workgroup:Container]]


As of 22.05 [https://search.nixos.org/options?query=virtualisation.docker.rootless rootless] docker is available. Alternatively you can use [https://nixos.wiki/wiki/Podman Podman].
Alternatively you can use [https://nixos.wiki/wiki/Podman Podman].


[[Category:Cookbook]]
[[Category:Cookbook]]

Revision as of 12:33, 15 June 2023

Docker is a utility to pack, ship and run any application as a lightweight container.

Docker on NixOS

Installation

To install docker, add the following to your your NixOS configuration:

virtualisation.docker.enable = true;

More options are available.

Adding users to the docker group will provide them access to the socket:

users.users.<myuser>.extraGroups = [ "docker" ];

If you prefer, you could achieve the same with this:

users.extraGroups.docker.members = [ "username-with-access-to-socket" ];

If you're still unable to get access to the socket, you might have to re-login.

Warning: Beware that the docker group membership is effectively equivalent to being root!

Note: If you use the btrfs filesystem, you might need to set the storageDriver option:

virtualisation.docker.storageDriver = "btrfs"

Rootless docker

To use docker in rootless mode, you can activate the rootless option:

virtualisation.docker.rootless = {
  enable = true;
  setSocketVariable = true;
};

The setSocketVariable option sets the DOCKER_HOST variable to the rootless Docker instance for normal users by default.

docker images as systemd services

To make sure some docker images are running as systemd services, you can use 'oci-containers':

virtualisation.oci-containers.containers = [
   ...
];

Creating images

Building a docker image with nixpkgs

There is an entry for dockerTools in the nixpkgs manual for reference. In the linked page they give the following example config:

buildImage {
  name = "redis";
  tag = "latest";

  fromImage = someBaseImage;
  fromImageName = null;
  fromImageTag = "latest";

  copyToRoot = pkgs.buildEnv {
    name = "image-root";
    paths = [ pkgs.redis ];
    pathsToLink = [ "/bin" ];
  };

  runAsRoot = ''
    #!${pkgs.runtimeShell}
    mkdir -p /data
  '';

  config = {
    Cmd = [ "/bin/redis-server" ];
    WorkingDir = "/data";
    Volumes = { "/data" = { }; };
  };

  diskSize = 1024;
  buildVMMemorySize = 512;
}

More examples can be found in the nixpkgs repo.

Also check out the excellent article by lethalman about building minimal docker images with nix.

Reproducible image dates

The manual advises against using created = "now", as that prevents images from being reproducible.

An alternative, if using flakes, is to do created = builtins.substring 0 8 self.lastModifiedDate, which uses the commit date, and is therefore reproducible.

How to calculate the sha256 of a pulled image

The sha256 argument of the dockerTools.pullImage function is the checksum of the archive generated by Skopeo. Since the archive contains the name and the tag of the image, Skopeo arguments used to fetch the image have to be identical to those used by the dockerTools.pullImage function.

For instance, the sha of the following image

pkgs.dockerTools.pullImage{
  imageName = "lnl7/nix";
  finalImageTag = "2.0";
  imageDigest = "sha256:632268d5fd9ca87169c65353db99be8b4e2eb41833b626e09688f484222e860f";
  sha256 = "1x00ks05cz89k3wc460i03iyyjr7wlr28krk7znavfy2qx5a0hfd";
};

can be manually generated with the following shell commands

skopeo copy docker://lnl7/nix@sha256:632268d5fd9ca87169c65353db99be8b4e2eb41833b626e09688f484222e860f docker-archive:///tmp/image.tgz:lnl7/nix:2.0
nix-hash --base32 --flat --type sha256 /tmp/image.tgz
1x00ks05cz89k3wc460i03iyyjr7wlr28krk7znavfy2qx5a0hfd

Docker Compose with Nix

Arion is created for running Nix-based projects in Docker Compose. It uses the NixOS module system for configuration, it can bypass docker build and lets you use dockerTools or use the store directly in the containers. The images/containers can be typical dockerTools style images or full NixOS configs.

To use Arion, you first need to add its module to you NixOS configuration:

modules = [ arion.nixosModules.arion ];

After that you can access its options under

virtualisation.arion = {}

A config for a simple container could look like this:

virtualisation.arion = {
  backend = "docker";
  projects = {
    "db" = settings.services."db".service = {
      image = "";
      restart = "unless-stopped";
      environment = { POSTGRESS_PASSWORD = "password"; };
    };
  };
};

Using Nix in containers

While dockerTools allows to build lightweight containers, it requires nix to be installed on the host system. An alternative are docker images with nix preinstalled:

See also

Workgroup:Container

Alternatively you can use Podman.