On Failure Max Retry Policy In Docker Compose 2

Hi,

I’ve using docker compose 2. I want to set restart policy but don’t want to create loop.
For this there is max retry condition. But it seems I can’t use it in version 2.
I get error in Intellij when I want to set max retry like this -restart=on-failure[:2]

Do we need to use version 3 for this?

There is no version 3. Do you mean the version of the compose file? If you go to the docs and search for the restart policy in compose file v2, it doesn’t mention retrying:

Compose file v3 docs does mention “restart_policy” with “max_attempts”

Although I can’t see any mention in the Compose Specs:

Small addition to @rimelek’s answer: restart_policy and max_attempts are only valid for swarm services (regardless if deployed as swarm stack or directly as swarm service), it will be ignored for docker-compose deployments and be an invalid argument for `docker run´.

Oh, you are right.

Docker run supports it according to the docs, it just a different syntax. It actually how Cihad quoted it:

It just not the parameter of docker compose which I forgot to mention.

your syntax for -restart=on-failure[:2] is incorrect. Try with -restart=on-failure:2

In YAML, it should be restart: on-failure:2

1 Like

You are right that syntax works in Docker compose without swarm. I didn’t know that, so thanks for helping us out :slight_smile: I also didn’t think @guzelcihad would use on-failure[:2] instead of on-failure:2, but reading it again, there is a good chance it happened.

I was indeed not aware that --restart=on-failure allows to set max retries. I learned something new today :slight_smile:

You could have learned it on April 13 :smiley:

I just totally missed those square brackets in the command

You mean I could have learned it on April 13th :smiley: I must admit back in the day it didn’t cought my attention. I just focused on the two configuration items for swarm deployments.

Yes, I fixed my post :slight_smile:

1 Like