It is a single flag with no cost that I would put on nearly every container that does not need sudo inside. A process in the container can no longer climb to more privileges than it started with.
The docker run reference describes it like this: the option stops container processes from gaining new privileges, so commands that raise privileges such as su or sudo no longer work inside the container.
docker run --rm -it --security-opt no-new-privileges ubuntu bash
In Compose you write it under the service:
services:
app:
image: example/app:1.0
security_opt:
- no-new-privileges
The Compose reference says that for boolean options like this the value can be left out and the option counts as enabled, so no-new-privileges, no-new-privileges=true and no-new-privileges:true all mean the same.
Some images start as root on purpose and drop to a normal user through a setuid helper or an entrypoint that calls su-exec or gosu. After adding the flag, check that the container still starts and that its logs show no permission errors. If an image breaks, leave the flag off for that one.
It combines well with --cap-drop ALL. Add them one at a time, so you know which one caused a failure.
Further reading: docker run reference, Compose services: security_opt