chronograf can crash when using Docker bind mounts #781
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Assume a Docker "bind mount" is used to map Chronograf's persistent store. Examples:
a
docker run
command:these lines in a
docker compose
service definition:Prior to starting the container, Docker tries to ensure that the external path to the persistent store exists via the equivalent of:
The practical result is that any path component that didn't exist beforehand is created and owned by root.
Make two assumptions (typical "first launch" conditions):
./chronograf
did not exist so Docker has just created thechronograf
folder with root ownership; andIn the absence of passing
CHRONOGRAF_AS_ROOT
, the first-time user is then in the situation where:the persistent store is owned by root;
the container launches as root but downgrades its privileges to user
chronograf
(userID 999);the executable is then unable to write into its persistent store. It crashes with the error message:
Depending on how the container was launched, it then either halts or goes into a restart loop (eg if
restart: unless-stopped
).Currently, there are two solutions to this:
The user passes the
CHRONOGRAF_AS_ROOT
environment variable with the valuetrue
; orThe user manually adjusts ownership on the persistent store:
Option 1 defeats the purpose of running with reduced privileges. Option 2 isn't documented so it is an example of "hidden knowledge". The user has to:
docker logs -f chronograf
(the-f
being particularly important if the container is in a restart loop);chown
(or100:101
for the alpine container).It would be preferable if the container handled these situations correctly for itself, which is the main goal of this Pull Request.
This problem does not occur if a named volume mount is used rather than a bind mount. That is because of the "copy" step whereby Docker recursively copies the internal path to the external path before the Unix-bind-mount association is formed. The last path component of the volume mount (ie the
_data
folder) is then owned by userID 999. Even ifCHRONOGRAF_AS_ROOT
istrue
, root can still write into that folder.If the container is launched without an explicit volume mapping, a new anonymous volume mount is created each time the container is recreated, but otherwise behaves the same as a named volume mount. This is a side-effect of the Dockerfile declaration:
Although the default for Docker is to launch the container as root, it is also possible to use either the
-u
option (docker run
) oruser:
clause (docker compose
) to have Docker launch the container as some other user. In this situation, with the exception of userID 999, the container will lack the privileges to write to/var/lib/chronograf
so it will abort with the permission error mentioned above, and the user will also have to know which userID to employ to set up the persistent store.This Pull Request tries to deal with that possibility by writing a hint into the log. For example, if the container is launched as userID 1000 but doesn't have write permission for
/var/lib/chronograf
, the user would see: