Skip to content

Instantly share code, notes, and snippets.

@573
Last active January 29, 2024 21:25
Show Gist options
  • Save 573/6692d06a14f8844abbe40935d8eb8146 to your computer and use it in GitHub Desktop.
Save 573/6692d06a14f8844abbe40935d8eb8146 to your computer and use it in GitHub Desktop.
run services in docker in nixos in wsl(2)

Set up (pointers here) nixos first.

running the container like this docker run --name redis -dit -p 0.0.0.0:6379:6379 imagename (microsoft/WSL#5618 (comment)) let's you from the host windows do either:

Create [#file-redis-small.nix] like this (mod of https://yann.hodique.info/blog/using-nix-to-build-docker-images/, source: https://gist.github.com/sigma/9887c299da60955734f0fff6e2faeee0, this again modding http://lethalman.blogspot.com/2016/04/cheap-docker-images-with-nix_15.html, latter via https://nixos.wiki/wiki/Docker)

curl localhost:6379

or

NIX_PATH=nixpkgs=http://nixos.org/channels/nixpkgs-unstable/nixexprs.tar.xz nix-shell -p redis redis-cli -c

Works even with VPN active once the docker container is started at least.

{ pkgs ? import <nixpkgs> {} }:
with pkgs;
let
su_exec = stdenv.mkDerivation {
name = "su-exec-0.2";
src = fetchurl {
url = https://github.com/ncopa/su-exec/archive/v0.2.tar.gz;
sha256 = "09ayhm4w7ahvwk6wpjimvgv8lx89qx31znkywqmypkp6rpccnjpc";
};
preBuild = ''
makeFlagsArray=(CC="${musl.dev}/bin/musl-gcc -static"
CFLAGS="-I${musl.dev}/include"
LDFLAGS="-L${musl.dev}/lib")
'';
buildFlags = "su-exec-static";
installPhase = ''mkdir -p $out/bin && cp su-exec-static $out/bin/su-exec'';
};
in
dockerTools.buildImage {
name = "redis";
runAsRoot = ''
#!${stdenv.shell}
export PATH=/bin:/usr/bin:/sbin:/usr/sbin:$PATH
${dockerTools.shadowSetup}
groupadd -r redis
useradd -r -g redis -d /data -M redis
mkdir /data
chown redis:redis /data
${gnused}/bin/sed -i 's:/nix/.*bash.*::' etc/passwd
${gnused}/bin/sed -i 's:/nix/.*bash.*::' etc/passwd-
'';
config = {
Cmd = [ "${su_exec}/bin/su-exec" "redis" "${redis}/bin/redis-server" ];
ExposedPorts = {
"6379/tcp" = {};
};
WorkingDir = "/data";
Volumes = {
"/data" = {};
};
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment