I would like to be able to execute code before service start and shutdown. Ideally, I would like a solution that is applicable to any service. For the sake of this question, let's assume I want to run echo up before the service starts and echo down before the service is requested to shutdown, without needing to know any specifics about how the service is defined.
x-lifetime-hooks:: &lifetime-hooks
# PART I DONT KNOW
services:
foo:
<<: *foo_definition # intentionally not shown
<<: *lifetime-hooks
Telling me that it's not possible is equally valuable information to me as a working solution, meaning I will mark it as an answer. If the general, reusable solution I described is not possible, then consider this specific service definition.
services
tailscale:
image: tailscale/tailscale
hostname: tailscale
environment:
TS_AUTH_KEY: ${TS_AUTH_KEY}
cap_add:
- net_admin
command: |
/bin/sh -c 'sh -s <<EOF
tailscaled --tun=userspace-networking &
until tailscale up --ssh --authkey=$$TS_AUTH_KEY; do sleep 0.1; done
sleep infinity
EOF'
Where I would like to run tailscale down on service shutdown, ideally avoiding having to create a Dockerfile.