0

I'm using automation (ansible) to deploy a dockerised app (gitea). The app runs s6 in its docker entrypoint.

I want to run my own script:

  • after the container's first load (so the files and db are ready)
  • at most once

OPTION 1:
I thought to add a script /etc/s6/custom/run and place my stuff in there. But it would be restarted continuously, and there's no guarantee of order.

OPTION 2:
I read the docs for s6-svc which show an -O switch to run the service at most once. But I can't use that in an automated fashion, and also it may run before the other s6 services have run.

So I thought of this instead:

  • add a /etc/s6/custom/run script
  • it checks whether the app is "ready"
    • if no: it does nothing, and exits
    • if yes: it does it's job, then creates a /etc/s6/custom/down file to signal that it should not be restarted

PROBLEM:
The problem is for the "no" case: until the container is running that custom script will be restarted many times.

Is it possible to delay restarting it, or, a better way entirely?

lonix
  • 896
  • 10
  • 23
  • Whoever voted to close, your reason is dumb and vindictive: it does "demonstrate reasonable information technology management practices" and is not "related to unsupported hardware or software platforms". – lonix Jun 25 '23 at 00:56

1 Answers1

0

My solution, assuming s6's services in the container are located at /etc/s6/:

docker-compose.yml:

volumes:
  - ./data:/data                   # a mapped or named volume
  - ./run:/etc/s6/custom/run:ro

run:

#!/bin/sh

# ensure not run (successfully) before
if [ -f /data/custom-init-performed ]; then
  echo 'INFO: custom init already performed'
  s6-svc -D /etc/s6/custom     # prevent s6 from restarting service
  exit 0
fi

# ensure container healthy
# if you're using healthchecks then use whatever url is applicable here
# if not, use another approach to determine whether container is ready
until [ $(curl -sf http://localhost:3000/api/healthz | grep -E '^  "status": "pass",$' | wc -l) = 1 ]; do
  echo 'WARN: container not healthy yet, will retry...' >&2
  sleep 5
done

# do once-only init work
# ...

# prevent script's core logic from running again
touch /data/custom-init-performed

# prevent s6 from restarting service
s6-svc -D /etc/s6/custom

exit 0
lonix
  • 896
  • 10
  • 23