Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

A PyInstaller binary stops with libz.so.1 failed to map segment in Docker or on a server

Errorerror while loading shared libraries: libz.so.1: failed to map segment from shared object: Operation not permitted

Short answer

A PyInstaller one-file program unpacks its libraries into a temp folder and loads them from there. If that folder sits on a filesystem mounted noexec, the loader refuses to map the library and prints this error. Point TMPDIR at a folder that allows execution, or remove noexec from the mount that holds the temp folder.

  • Applies to PyInstaller 6, Docker Engine 27, Docker Compose v2, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List the mounts that carry the noexec flag. If /tmp, or the folder your container uses as /tmp, is in the list, one of the two causes below applies.

    findmnt -o TARGET,FSTYPE,OPTIONS | grep noexec
  2. 2Match what you see, then jump to its fix

    1. 1The same binary runs fine from your own folder but fails once it unpacks into /tmp/tmp on the host is mounted noexecFix
    2. 2The error appears only inside the container, and your run command or Compose file mounts a tmpfs on /tmpA tmpfs mounted into the container carries noexecFix

What does the error look like?

  • error while loading shared libraries: libz.so.1: failed to map segment from shared object: Operation not permitted
  • The program built with --onefile runs on your laptop but not in the container or on the server

Cause 1 of 2

/tmp on the host is mounted noexec

1Check

Show the mount options for /tmp. The PyInstaller troubleshooting page names a noexec mount as a possible reason for this exact error.

findmnt -T /tmp -o TARGET,OPTIONS

2Fix

Tell the program to unpack somewhere else by exporting TMPDIR before you start it, using a folder on a filesystem that is not mounted noexec. /var/tmp is the example the PyInstaller docs use.

export TMPDIR=/var/tmp/
./hello

3Verify

Start the binary again in the same shell. It should run without the loader error.

./hello

Confirmed by pyinstaller.org

Did this fix it?

Cause 2 of 2

A tmpfs mounted into the container carries noexec

1Check

docker run accepts mount options for --tmpfs that are identical to mount -t tmpfs -o, and the docs example sets noexec. The noexec option means no binary on that filesystem may be executed directly, which includes mapping a library from it. Look for a tmpfs mount in your command or Compose file.

docker inspect --format '{{json .HostConfig.Tmpfs}}' my-container

2Fix

Remove noexec from the tmpfs options, or leave the tmpfs off /tmp and give the program a different TMPDIR that is a normal writable path in the container.

# before
docker run --tmpfs /tmp:rw,noexec,nosuid,size=65536k my-image
# after
docker run --tmpfs /tmp:rw,nosuid,size=65536k my-image

3Verify

Run the container again and start the program.

docker run --rm --tmpfs /tmp:rw,nosuid,size=65536k my-image ./hello

Confirmed by docs.docker.com

Did this fix it?

How do I stop it coming back?

  • Keep noexec off the folder your bundled apps unpack into, or set TMPDIR for the service that runs them.
  • When you harden a container with a read-only root and a tmpfs, test the real program once before you ship it.
  • Write the TMPDIR choice into the systemd unit or the Compose environment so it does not depend on one shell.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Servers

A dev container port does not open in the browser when Docker runs on a remote host

If your application only accepts calls from localhost, it will reject connections from published ports

Three things usually break it: the app listens only on localhost inside the container, so published ports are rejected; you open the container port instead of the mapped localhost port VS Code shows; or you changed appPort or a Compose ports list and did not rebuild.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.