Docker deployment improvements #26
authorAdam Dullage <redacted>
Sat, 18 Mar 2023 21:57:56 +0000 (21:57 +0000)
committerAdam Dullage <redacted>
Sat, 18 Mar 2023 21:57:56 +0000 (21:57 +0000)
.dockerignore
Dockerfile
README.md
entrypoint.sh [new file with mode: 0644]

index 0ed27f0421c19dfbecefe1186bb74faba1df82b9..117b8880384c9295fd8482e41811c229609a4086 100644 (file)
@@ -8,3 +8,4 @@
 !package.json
 !package-lock.json
 !flatnotes/**
+!entrypoint.sh
index 50cb1d189a5ff544484c178de9c9bbda175b74fb..7239fca0d7e508f9edfac20b11126e4cbd3bcbe4 100644 (file)
@@ -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" ]
index c93682c0bab9102d97148d6e74c35417f4081908..8d1b1e2ee9d39b725680fb1d0cb602aceaf9a6a0 100644 (file)
--- 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 (file)
index 0000000..a7159c1
--- /dev/null
@@ -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
git clone https://git.99rst.org/PROJECT