Shared memory

and how to configure it

Containers with applications often require larger shared memory than the Docker’s default of 64MB.

Experience shows, that 256MB is usually enough in most cases.

You can increase the shared memory (SHM) size of individual containers or you can change the Docker’s default value.

Table of contents
  1. Shared memory
    1. Checking current SHM size
    2. Setting SHM using Docker CLI
    3. Setting SHM using Docker Compose
    4. Setting default SHM size on Linux
    5. Setting default SHM size in Docker Desktop

Checking current SHM size

You can check the current SHM size inside a running container by executing the following command:

df -h /dev/shm
Filesystem      Size  Used Avail Use% Mounted on
shm             256M     0  256M   0% /dev/shm

Setting SHM using Docker CLI

The SHM size is set by container creation.

For example, the following command fragment shows how to set the SHM size to 256MB:

docker run --shm-size=256m ...

Setting SHM using Docker Compose

The SHM size is set in the service configuration.

If the service builds a new image, then you can set the default SHM size for the image during its build time.

You can also set the SHM size for the run time of the service container.

For example, the following compose file fragment shows, how to set the SHM size to 256MB for the built image and to 512MB for the created container:

services:
  some_service:
    build:
      context: .
      shm_size: '256m'  ## image build time
      ...
    shm_size: '512m'    ## container run time
    ...

Setting default SHM size on Linux

If you are on Linux and you don’t have the Docker Desktop for Linux installed, then you can change the default SHM size by changing the value of the parameter default-shm-size in the file /etc/docker/daemon.json.

If the file does not exist yet, then create it first.

For example, changing the SHM size to 256MB:

{
  "default-shm-size": "256m"
}

After modifying the file you have to restart the Docker service:

sudo systemctl restart docker

Setting default SHM size in Docker Desktop

If you have Docker Desktop installed (on Windows or Linux), then you can change the default SHM size by changing the value of the parameter default-shm-size in the Docker Engine configuration.

For example, changing the SHM size to 256MB:

{
  "builder": {
    "gc": {
      "defaultKeepStorage": "20GB",
      "enabled": true
    }
  },
  "default-shm-size": "256M",
  "experimental": false
}

Previous Top Next