Custom sync server - Anki does not initiate

I built a docker sync server using these directions: anki/docs/syncserver at main · ankitects/anki · GitHub

When I try to start Anki with SYNC_USER1=user:pass anki --syncserver Anki does not start and I must press Ctrl + C

Anki starting…
2024-11-16T10:57:50.888891Z INFO listening addr=0.0.0.0:8080
^CTraceback (most recent call last):
File “anki.syncserver”, line 14, in run_sync_server
File “anki._backend”, line 93, in syncserver
Exception

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “”, line 1, in
File “aqt”, line 38, in
File “anki.syncserver”, line 14, in run_sync_server
KeyboardInterrupt

If I set https://localhost::8080/ under Self-hosted sync server option I receive this warning: Invalid sync server specified. Please check the preferences.

1 Like

I just want to confirm, which of the following are you trying to do?

  1. Run the custom sync server directly on your local PC.
  2. Run it inside a Docker container.

If it’s option 1, you don’t need to refer to the link you provided. Just follow the instructions on this page.

Anki starting…
2024-11-16T10:57:50.888891Z INFO listening addr=0.0.0.0:8080

These lines indicate that the custom sync server has started correctly. If you’d like to run the Anki desktop client on the same PC, simply start Anki in a separate command session.

If it’s option 2, you can follow the instructions in the link you provided, and run the following steps to start the custom sync server:

  1. Build the Docker image with the docker build command. Make sure to configure <Dockerfile> and <version> appropriately in the command.
    • Example:
      docker build -f docs/syncserver/Dockerfile --no-cache --build-arg ANKI_VERSION=24.06.3 -t anki-sync-server .
  2. Create and start the container from the image with the docker run command.

You can check the server logs with the following command:

docker logs anki-sync-server


If you want to start the server using the docker compose up command, my previous post might be helpful.


If I set https://localhost::8080/ under Self-hosted sync server option I receive this warning: Invalid sync server specified. Please check the preferences.

The documentation says that “The server listens on an unencrypted HTTP connection”, so try replacing https with http.

3 Likes

Thank you for your detailed response!!

1 Like

@hkr Is there a way to setup a sync server with SSL Certificates to secure the data transfer?

There are various ways to achieve that, but I think one of the simplest methods is to configure a reverse proxy using Caddy. From this thread, it appears you’ve already experimented with Nginx, but if your goal is just to enable HTTPS, Caddy is much easier to configure. You can refer to the documentation here: Automatic HTTPS — Caddy Documentation.

Here’s an example configuration:

First, get your own domain name (if you don’t already have one).

Next, here is an example setup for an Anki custom sync server with Caddy:

.
├── CaddyFile 
├── Dockerfile
└── compose.yaml

CaddyFile

YOUR.DOMAIN.NAME {
    reverse_proxy anki-sync-server:8080
    log {
		output file /var/log/caddy/access.log
	}
}

Replace YOUR.DOMAIN.NAME with your own domain name.

Dockerfile

Use the Dockerfile provided by the Anki documentation.

compose.yaml

services:
  caddy-server:
    image: caddy:latest
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - ./caddy/config:/config
      - ./caddy/log:/var/log/caddy/
      # Location to store certificates and ACME information
      - ./caddy/data:/data
    ports:
      - "443:443"

  anki-sync-server:
    build:
      context: .
      args:
        ANKI_VERSION: 24.11
    tty: true
    environment:
      - SYNC_USER1=user:password
      - SYNC_BASE=/.syncserver
    volumes:
      - ./.syncserver:/.syncserver
    expose:
      - 8080

Finally, make sure to enter https://your.domain.name in the sync server field in the Anki client settings (for Desktop, AnkiDroid, or AnkiMobile). There’s no need to include a port number (it should work even if you explicitly specify port 443).

Screenshot

3 Likes

You are the best @hkr. Thanks for your answer. I am happy to test it :star_struck:

1 Like

@opensourcer Does that work for you? I have the server on my localhost for testing purposes, but I always get the following message:


Here is my setup:
Caddyfile:

localhost {
    reverse_proxy anki-sync-server:8080
    log {
        output file /var/log/caddy/access.log
    }
}

Dockerfile:

FROM rust:1.82.0-alpine3.20 AS builder

ARG ANKI_VERSION

RUN apk update && apk add --no-cache build-base protobuf && rm -rf /var/cache/apk/*

RUN cargo install --git https://github.com/ankitects/anki.git \
--tag ${ANKI_VERSION} \
--root /anki-server  \
anki-sync-server

FROM alpine:3.20.2

RUN adduser -D -h /home/anki anki

COPY --from=builder /anki-server/bin/anki-sync-server /usr/local/bin/anki-sync-server


RUN apk update && apk add --no-cache bash && rm -rf /var/cache/apk/*

USER anki

ENV SYNC_PORT=${SYNC_PORT:-"8080"}

EXPOSE ${SYNC_PORT}

CMD ["anki-sync-server"]

# This health check will work for Anki versions 24.08.x and newer.
# For older versions, it may incorrectly report an unhealthy status, which should not be the case.
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD wget -qO- http://localhost:${SYNC_PORT}/health || exit 1

LABEL maintainer="Jean Khawand <jk@jeankhawand.com>"

→ from

compose.yml:

services:
  caddy-server:
    image: caddy:latest
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - ./caddy/config:/config
      - ./caddy/log:/var/log/caddy/
      # Location to store certificates and ACME information
      - ./caddy/data:/data
    ports:
      - "443:443"

  anki-sync-server:
    build:
      context: .
      args:
        ANKI_VERSION: 24.11
    tty: true
    environment:
      - SYNC_USER1=user:password
      - SYNC_BASE=/.syncserver
    volumes:
      - ./.syncserver:/.syncserver
    expose:
      - 8080

@hkr Do you see the issue. I would like to have the server in my local network first and in next step I’ll move to a cloud server. Before I would like to see that all is working…

Can you look in the ./caddy/data folder on your host and see if there’s a certificate there for you to install? But right now I have it set up with Traefik so there shouldn’t be any trouble there at least (in regards to Anki sync-server with reverse proxies).

Thank you for your quick reply @voczi
What do you mean by installing the certificate?
under caddy/data/caddy/certificates/local/localhost I see:

  • localhost.crt
  • localhost.json
  • localhost.key

Do I have to install the .crt file on the clients?

Yep.
If you get an actual domain for this, I think you can just use Let’s Encrypt without having to install any certificates on the client-side.

Can I also use self-sign certificates?

Yes, that’s what I do. GitHub - chris2511/xca: X Certificate and Key management is decent for this.