From: Adam Dullage Date: Sat, 18 Mar 2023 21:57:56 +0000 (+0000) Subject: Docker deployment improvements #26 X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=d39c9beb41b5e4d21f6925af405aaf03b22077af;p=flatnotes.git Docker deployment improvements #26 --- diff --git a/.dockerignore b/.dockerignore index 0ed27f0..117b888 100644 --- a/.dockerignore +++ b/.dockerignore @@ -8,3 +8,4 @@ !package.json !package-lock.json !flatnotes/** +!entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 50cb1d1..7239fca 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,55 +1,47 @@ +ARG BUILD_DIR=/build + +# Build Container FROM --platform=$BUILDPLATFORM python:3.11-slim-bullseye AS build -RUN apt update && apt install -y npm +ARG BUILD_DIR -ARG BUILD_DIR=/build RUN mkdir ${BUILD_DIR} WORKDIR ${BUILD_DIR} +RUN apt update && apt install -y npm + COPY package.json package-lock.json ./ RUN npm ci COPY flatnotes/src ./flatnotes/src RUN npm run build - +# Runtime Container FROM python:3.11-slim-bullseye -ARG USER=flatnotes -ARG UID=1000 -ARG GID=1000 +ARG BUILD_DIR -ARG APP_DIR=/app -ARG DATA_DIR=${APP_DIR}/data +ENV PUID=1000 +ENV PGID=1000 -ENV FLATNOTES_PATH=${DATA_DIR} -ENV PORT=80 +ENV APP_PATH=/app +ENV FLATNOTES_PATH=/data -RUN addgroup \ - --gid $GID \ - ${USER} \ - || echo "Group '${GID}' already exists." +RUN mkdir -p ${APP_PATH} +RUN mkdir -p ${FLATNOTES_PATH} -RUN adduser \ - --disabled-password \ - --gecos "" \ - --ingroup ${USER} \ - --uid ${UID} \ - ${USER} \ - || echo "User '${UID}' already exists." +RUN apt update && apt install -y gosu \ + && rm -rf /var/lib/apt/lists/* RUN pip install pipenv -RUN mkdir -p ${DATA_DIR} -RUN chown -R ${UID}:${GID} ${APP_DIR} -WORKDIR ${APP_DIR} - -USER ${UID} +WORKDIR ${APP_PATH} -COPY --chown=${UID}:${GID} LICENSE Pipfile Pipfile.lock ./ -RUN pipenv install --deploy --ignore-pipfile +COPY LICENSE Pipfile Pipfile.lock ./ +RUN pipenv install --deploy --ignore-pipfile --system COPY flatnotes ./flatnotes -COPY --from=build --chown=${UID}:${GID} /build/flatnotes/dist ./flatnotes/dist +COPY --from=build ${BUILD_DIR}/flatnotes/dist ./flatnotes/dist -ENTRYPOINT [ "pipenv", "run", "python", "-m", "uvicorn", "main:app", "--app-dir", "flatnotes", "--host", "0.0.0.0", "--port", "${PORT}" ] +COPY entrypoint.sh / +ENTRYPOINT [ "/entrypoint.sh" ] diff --git a/README.md b/README.md index c93682c..8d1b1e2 100644 --- a/README.md +++ b/README.md @@ -22,18 +22,18 @@ Equally, the only thing flatnotes caches is the search index and that's incremen The easiest way to install flatnotes is using Docker. -Note: To use either of the options below, please ensure the current directory contains a folder called "data" and that it's writable by user 1000. - ### Example Docker Run Command ```shell docker run -d \ + -e "PUID=1000" \ + -e "GUID=1000" \ -e "FLATNOTES_AUTH_TYPE=password" \ -e "FLATNOTES_USERNAME=user" \ -e "FLATNOTES_PASSWORD=changeMe" \ -e "FLATNOTES_SECRET_KEY=aLongRandomSeriesOfCharacters" \ - -v "$(pwd)/data:/app/data" \ - -p "80:80" \ + -v "$(pwd)/data:/data" \ + -p "8080:8080" \ dullage/flatnotes:latest ``` @@ -46,16 +46,18 @@ services: container_name: flatnotes image: dullage/flatnotes:latest environment: + PUID: 1000 + GUID: 1000 FLATNOTES_AUTH_TYPE: "password" FLATNOTES_USERNAME: "user" FLATNOTES_PASSWORD: "changeMe!" FLATNOTES_SECRET_KEY: "aLongRandomSeriesOfCharacters" volumes: - - "./data:/app/data" - # - "./index:/app/data/.flatnotes" - # Optional. Allows you to save the search index in a different location. + - "./data:/data" + # Optional. Allows you to save the search index in a different location: + # - "./index:/data/.flatnotes" ports: - - "80:80" + - "8080:8080" restart: unless-stopped ``` diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..a7159c1 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +set -e + +USERNAME=flatnotes + +echo Setting up user and group... +addgroup \ + --gid ${PGID} \ + ${USERNAME} \ + || echo "Group '${PGID}' already exists." + +adduser \ + --disabled-password \ + --gecos "" \ + --uid ${PUID} \ + --gid ${PGID} \ + ${USERNAME} \ + || echo "User '${PUID}' already exists." + +echo Setting file permissions... +chown -R ${PUID}:${PGID} ${FLATNOTES_PATH} + +echo Starting flatnotes... +cd ${APP_PATH} +exec gosu ${PUID}:${GUID} \ + python -m \ + uvicorn \ + main:app \ + --app-dir flatnotes \ + --host 0.0.0.0 \ + --port 8080