145

Currently When I create new docker container the size of the shared memory directory is limited to 64MB. But, I need to increase this size since my application depend on this shared memory. Is there any way to increase the size of /dev/shm in docker container? I heard that the 64MB is hard coded in the docker code, How to install docker from source and change the value of the /dev/shm?

user2521791
  • 1,621
  • 2
  • 14
  • 13
  • 1
    I had to struggle to find this. However, it's exactly my problem. Adding shm_open and mmap here in hopes Google catches this SO post and makes other's lives easier. That's how I backed into this issue, not knowing anything about mapping the files to /dev/shm. – Greg Vogel Jun 20 '19 at 18:40

5 Answers5

160

If you're using docker-compose, you can set the your_service.shm_size value if you want your container to use that /dev/shm size when running or your_service.build.shm_size when building.

Example:

version: '3.5'
services:
  your_service:
    build:
      context: .
      shm_size: '2gb' <-- this will set the size when BUILDING
    shm_size: '2gb' <-- when RUNNING 

Link to source.

Diego M.F.
  • 1,757
  • 1
  • 10
  • 4
  • 8
    this answer shows the differences of using shm_size in build case and service case. I wonder why the answer was not accepted! – Tuhin Aug 31 '21 at 14:11
  • 2
    Because there is no mention of docker-compose in the question. – mianos May 16 '22 at 07:58
  • Putting `shm_size` in `your_service.shm_size` solved my problem. While others keep saying put it in `build` – Jay Sep 25 '22 at 14:07
109

You can modify shm size by passing the optional parameter --shm-size to docker run command. The default is 64MB.

eg:

docker run -it --shm-size=256m oracle11g /bin/bash
Alejandro Galera
  • 3,445
  • 3
  • 24
  • 42
Lijo Jacob
  • 1,241
  • 1
  • 10
  • 9
  • 2
    This is not working on debian 8 with docker 1.11.0. Not working as build params either! – Dimitri Kopriwa Jun 28 '16 at 10:58
  • 1
    Does increasing the shm size have any negative impact? (besides increased resource usage) – Kim Kern Oct 20 '20 at 09:06
  • 1
    This worked for me, but I set it to 2gb: --shm-size=2gb – Brian Wright Feb 04 '21 at 19:01
  • 2
    This solution is helpful (works also with `=` sign replaced by space). However, on Docker Desktop for Windows, for `docker build` (rather than `docker run`) it stopped working at some moment (perhaps after upgrade but I can't prove it). I realized it was caused by activating Docker Buildkit. This thread https://github.com/docker/buildx/issues/418 shows it does not support `--shm-size` option. Solution was to permanently set system variable `DOCKER_BUILDKIT=0` and everything returned to normal. – Tomáš Záluský Mar 17 '21 at 12:39
  • This `docker run --shm-size` argument is also recommended by Ray (`ray.init()`, which needs it to run out-of-core and it also recommends _"[..] to set this to more than 30% of available RAM."_ – mirekphd Aug 21 '22 at 09:50
17

If you use docker-compose to set up your docker environment, it is also possible to set the shared memory in the docker-compose.yml configuration file:

build:
  context: .
  shm_size: '2gb'

More info in the compose-file docs:

TmTron
  • 17,012
  • 10
  • 94
  • 142
11

If anybody is using an older docker version prior 1.10.0 and cannot upgrade for some reason, there is a workaround I used to set shm-size which works fine for me (you need sudo-rights to create the mount on the host):

sudo mkdir /mnt/dockershm
sudo mount -t tmpfs -o size=1G tmpfs /mnt/dockershm
docker run -d -v /mnt/dockershm:/dev/shm dockerimagetorun:latest
Storm
  • 111
  • 2
  • 5
3

Sometimes it may more appropriate to edit the global config for docker daemon, rather than set the value in docker-compose.yml or at the cli.

Here is the documentation for Daemon configuration file

Pick one method of editing the config:

  1. On Docker Desktop you can go to Settings->[Docker Engine].
  2. On linux you may edit the default file at /etc/docker/daemon.json
  3. On windows %programdata%\docker\config\daemon.json

Edit the config file to change the shm default size.

Example:

{
   ...
   "default-shm-size" : "128M"
   ...
}

After the change, restart docker may be required to relaod the config.

Davey
  • 2,355
  • 1
  • 17
  • 18
Nahuel Greco
  • 1,080
  • 14
  • 12
  • This is the only way to change the config when you don't have access to the docker-compose.yml file or the docker command in the cli. For example using the supabase cli that: in the background generates and starts a group of docker-compose services. – Davey Mar 17 '23 at 09:54
  • this is also useful for local mode of sagemaker jobs – Thesane May 20 '23 at 05:43