Multiple zrok shares on docker

Hi! I want to have two shares, each from a seperate docker-compose. Each has there own unique name domain and a different target.

I tried to use the same volume where .zrok is stored but then it overwrites the reserved share of the one that started first and changes it's target.

zrok backend is already reserved: stockbrood
zrok-epic-share-1  | INFO: running: zrok share reserved stockbrood --override-endpoint http://host.docker.internal:3000 --headless                                                                                                                                  

I also tried to use a different volume and keep things seperate but then it tries to enable the environment with the same key cause both .env have the same ZROK_ENABLE_TOKEN. I need to enable the environment so that it creates the .zrok folder in the volume but it can't create it cause it can't execute zrok enable twice.

I wonder if it is possible to have seperate state directories of different shares in docker. Currently it fills the state directory with the zrok enable command but this can't be run twice with the same ZROK_ENABLE_TOKEN (I think or at least it gives an error that there was a problem enabling the environment).

[EDIT]
I got it working with customising the zrok-share.bash locally.
I made my reserved.json support multiple reserved shares in one enabled environment.

{
  "shares": [
    {
      "token": "stockbrood",
      "target": "http://host.docker.internal:80",
      "frontend_endpoints": [
        "https://stockbrood.share.zrok.io"
      ]
    }
  ]
}

instead of

{"token":"stockbrood","frontend_endpoints":["https://stockbrood.share.zrok.io"]}

The key logic that checks for existing shares:

EXISTING_SHARES=$(jq -r '.shares[]?.target' ~/.zrok/reserved.json 2>/dev/null)
FOUND_SHARE=false
    
while IFS= read -r target; do
  if [[ "${target}" == "${ZROK_TARGET}" ]]; then
    FOUND_SHARE=true
    ZROK_RESERVED_TOKEN=$(jq -r --arg target "${ZROK_TARGET}" '.shares[] | select(.target == $target) | .token' ~/.zrok/reserved.json)

The part that adds new shares to the array instead of overwriting:

jq --arg token "${NEW_TOKEN}" \
   --arg target "${ZROK_TARGET}" \
   --argjson endpoints "${NEW_ENDPOINTS}" \
   '.shares += [{"token": $token, "target": $target, "frontend_endpoints": $endpoints}]' \
   ~/.zrok/reserved.json

Welcome, @NinoV-2469197. I'm glad you found a good solution. That's tidier than redundant volumes for each share.

FYI: A forthcoming release of zrok has a background "agent" mode that will simplify multiple shares, so we'll be rewriting the Docker share guides soon.

1 Like