Shared Memory in Docker

Table Of Contents

Introduction

Recently, I was experimenting with system testing on a new stack. While working with Selenium Remote Driver to create headless web browsers (more on that later) to run against my app, I came across a Docker option I had not known. This Docker feature allowed for easily accessible and fast file reading and writing between the host and the container. It was being used as a volume, but I learned it was much more. When I looked it up, it was referred to as a Shared Memory Device.

Shared Memory Device

Turns out that this is an old Linux staple. The shared memory device, located in the /dev/shm/ directory in the file system, utilizes temporary storage using RAM rather than disk storage. Using a shared volume available on the RAM and not the disk makes for a much faster read/write speeds. These devices also allows for inter-process communication.

Use in Docker

Docker allows for the use of this device. When conducting my testing, I used the volume mapping feature to easily map local directories with directories inside the containers we’re working on. As seen below.

docker run --rm -it -v /dev/shm/:/dev/shm/ --name ubuntu ubuntu

This mapped the local shared memory device to the Docker device. Docker, by default, allows for up to 64 MB of storage space using the shared memory device. You can see the default allocation when searching for the shared storage device within the container. As seen below.

$ docker inspect ubuntu | grep -i shm
            "ShmSize": 67108864,

I was saving screenshots just to ensure that I was looking at the thing I was hoping to look at. Although I was only doing some foundational work, I could find myself in a pickle with only 64 MB of space available if this work ends up scaling. Should we need to, Docker lets us change the size of this device using the –shm-size option. As seen below.

docker run --rm -it -v /dev/shm/:/dev/shm --shm-size=2gb --name ubuntu ubuntu

Above we add in the –shm-size option and we can specify all the space we need! Let’s look one more time to confirm that it works.

$ docker inspect ubuntu | grep -i shm
            "ShmSize": 2147483648,

Shazam! We have more storage in our shared memory device to play around with.

Conclusion

There is more to come about my adventures in Dockerizing system testing, but for now, I was able to learn a new thing about Linux and Docker. I hope it helps someone who came across it without knowing as I did.

-George