Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Do docker containers share RAM for files memory mapped from the same layer but a different image?

I'm not 100% certain about whether this is a U&L question or a SO question. On balance I'm posting it on U&L as it's OS related.

Background

As far as I know, Linux will load shared libraries (.so files) by memory mapping them as copy-on-write. One advantage of this is that multiple processes which share the same large library will all share the same physical RAM for much of that library's content.

This doesn't necessarily happen with Docker because processes run in their own "container" based off an "image" and each image contain's it's own copy of shared libraries. This is deliberate. It allows programs to ship with their own dependencies (libraries) which may be substantially different to the libraries already installed on the system.

So a program running natively on a docker host will not share the same memory for libraries as a program running inside a docker container because the program in the docker container has mapped to different copies of the libraries.

Docker layers explained

Docker images are created in layers. Each layer adds to the lower one, sometimes overwriting existing files. Not every file is changed in every layer.

Docker allows you create new images by adding new layers to an older image. When this happens you end up with multiple images sharing the same layers. The images share identical copies of some of the same files.

Docker keeps the layers separately, at least before runtime. Eg: wen pulling an image from Docker Hub, Docker fetches images by fetching each image's constituent layers. It only fetching the layers it doesn't already have.

What I don't know

When creating or running a container Docker must assemble the layers into a single coherent file system. I don't know how it does this. It could:

Depending on what it does, files which originated from the same layer may be identical copies, or they may the exact same file on the file system.

This will ultimately affect what happens when the files are memory mapped by multiple processes.

What am I really trying to discover?

I want to know if running two containers from two different images will share the same RAM for a single shared library that originated in a single layer.

Answer*

Cancel
1
  • this question points out that the behaviour in this answer may not be reproducible any more, which came as a surprise to me because I swear I tested it before accepting. Docker documentation makes clearer the fact that, with overlayfs storage driver (default), files are hard linked from lower layers to upper layers ensuring they are exactly the same file on disk and in memory. (documented here) Commented Jul 22 at 5:50