Is There a Way to Run Docker Inside a nix-shell?

Is it possible to start a nix-shell using docker & docker-compose to start a service? When I start a shell using nix-shell -p docker docker-compose cmake perl and run the stack I get the error: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?. Trying to start the daemon in the shell results in an error message suggesting that I look at running in rootless mode.

I have Googled this question, and I find a lot of answers about how to run a nix-shell in a docker container, but not the other way around. Usually, if something is so difficult to find an answer for, I question the wisdom of what I am trying to do.

For some context: I have my own EQEmu server running via The Akk Stack on a bare metal microserver on Ubuntu. For testing changes, I also have a test server running on an old laptop, but the laptop is old and now failing to boot. So, rather than find another piece of hardware to run it on, I considered just running a VM on my desktop system (NixOS). But then, since it’s a NixOS system, I thought maybe nix-shell would make more sense.

So, if I don’t want to install docker globally, is it realistic to run a docker-compose stack in a nix-shell? Or should I just install a VM instead?

I’m sure I’ve done this successfully using rootlesskit, but I do also remember having to hack the wrapper up a bit for my particular environment. I’ll see if I can dig that out tomorrow.


Hmm, so I’m running this on Ubuntu, not NixOS, but AFAICT you just need nix-shell -p rootlesskit docker followed by dockerd-rootless. (My local hack turned out just to be removing --disable-host-loopback which obviously I needed for some specific reason which I’ve now forgotten.)

Only thing I can think you might need to do is make sure /etc/uidmap and friends are set-up, if NixOS doesn’t do it automatically.


Perfect, thank you!

The only thing to add (which is possibly specific to NixOS) is that I had to export an extra environment variable. It seems that, by default, docker looks to /var/run/docker.sock for the daemon, but rootlessly it runs in /run/user/<userid>. If you don’t export that variable, then trying to interact with the docker client gives the message: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

So, within the shell this works fine:

export DOCKER_HOST=unix://$XDG_RUNTIME_DIR/docker.sock
docker run hello-world
1 Like