How to increase the size of the /dev/shm in docker container

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?

156281 次浏览

您可以通过将可选参数 --shm-size传递给 docker run命令来修改 shm 大小。

例如:

docker run -it --shm-size=256m oracle11g /bin/bash

如果您使用 docker-compose 来设置您的 docker 环境,还可以在 docker-compose.yml配置文件中设置共享内存:

build:
context: .
shm_size: '2gb'

更多信息在撰写文件的文档中:

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 跑步 or your_service.build.shm_size when 建筑物.

例如:

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

链接到 source

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