In my docker-compose.yaml I am saying to alway restart app service

    build: .
    image: app:latest
      - 3000:3000
    restart: always
     - "./:/usr/src/app"

This works, but in case of permanent error (eg. server is running some other way, …), the service keeps trying to start. Is there way to limit number of attempts to restart the service?

Here is a demo code, which I created to take us on same page:


You can find the answer in the docker compose reference description here:


You can apply a condition to the restart block inside of the compose file. In your case something like:

  condition: on-failure   
  max-attempts: 3
  • It seems to me what you refer to, is for docker stacks. I'll try it at home, but I think this is ignored by docker compose. – Mailo Světel Apr 8 '20 at 17:45
  • @MailoSvětel Did you try the solution? – OYPS Apr 11 '20 at 7:44
  • Sorry for the delay, I was on other stuff. It works just as I suspected. Check the updated question for links to demo code – Mailo Světel Apr 12 '20 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.