diff --git a/Dockerfile b/Dockerfile index 423aed3..40dab6f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ # Use an official Python runtime as a parent image -FROM python:3.9-slim +FROM python:3.12-slim # Set the working directory in the container WORKDIR /app @@ -10,25 +10,16 @@ COPY . /app # Install the Anki sync server RUN pip install anki -# Set environment variables for the sync server (don't change) +# Set environment variables for the sync server (do not change) ENV SYNC_BASE=/data ENV SYNC_HOST=0.0.0.0 ENV SYNC_PORT=8080 -# Add user: -# Add additional users via SYNC_USER2, SYNC_USER3, ... -# Generate passwords via https://git.sr.ht/~laalsaas/pbkdf2-password-hash -ENV SYNC_USER1=user:pass -ENV PASSWORDS_HASHED=1 - -# Set maximum request size (in MB) -ENV MAX_SYNC_PAYLOAD_MEGS=100 +# Create the data directory +RUN mkdir -p /data # Expose the port the sync server runs on EXPOSE 8080 -# Create the data directory -RUN mkdir -p /data - -# Run the sync server -CMD ["python", "-m", "anki.syncserver"] +# Run the script to update the package and start the server +CMD ["/app/start.sh"] diff --git a/README.md b/README.md index b7631f4..8fd2ecb 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,38 @@ # Anki Sync Server Docker Container -This docker container simply wraps the official minimal sync server based on Python. More information can be found in the [official documentation](https://docs.ankiweb.net/anki-syncserver.html). +This docker container simply wraps the official minimal sync server based on Python. More information can be found in the [official documentation](https://docs.ankiweb.net/sync-server.html). ## Configuration -The docker container uses the same environmental variable as described in the [official documentation](https://docs.ankiweb.net/anki-syncserver.html). Some meaningful defaults are set. One might want to change the following environmental variables: +The docker container uses the same environmental variable as described in the [official documentation](https://docs.ankiweb.net/sync-server.html). For more information, please consult it directly. The following environmental variables are bound to the container's setup and therefore, should not be changed: -- `SYNC_USER1`: The username and password for the first user in the format `user:pass`. -- Additional users can be created using `SYNC_USER2`, `SYNC_USER3`, ... -- `PASSWORDS_HASHED`: Indicates if the passwords are hashed using [PBKDF2](https://git.sr.ht/~laalsaas/pbkdf2-password-hash) (default: `1`). -- `MAX_SYNC_PAYLOAD_MEGS`: Maximum allowed request size in MB (default: `100`). +- `SYNC_BASE` +- `SYNC_HOST` +- `SYNC_PORT` + +The following environmental variables has to be set outside of the container. Failing to do so, will abort the start of the container +- `SYNC_USER1`: The username and password for the first user in the format `user:pass`. ## Running the Docker Container To start the Anki sync server in a Docker container: ```sh -docker run -d --name anki-syncserver -p 8080:8080 -v ./data:/data ghcr.io/lorenzschmid/anki-syncserver +docker run \ + -d \ + --name anki-syncserver \ + -p 8080:8080 \ + -v ./data:/data \ + -e SYNC_USER1=user:pass \ + ghcr.io/lorenzschmid/anki-syncserver ``` - `-d`: Runs the container in detached mode. - `--name`: Assigns a name to the container (`anki-syncserver`). - `-p 8080:8080`: Maps port `8080` on the host to port `8080` in the container. - `-v ./data:/data`: Mounts the host directory `./data` to the container’s `/data` directory for data persistence. +- `-e SYNC_USER1=user:pass`: Sets the `SYNC_USER1` environment variable to configure the sync server’s username and password. or via docker compose: @@ -36,8 +45,6 @@ services: container_name: anki-syncserver environment: - SYNC_USER1=user:pass - - PASSWORDS_HASHED=1 - - MAX_SYNC_PAYLOAD_MEGS=100 ports: - "8080:8080" volumes: diff --git a/start.sh b/start.sh new file mode 100644 index 0000000..a9de93f --- /dev/null +++ b/start.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +# Ensure all stdout and stderr are sent to the Docker logs +exec > >(tee -i /proc/1/fd/1) 2>&1 + +# Check if SYNC_USER1 is set +if [ -z "$SYNC_USER1" ]; then + echo "ERROR: SYNC_USER1 environment variable is not set. Exiting." + exit 1 +fi + +# Update the Anki sync server package +echo "Updating Anki sync server package..." +pip install --upgrade anki + +# Start the Anki sync server +echo "Starting Anki sync server..." +exec python -m anki.syncserver