Skip to content

Commit

Permalink
Make pip package update independent of docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzschmid committed Sep 3, 2024
1 parent 4efd48b commit 75c8ca7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
21 changes: 6 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"]
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -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:

Expand All @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions start.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 75c8ca7

Please sign in to comment.