I have been doing some testing in this regards, it looks like the /var/run
directory is special in docker.
Here is some sample config and output:
ubuntu:
image: ubuntu
command: "bash -c 'mount'"
tmpfs:
- /var/run
- /var/cache
Running docker-compose up ubuntu
shows what is mounted. Can see /var/cache
is mounted but /var/run
isn’t.
...
ubuntu_1 | tmpfs on /var/cache type tmpfs (rw,nosuid,nodev,noexec,relatime)
...
If you use docker-compose run ubuntu bash
you can see it’s also mounted there but not /var/run
.
The reason is that /var/run
is normally a symlink to /run
and hence you creating /var/run/mysql
as a tmpfs doesn’t work.
It will work if you change it to /run/mysql
, but /run
is normally mounted as tmpfs anyway so you might as well just make /run
a tmpfs. Like so:
ubuntu:
image: ubuntu
command: "bash -c 'mount'"
tmpfs:
- /run
- /var/cache
Note: I’d like to amend my answer and show the way to do it using volumes
:
services:
ubuntu:
image: ubuntu
command: "bash -c 'mount'"
volumes:
- cache_vol:/var/cache
- run_vol:/run
volumes:
run_vol:
driver_opts:
type: tmpfs
device: tmpfs
cache_vol:
driver_opts:
type: tmpfs
device: tmpfs
This also allows you to share the tmpfs
mounts if needed.