A PyInstaller binary stops with libz.so.1 failed to map segment in Docker or on a server
error while loading shared libraries: libz.so.1: failed to map segment from shared object: Operation not permittedShort 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.
Fix it fast
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 noexec2Match what you see, then jump to its fix
What does the error look like?
error while loading shared libraries: libz.so.1: failed to map segment from shared object: Operation not permittedThe 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,OPTIONS2Fix
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/
./hello3Verify
Start the binary again in the same shell. It should run without the loader error.
./helloConfirmed by pyinstaller.org
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-container2Fix
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-image3Verify
Run the container again and start the program.
docker run --rm --tmpfs /tmp:rw,nosuid,size=65536k my-image ./helloConfirmed by docs.docker.com
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
Apache logs SIGWINCH and exits on its own
the httpd container stops although nothing crashed
SIGWINCH is Apache's graceful-stop signal.
2 causes, each with a fixServers
sam local fails: Running AWS SAM projects locally requires Docker
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
sam local start-api and sam local invoke run your function inside a Docker container, so SAM stops if it cannot talk to Docker.
3 causes, each with a fixServers
dnf fails to download repo metadata in a CentOS 8 Docker build
The same Dockerfile built fine before 2022
CentOS Linux 8 reached end of life on 31 December 2021 and its content left the mirrors.
2 causes, each with a fixServers
pip install confluent-kafka fails because librdkafka/rdkafka.h is missing
pip stops while building confluent-kafka from source
The error means pip is compiling confluent-kafka from source, and the compiler cannot find the librdkafka C headers.
3 causes, each with a fixServers
Cypress says the npm package is installed but the binary is missing
Cypress binary is missing after npm install in CI or in a Docker build
The npm package is only a launcher.
3 causes, each with a fixServers
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