0

I have compose.yml file:

  api:
    restart: on-failure
    command: uvicorn app:app
    ...

  jobs:
    restart: on-failure
    command: python job.py
    ...

job.py:

import asyncio
from prometheus_client import start_http_server

async def background(sio):
    await asyncio.gather(...) # <- there are many tasks with while True


start_http_server(5000)
asyncio.run(background(sio))

It works okay. After stopping everything turns off. But when I restart system, jobs container starting automatically. Why?! api is not starting, but why jobs starting?

Alex Poloz
  • 366
  • 7
  • 22
  • I'm not sure, but my guess would be that when Docker sends a SIGTERM to python, it results in a non-zero exit code (an error) but uvicorn doesn't, but I don't know for sure. – eten Feb 24 '23 at 07:24
  • If that is the case, you can gracefully process the SIGTERM with this answer https://stackoverflow.com/questions/18499497/how-to-process-sigterm-signal-gracefully – eten Feb 24 '23 at 07:28
  • https://docs.docker.com/config/containers/start-containers-automatically/ – Hans Kilian Feb 24 '23 at 08:49

2 Answers2

0

I think Docker is just trying to run the containers that were running before the system shutdown. Maybe try to change restart policy to no to prevent restarting on startup?

  jobs:
    restart: no
protob
  • 3,317
  • 1
  • 8
  • 19
0

According to the documentation unless-stopped is probably the right option.

  jobs:
    restart: unless-stopped
    command: python job.py
Mr.Hem
  • 25
  • 5