To prevent an Apptainer (formerly Singularity) container from accessing host ports, you need to restrict its network access. By default, Apptainer containers inherit the host's network namespace, which allows them to access host ports.
--network noneThis completely disables network access for the container, including host ports:
bashapptainer run --writable-tmpfs --fakeroot --network none docker://debian
If you still need some network access (e.g., for internal communication), you can create a private network namespace:
bashapptainer run --writable-tmpfs --fakeroot --network=private docker://debian
--no-ipv4 or --no-ipv6 (Partial Restriction)If you only want to block IPv4 or IPv6:
bashapptainer run --writable-tmpfs --fakeroot --no-ipv4 docker://debian
If you need fine-grained control, you can use iptables or nftables on the host to block the container's access to specific ports:
bashsudo iptables -A OUTPUT -p tcp --dport <PORT> -j DROP
<PORT> for all processes, including the container.--network none.--network=private and configure internal networking.Would you like help setting up a specific firewall rule or testing these options?