3

Is docker tmpfs working on wsl2.
If I run this in WSL2:

docker run -it --rm -e POSTGRES_PASSWORD=secret --tmpfs /var/lib/postgresql/data postgres:13-alpine sh

The whole container will run in the RAM?

Michael
  • 896
  • 10
  • 28

1 Answers1

3

[EDIT] As @Nik found, tmpfs in WSL is currently mapped to filesystem. At command line level it works as it is mapped in RAM, but it is actually mapped to filesystem. So, take care of this caveat until it is implemented as one would assume.

According to your first question: "Is docker tmpfs working on wsl2?" it seems the answer is yes. In fact, try to run a container like that:

$ docker run -it --name tmptest --mount type=tmpfs,destination=/mytmp busybox 

If you then inspect the container, you can see that /mytmp is mounted correctly as a tmpfs:

"Mounts": [
    {
        "Type": "tmpfs",
        "Source": "",
        "Destination": "/mytmp",
        "Mode": "",
        "RW": true,
        "Propagation": ""
    }
]

Some notes about your second question "The whole container will run in the RAM?":

  1. It's just the content of the folder /var/lib/postgresql/data that is stored in RAM, not the "whole container" whatever you think that means.

  2. It seems to me you're not running the db but a shell instead. So, unless you start the db from the shell I guess you would have no particular advantages in having /var/lib/postgresql/data in RAM.

  3. Technically speaking any program has to be loaded in RAM to work, or at least the portion that is currently executed.

danidemi
  • 4,404
  • 4
  • 34
  • 40
  • 1
    Note that it does work, but under WSL2, seems it's not RAM backed in fact. https://www.reddit.com/r/bashonubuntuonwindows/comments/69zez8/does_tmpfs_really_work/ – Nik Feb 13 '21 at 10:09
  • Good catch @Nik, I wasn't aware of it. However, the post you referred is three years old. Do you know if it still applies? – danidemi Feb 13 '21 at 21:41
  • 2
    It does yes - I discovered this yesterday when trying to optimise something in WSL. – Nik Feb 14 '21 at 22:22
  • @Nik that's sad. I develop static websites with 11ty on Windows using WSL2 and I use `tmpfs` for the output folder in dev/watch mode to spare my SSD from heavy writes. My config is working, but - based on your comments - using `tmpfs` for this purpose is useless... Do you know any idea/alternative on how to make a REAL ramdisk inside WSL2? – szegheo Mar 22 '21 at 12:40
  • I don't think it will be easy, but you could try looking at an alternative tmpfs implementation. Or for your scenario, maybe even a network filesystem could work for you (at cost of performance) – Nik Mar 22 '21 at 17:13
  • Testing this on WSL2 on Windows 11 it appears that tmpfs is indeed backed by RAM or is at least orders of magnitude faster than a non-tmpfs mount. I tested with: `docker run --rm --tmpfs /tmp:rw,noexec,nosuid,size=1G debian:buster-slim dd if=/dev/zero of=/tmp/test.img bs=512 count=1000 oflag=dsync` – ColinM Oct 12 '22 at 15:57