Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

add /var/lib/plausible (#4275)

ruslandogab73bdf932fdc
diff --git a/Dockerfile b/Dockerfile
index a66dd8e92e..602fcfdbec 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -71,9 +71,15 @@ RUN apk add --no-cache openssl ncurses libstdc++ libgcc ca-certificates
COPY --from=buildcontainer --chmod=a+rX /app/_build/${MIX_ENV}/rel/plausible /app
COPY --chmod=755 ./rel/docker-entrypoint.sh /entrypoint.sh
+# we need to allow "others" access to app folder, because
+# docker container can be started with arbitrary uid
+RUN mkdir -p /var/lib/plausible && chmod ugo+rw -R /var/lib/plausible
+
USER 999
WORKDIR /app
ENV LISTEN_IP=0.0.0.0
ENTRYPOINT ["/entrypoint.sh"]
EXPOSE 8000
+ENV DEFAULT_DATA_DIR=/var/lib/plausible
+VOLUME /var/lib/plausible
CMD ["run"]
diff --git a/config/runtime.exs b/config/runtime.exs
index b0572c3090..ff905ca0b8 100644
--- a/config/runtime.exs
+++ b/config/runtime.exs
@@ -232,7 +232,8 @@ maxmind_edition = get_var_from_path_or_env(config_dir, "MAXMIND_EDITION", "GeoLi
data_dir = get_var_from_path_or_env(config_dir, "DATA_DIR")
persistent_cache_dir = get_var_from_path_or_env(config_dir, "PERSISTENT_CACHE_DIR")
-data_dir = data_dir || persistent_cache_dir
+# DEFAULT_DATA_DIR comes from the container image, please see our Dockerfile
+data_dir = data_dir || persistent_cache_dir || System.get_env("DEFAULT_DATA_DIR")
persistent_cache_dir = persistent_cache_dir || data_dir
enable_email_verification =
diff --git a/lib/plausible/imported/csv_importer.ex b/lib/plausible/imported/csv_importer.ex
index 3f7f8100a3..ff763e304b 100644
--- a/lib/plausible/imported/csv_importer.ex
+++ b/lib/plausible/imported/csv_importer.ex
@@ -323,7 +323,9 @@ defmodule Plausible.Imported.CSVImporter do
@doc """
Returns local directory for CSV imports storage.
- Builds upon `$DATA_DIR` or `$PERSISTENT_CACHE_DIR` (if set) and falls back to /tmp
+ Builds upon `$DATA_DIR`, `$PERSISTENT_CACHE_DIR` or `$DEFAULT_DATA_DIR` (if set) and falls back to /tmp.
+
+ `$DEFAULT_DATA_DIR` is set to `/var/lib/plausible` in container images.
Examples:
diff --git a/test/plausible/config_test.exs b/test/plausible/config_test.exs
index 1e0a0e11d8..1c163638b3 100644
--- a/test/plausible/config_test.exs
+++ b/test/plausible/config_test.exs
@@ -295,14 +295,31 @@ defmodule Plausible.ConfigTest do
end
describe "storage" do
- test "with only DATA_DIR set" do
- env = [
- {"MAXMIND_LICENSE_KEY", "abc"},
- {"PERSISTENT_CACHE_DIR", nil},
- {"DATA_DIR", "/data"}
+ setup do
+ defaults = [
+ # comes from our Dockerfile
+ {"DEFAULT_DATA_DIR", "/var/lib/plausible"},
+ # needed to exercise Plausible.Geo :cache_dir
+ {"MAXMIND_LICENSE_KEY", "abc"}
]
- config = runtime_config(env)
+ env = fn env -> env ++ defaults end
+ {:ok, env: env}
+ end
+
+ test "defaults", %{env: env} do
+ config = runtime_config(env.([{"PERSISTENT_CACHE_DIR", nil}, {"DATA_DIR", nil}]))
+
+ # exports/imports
+ assert get_in(config, [:plausible, :data_dir]) == "/var/lib/plausible"
+ # locus (mmdb cache)
+ assert get_in(config, [:plausible, Plausible.Geo, :cache_dir]) == "/var/lib/plausible"
+ # tzdata (timezones cache)
+ assert get_in(config, [:tzdata, :data_dir]) == "/var/lib/plausible/tzdata_data"
+ end
+
+ test "with only DATA_DIR set", %{env: env} do
+ config = runtime_config(env.([{"PERSISTENT_CACHE_DIR", nil}, {"DATA_DIR", "/data"}]))
# exports/imports
assert get_in(config, [:plausible, :data_dir]) == "/data"
@@ -312,14 +329,8 @@ defmodule Plausible.ConfigTest do
assert get_in(config, [:tzdata, :data_dir]) == "/data/tzdata_data"
end
- test "with only PERSISTENT_CACHE_DIR set" do
- env = [
- {"MAXMIND_LICENSE_KEY", "abc"},
- {"PERSISTENT_CACHE_DIR", "/cache"},
- {"DATA_DIR", nil}
- ]
-
- config = runtime_config(env)
+ test "with only PERSISTENT_CACHE_DIR set", %{env: env} do
+ config = runtime_config(env.([{"PERSISTENT_CACHE_DIR", "/cache"}, {"DATA_DIR", nil}]))
# exports/imports
assert get_in(config, [:plausible, :data_dir]) == "/cache"
@@ -329,14 +340,8 @@ defmodule Plausible.ConfigTest do
assert get_in(config, [:tzdata, :data_dir]) == "/cache/tzdata_data"
end
- test "with both DATA_DIR and PERSISTENT_CACHE_DIR set" do
- env = [
- {"MAXMIND_LICENSE_KEY", "abc"},
- {"PERSISTENT_CACHE_DIR", "/cache"},
- {"DATA_DIR", "/data"}
- ]
-
- config = runtime_config(env)
+ test "with both DATA_DIR and PERSISTENT_CACHE_DIR set", %{env: env} do
+ config = runtime_config(env.([{"PERSISTENT_CACHE_DIR", "/cache"}, {"DATA_DIR", "/data"}]))
# exports/imports
assert get_in(config, [:plausible, :data_dir]) == "/data"