How to install the docker in NixOS with simple steps?

NixOS

How to install the docker in NixOS with simple steps?

Install docker NixOS with easy steps.

--

NixOS is a free and open-source Linux distribution based on the Nix language. NixOS supports reproducible and declarative system-wide configuration. NixOS has an inbuilt package manager that helps install any package from nixpkgs.

Docker is a software platform that allows you to build, test, and deploy applications quickly using containers.

Installing docker in NixOS is a very easy process. Copy and paste follow code into your configuration.nix file, which I mention in this article.

  1. Incorrect way to install docker
  2. The correct way to install docker

Incorrect way to install docker

For newly developer-installed docker, look at this. It is wrong. Please don't use that. You can install docker successfully in your system with the following method, which works fine.

# configuration.nix

users.users.rajdeep = {
packages = with pkgs; [ docker ];
};

# home manager
home.packages = with pkgs; [ docker ]

But the problem is that you can face this type of error when you install other software that works with or on top of docker.

Error 1

ERRO[0000] daemon Docker Engine socket not found and containerDaemonSocket option was not set

Error 2

dockerd
INFO[2024-02-25T17:34:28.394168092+05:30] Starting up
dockerd needs to be started with root privileges. To run dockerd in rootless mode as an unprivileged user, see https://docs.docker.com/go/rootless/

Error 3

➜ sudo systemctl start docker
[sudo] password for rajdeep:
Failed to start docker.service: Unit docker.service not found.

The correct way to install docker

To install docker, you need to follow these steps.

  1. Enable docker with virtualisation.
  2. Add docker in the group.
  3. Rootless docker

Enable docker with virtualisation.

The first enables docker with virtualisation. It helps to docker uses a daemon that manages Linux containers with docker command line tool.

# configuration.nix

virtualisation.docker.enable = true;

Add docker in the group

Adding users to the docker group will provide access to the socket.

# configuration.nix

# spefic user
users.users.<myusername>.extraGroups = [ "docker" ];

# all users
users.extraGroups = [ "docker" ];

Rootless docker

Rootless mode allows the Docker daemon and containers to be run without root users' permission.

It provides an extra layer of security and saves from potential vulnerabilities in the daemon and the container runtime.

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

Your final configuration.nix looks like this for docker installation.

# configuration.nix

users.users.rajdeep = {
extraGroups = ["docker"];
};

# enable docker
virtualisation.docker.enable = true;

# use docker without Root access (Rootless docker)
virtualisation.docker.rootless = {
enable = true;
setSocketVariable = true;
};

Lastly, rebuild your system using the nixos-rebuild command.

nixos sudo nixos-rebuild switch

After your NixOS is rebuilt successfully, restart your machine.

Check the wiki page for more learn https://nixos.wiki/wiki/Docker

To learn more about NixOS and Linux stuff, follow the Linux publication on Medium and other updates. Follow me on Twitter (X) and Medium.

--

--

Rajdeep Singh
The Linux

JavaScript | TypeScript | Reactjs | Nextjs | Rust | Biotechnology | Bioinformatic | Frontend Developer | Author | https://linktr.ee/officialrajdeepsingh