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:
- Copy the files into one place
- Create hard links into one place
- Use an overlay file system
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.