Score:0

Run custom s6 script in docker container once only

cn flag

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 avatar
cn flag
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".
Score:0
cn flag

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
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.