Added warning if in docker and data dir isn't mounted

Had to remove the VOLUME definition in the dockerfile for this, otherwise the data dir would always be mounted by an anonymous volume if the user didn't mount it correctly, so the python script wouldn't be able to detect it.
This commit is contained in:
Donkie
2023-11-07 21:45:52 +01:00
parent d55dde5bfe
commit fef41a5628
3 changed files with 29 additions and 1 deletions

View File

@@ -66,6 +66,5 @@ ENV BUILD_DATE=${BUILD_DATE}
# Run command # Run command
EXPOSE 8000 EXPOSE 8000
VOLUME ["/home/app/.local/share/spoolman"]
ENTRYPOINT ["uvicorn", "spoolman.main:app"] ENTRYPOINT ["uvicorn", "spoolman.main:app"]
CMD ["--host", "0.0.0.0", "--port", "8000"] CMD ["--host", "0.0.0.0", "--port", "8000"]

View File

@@ -326,3 +326,16 @@ def check_write_permissions() -> None:
gid, gid,
) )
sys.exit(1) sys.exit(1)
def is_docker() -> bool:
"""Check if we are running in a docker container."""
return Path("/.dockerenv").exists()
def is_data_dir_mounted() -> bool:
"""Check if the data directory is mounted as a shfs."""
# "mount" will give us a list of all mounted filesystems
mounts = subprocess.run("mount", check=True, stdout=subprocess.PIPE, text=True) # noqa: S603, S607
data_dir = str(get_data_dir().resolve())
return any(data_dir in line for line in mounts.stdout.splitlines())

View File

@@ -95,6 +95,22 @@ async def startup() -> None:
logger.info("Startup complete.") logger.info("Startup complete.")
if env.is_docker() and not env.is_data_dir_mounted():
logger.warning("!!!! WARNING !!!!")
logger.warning("!!!! WARNING !!!!")
logger.warning("The data directory is not mounted.")
logger.warning(
'Spoolman stores its database in the container directory "%s". '
"If this directory isn't mounted to the host OS, the database will be lost when the container is stopped.",
env.get_data_dir(),
)
logger.warning(
"Please carefully read the docker part of the README.md file, "
"and ensure your docker-compose file matches the example.",
)
logger.warning("!!!! WARNING !!!!")
logger.warning("!!!! WARNING !!!!")
if __name__ == "__main__": if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000) uvicorn.run(app, host="0.0.0.0", port=8000)