Skip to content

Deploy CrowdSec with Docker

CrowdSec is an open source behavioral detection solution that analyzes logs and various events to identify malicious behavior.

Unlike a simple blocking system based on IP address lists, CrowdSec relies on detection scenarios to identify different types of attacks, then produces decisions that can be used by bouncers to apply remediation, for example by blocking an IP address.

In this part, we only see how to deploy CrowdSec to integrate it with NGX Ops.

For a complete CrowdSec installation with bouncer configuration and remediation setup, you will find several additional resources at the end of this page.

Deploy CrowdSec

In a folder dedicated to CrowdSec, for example /containers/crowdsec, create the following compose.yml file:

services:
  crowdsec:
    image: crowdsecurity/crowdsec:latest
    restart: always
    user: root
    environment:
      COLLECTIONS: "crowdsecurity/nginx crowdsecurity/sshd crowdsecurity/wordpress crowdsecurity/base-http-scenarios crowdsecurity/http-cve crowdsecurity/http-dos"
      GID: "${GID-1000}"
    volumes:
      - /var/log:/var/log
      - /containers/nginx/nginx/logs:/var/log/nginx
      - ./crowdsec/db:/var/lib/crowdsec/data/
      - ./crowdsec/config:/etc/crowdsec/
    networks:
      crowdsec_network:
        ipv4_address: 10.199.0.5

networks:
  crowdsec_network:
    ipam:
      driver: default
      config:
        - subnet: 10.199.0.0/24

Note: this example mounts the NGX Ops log directory directly in the CrowdSec container. Adapt the /containers/nginx/nginx/logs path to your installation.

Then start CrowdSec:

docker compose up -d

Check that the container started correctly:

docker compose ps

Configure Nginx log analysis

We now tell CrowdSec that it must analyze the Nginx logs.

In the directory:

./crowdsec/config/acquis.d/

create the file:

nginx.yaml

For example:

nano ./crowdsec/config/acquis.d/nginx.yaml

Add the following content:

filenames:
  - /var/log/nginx/*.log

labels:
  type: nginx

The /var/log/nginx/*.log path is the path as seen from the CrowdSec container.

The Docker mount defined earlier maps this path to the logs generated by NGX Ops on the host.

Then restart CrowdSec to apply the new configuration:

docker compose restart crowdsec

Check that it works

To check that CrowdSec is analyzing the logs, use the command:

docker compose exec crowdsec cscli metrics

This command notably lets you check that the log files are taken into account and that events are analyzed by CrowdSec.

If the Nginx logs are correctly detected, they appear in CrowdSec's metrics.

Integration with NGX Ops

At this stage, CrowdSec can analyze the logs generated by NGX Ops.

This configuration is the detection part.

It notably lets NGX Ops rely on CrowdSec to get information on IP addresses that have been the subject of decisions or detections.

To set up automatic blocking of IP addresses, you then need to configure a bouncer suited to your architecture.

CrowdSec and NGX Ops are complementary: NGX Ops provides administration and visibility around Nginx, while CrowdSec brings its behavioral detection engine and, with a bouncer, the ability to apply automatic remediation.

Additional resources

If you want to go further with setting up CrowdSec, here are several articles available on RDR-IT:

(These articles are in French.)