Enable File Virus Scanning with ClamAV
This guide applies to single-node HAP deployments with Docker Compose. HAP connects to ClamAV through the file storage service built into the sc container.
Prerequisites
- Install Docker and Docker Compose on the scan server and prepare a persistent directory. This example uses
/data/clamav/dbfor the signature databases. - The SC image must be
3.3.0or later, and the built-in file storage service must run in v2 mode. - Allow only the HAP
sccontainer to reach TCP port3310on the scan server. The ClamAV TCP interface has no built-in authentication. Keep the port off the public internet. ClamAV Scanning Documentation - Verify that the scan server can pull the ClamAV image and that FreshClam can reach the signature update service directly or through a proxy.
Deploy ClamAV
Create the directories and pull the image on the scan server.
sudo mkdir -p /usr/local/clamav/config /data/clamav/db
sudo docker pull clamav/clamav-debian:1.5.4
Create /usr/local/clamav/docker-compose.yaml.
services:
clamav:
container_name: clamav
image: clamav/clamav-debian:1.5.4
restart: always
environment:
FRESHCLAM_CHECKS: '12'
ports:
- '3310:3310'
volumes:
- /data/clamav/db:/var/lib/clamav
- ./config/clamd.conf:/etc/clamav/clamd.conf
- ./config/freshclam.conf:/etc/clamav/freshclam.conf
Create /usr/local/clamav/config/clamd.conf.
Foreground yes
User clamav
DatabaseDirectory /var/lib/clamav
TemporaryDirectory /tmp
LocalSocket /tmp/clamd.sock
LocalSocketMode 600
TCPSocket 3310
TCPAddr 0.0.0.0
StreamMaxLength 100M
MaxFileSize 100M
MaxScanSize 400M
MaxRecursion 16
MaxFiles 10000
PCREMaxFileSize 100M
MaxScanTime 120000
MaxThreads 2
MaxQueue 8
CommandReadTimeout 5
ReadTimeout 120
SendBufTimeout 500
AlertEncrypted yes
AlertExceedsMax yes
FailIfCvdOlderThan 7
SelfCheck 60
ConcurrentDatabaseReload yes
EnableShutdownCommand no
EnableReloadCommand no
EnableStatsCommand no
LogClean no
LogVerbose no
ExtendedDetectionInfo no
Create /usr/local/clamav/config/freshclam.conf. If a proxy is required, uncomment the last two lines and enter the proxy host and port.
Foreground yes
DatabaseDirectory /var/lib/clamav
DatabaseOwner clamav
DatabaseMirror database.clamav.net
Checks 12
TestDatabases yes
ConnectTimeout 30
ReceiveTimeout 120
# HTTPProxyServer proxy.example.com
# HTTPProxyPort 3128
Set the permissions and validate the Compose configuration.
sudo chmod 755 /usr/local/clamav /usr/local/clamav/config
sudo chmod 644 /usr/local/clamav/config/clamd.conf /usr/local/clamav/config/freshclam.conf
sudo docker compose -f /usr/local/clamav/docker-compose.yaml config --quiet
On initial deployment, if the signature database directory on the host is empty and ClamAV is stopped, copy the databases from the image to the host and run FreshClam once.
sudo docker run --rm --network none --entrypoint cp \
-v /data/clamav/db:/seed clamav/clamav-debian:1.5.4 -a /var/lib/clamav/. /seed/
sudo docker compose -f /usr/local/clamav/docker-compose.yaml run --rm --no-deps \
clamav freshclam --stdout --config-file=/etc/clamav/freshclam.conf
Start ClamAV, check its status and logs, and wait for the container to show healthy.
sudo docker compose -f /usr/local/clamav/docker-compose.yaml up -d
sudo docker compose -f /usr/local/clamav/docker-compose.yaml ps
sudo docker logs --tail=100 clamav
Configure HAP
Edit /data/mingdao/script/docker-compose.yaml and add the following variables to the existing services.app.environment section. In the default single-node template, sc inherits these environment variables.
Replace clamav.example.com:3310 with the scan server address reachable from the sc container.
services:
app:
environment: &app-environment
ENV_FILE_CLAMAV_ENABLE: 'true'
ENV_FILE_CLAMAV_ADDR: 'clamav.example.com:3310'
ENV_FILE_CLAMAV_SCAN_EXTENSIONS: '*'
ENV_FILE_CLAMAV_MAX_FILE_BYTES: '67108864'
ENV_FILE_CLAMAV_TIMEOUT_SECONDS: '120'
ENV_FILE_CLAMAV_MAX_CONCURRENCY: '1'
ENV_FILE_CLAMAV_MAX_QUEUE: '2'
ENV_FILE_CLAMAV_QUEUE_TIMEOUT_SECONDS: '5'
* matches all file extensions. This example sets a scan file size limit of 64 MiB, a scan timeout of 120 seconds, one concurrent scan, a queue of two requests, and a queue timeout of five seconds.
Run the following command from the HAP deployment manager directory to apply the configuration.
bash ./service.sh restartall
Verify Scanning
Once HAP is ready, upload and open a plain-text or Word file. Then upload an EICAR test file and verify that scanning rejects it.
Maintain Signature Databases
Run the following commands on the scan server.
Check the Loaded Signature Database
Use nc to query the published port on the scan server.
sudo docker compose -f /usr/local/clamav/docker-compose.yaml ps
printf 'nVERSION\n' | nc -w 3 127.0.0.1 3310
VERSION returns the engine version, the loaded signature database version, and its build date. ClamD Protocol Documentation
To check when the signature database files on the host were modified, run the following command. The timestamps show the files on disk. Use VERSION to verify what ClamAV has loaded.
sudo find /data/clamav/db -maxdepth 1 -type f \( -name '*.cld' -o -name '*.cvd' \) \
-exec stat -c '%y %n' {} +
Manually Check for Signature Updates
Send SIGUSR1 to wake the running FreshClam process. Before each check, retrieve the process ID on the host and verify that the command returns exactly one positive PID.
FRESHCLAM_PID=$(sudo docker top clamav -eo pid,comm | awk '$2 == "freshclam" {print $1}')
printf '%s\n' "$FRESHCLAM_PID"
sudo kill -USR1 "$FRESHCLAM_PID"
sudo docker compose -f /usr/local/clamav/docker-compose.yaml logs -f --since=1m --timestamps clamav
In the logs, updated indicates that a database update was written to disk. up-to-date indicates that the database is current. In this example, SelfCheck 60 tells clamd to check for and load updated databases periodically. When the logs show that the reload succeeded, run VERSION to verify the loaded version. Press Ctrl+C to exit the log view.
Maintain the ClamAV Service
Before recreating or stopping the scan service, pause new uploads and fetches and wait for active tasks to finish. After editing the configuration, validate it and recreate the container on the scan server.
sudo docker compose -f /usr/local/clamav/docker-compose.yaml config --quiet
sudo docker compose -f /usr/local/clamav/docker-compose.yaml up -d --force-recreate clamav
Use stop clamav to stop the service temporarily and start clamav to resume it. If you use down to remove the container and Compose network, the signature database directory remains on the host. Use up -d to recreate the container. After recreating or restarting it, check the container status, logs, and loaded database version. Verify uploads before resuming operations.