The container seems to running as root by default. Is it possible to run it with a non-root user? I tried it, and it worked quite well for the most part, but I get e.g. this error on every startup:
crowdsec | time="2024-09-18T21:08:17+02:00" level=error msg="unable to open GeoLite2-City.mmdb : open /var/lib/crowdsec/data/GeoLite2-City.mmdb: permission denied"
crowdsec | time="2024-09-18T21:08:17+02:00" level=warning msg="unable to initialize GeoIP: open /var/lib/crowdsec/data/GeoLite2-City.mmdb: permission denied"
I think the reason is that this directory and its files are only accessible by root user and root group:
I can change manually the permissions in the running container which seems to work, and I guess, I could also patch the Dockerfile myself to adapt the permissions.
I am surprised I could not find any information online about running crowdsec as non-root user but maybe I had bad luck when searching. Is there an official way to do this? Or is it generally not advised to do so?
this works in general but you need to care about all the directories and create the manually as the limited user can not create it himself (in my case)
For anyone having this issue and stumbling open it, I created a custom Dockerfile to allow to run CrowdSec as a non-root user in a container:
FROM crowdsecurity/crowdsec:v[[$VERSION]]
RUN apk add --no-cache su-exec
RUN cat <<'EOF' > /entrypoint.sh
#!/bin/sh
set -eu
PUID=${PUID:-1000}
PGID=${PGID:-1000}
chown -R ${PUID}:${PGID} /etc/crowdsec /var/lib/crowdsec /staging /usr/local/lib/crowdsec/plugins
exec su-exec ${PUID}:${PGID} /bin/bash "/docker_start.sh" "$@"
EOF
RUN chmod +x /entrypoint.sh
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
CMD wget -qO- http://localhost:6060/metrics || exit 1
ENTRYPOINT ["/entrypoint.sh"]
You just have to add in your compose file or your run command the PUID and PGID environment variables to set your user (don’t use the USER directive). I don’t think it does a lot security wise as the container still has to start as root to deal with file ownership, but for people like me who do not want to have files created as root in their bind mounts, this solves this problem.
It’s working well on my side, if you see any improvement to be made don’t hesitate.