Skip to content

Commit 1f9bee8

Browse files
authored
docker: enforce volume use for /var/lib/crowdsec/data/ (#3757)
1 parent dd32817 commit 1f9bee8

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

.github/workflows/docker-tests.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ on:
66
- master
77
- releases/**
88
paths-ignore:
9-
- 'README.md'
9+
- "README.md"
1010
pull_request:
1111
branches:
1212
- master
1313
- releases/**
1414
paths-ignore:
15-
- 'README.md'
15+
- "README.md"
16+
env:
17+
CROWDSEC_BYPASS_DB_VOLUME_CHECK: "TRUE" # to avoid the db volume check in the docker_start.sh script
1618

1719
jobs:
1820
test_flavor:
@@ -26,7 +28,6 @@ jobs:
2628
runs-on: ubuntu-latest
2729
timeout-minutes: 20
2830
steps:
29-
3031
- name: Check out the repo
3132
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
3233
with:

docker/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ $ docker build -f Dockerfile.debian --build-arg=BUILD_ENV=slim .
6262

6363
## Required configuration
6464

65+
### Volumes
66+
67+
Since CrowdSec 1.7.0, `/var/lib/crowdsec/data/` is required to be mounted in a volume.
68+
If this directory is not mounted, the container will refuse to start.
69+
6570
### Journalctl (only for debian image)
6671

6772
To use journalctl as a log stream, with or without the `DSN` environment variable, you need to mount the journal log from the host to the container itself.
@@ -350,6 +355,7 @@ config.yaml) each time the container is run.
350355
| __Developer options__ | | |
351356
| `CI_TESTING` | false | Used during functional tests |
352357
| `DEBUG` | false | Trace the entrypoint |
358+
| `CROWDSEC_BYPASS_DB_VOLUME_CHECK` | false | Bypass volume check for `/var/lib/crowdsec/data/` |
353359

354360
## File Locations
355361

docker/docker_start.sh

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ difference() {
203203

204204
#-----------------------------------#
205205

206+
# Tell crowdsec we are running in docker
207+
# The user agent will be updated so we can better detect broken installations running in docker
208+
export CROWDSEC_CONTAINER_ENV="docker"
209+
206210
if [ -n "$CERT_FILE" ] || [ -n "$KEY_FILE" ] ; then
207211
printf '%b' '\033[0;33m'
208212
echo "Warning: the variables CERT_FILE and KEY_FILE have been deprecated." >&2
@@ -246,6 +250,21 @@ elif [ -n "$USE_WAL" ] && isfalse "$USE_WAL"; then
246250
conf_set '.db_config.use_wal = false'
247251
fi
248252

253+
# Bail out if:
254+
# - `/var/lib/crowdsec/data` is not a volume
255+
# - CROWDSEC_BYPASS_DB_VOLUME_CHECK is not set
256+
# This check is performed regardless of the database type and if we are a LAPI or not:
257+
# - This directory is also used to store datafiles used by the LP, and some of them are really big and costly to download (MMDB files)
258+
# Do *not* implement this check in the k8s docker_start.sh
259+
if ! is_mounted "/var/lib/crowdsec/data" && [ -z "$CROWDSEC_BYPASS_DB_VOLUME_CHECK" ]; then
260+
echo "No volume mounted for /var/lib/crowdsec/data"
261+
echo "This directory is used to store the crowdsec local database (if using sqlite) and datafiles used by the parsers and scenarios."
262+
echo "It is mandatory to mount a volume to this directory to persist the database and any datafiles downloaded from the hub."
263+
echo "If you are doing a log replay or using a remote database (mysql, postgresql) on a LAPI-only container, you can set the environment variable CROWDSEC_BYPASS_DB_VOLUME_CHECK to skip this check."
264+
echo "Exiting..."
265+
exit 0 # No error to avoid a restart loop
266+
fi
267+
249268
lapi_credentials_path=$(conf_get '.api.client.credentials_path')
250269

251270
if isfalse "$DISABLE_LOCAL_API"; then

docker/test/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ readme = "README.md"
66
requires-python = ">=3.12"
77
dependencies = [
88
"pytest>=8.3.4",
9-
"pytest-cs>=0.7.22",
9+
"pytest-cs>=0.7.24",
1010
"pytest-dotenv>=0.5.2",
1111
"pytest-xdist>=3.6.1",
1212
]

docker/test/uv.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)