Jay Taylor's notes

back to listing index

I Just Learnt About /dev/shm • Son Dang

[web search]
Original source (sondnm.github.io)
Tags: linux docker docker-volumes dev-shm shared-memory
Clipped on: 2020-06-23

I Just Learnt About /dev/shm

Posted on 2018, Sep 08 2 mins read

This morning I went to a tech talk organized by Grokking Vietnam. The talk was about using Docker and Selenium in End-to-End testing, which was a pretty interesting topic. In the middle of the talk, the speaker introduced a docker-compose.yml file with a noticeable line:

# docker-compose.yml
version: '3'
  # ...
  chrome:
    # ...
    volumes:
      - '/dev/shm:dev/shm' # this line ^^
  #...

Here he was trying to run test cases with Selenium webdriver on Docker. The chrome container is for running Google Chrome container in headless mode. The speaker then asked the audience to explain why we need the /dev/shm volumes. Some people including me tried to answer. I actually thought the shm was meant for Selenium Hub Management.

Finally, no one was able to give a correct answer and as the speaker explained the /dev/shm was meant for shared memory. Sometimes when the application running inside a Docker container consumes too much memory space, it has to be configured to access the host’s shared memory.

After a dive into Google (Google is my best friend), here is what I discovered:

  • /dev/shm is an implementation of shared memory concept. What is a shared memory? Shared memory is memory that may be accessed by multiple programs. It is efficient for passing data between programs.
  • You can check the size of Linux’s shared memory by using this command:

    $ df /dev/shm
    
  • /dev/shm seems to be Linux specific. On OSX, if you want to create a shared memory, check this Stack Overflow answer

  • On Linux, to change the size of /dev/shm, run this:

    $ mount -o remount,size=2G /dev/shm
    

    This command literally means “Remount the /dev/shm with the size of 2GB”.

  • To run Docker with a custom shared memory size, use the --shm-size option. For example:

    $ docker run -it --shm-size=256m <CONTAINER_NAME> /bin/sh
    

    According to this docs (look for --shm-size), its default is 64MB.

These are pretty much what I found today. Joining tech talk plus doing a bit of research seems to be a pretty useful learning method.