Endless loop when running commands

I’d like to have a service deployed in Swarm run some commands then stop.

My compose file will start a service, run some commands, then run the commands again. As in an endless loop.

I added the following restart policy: restart: on-failure. When I deployed, I got the following message: Ignoring unsupported options: restart. However, the service ran the commands once.

My compose file is (showing relevant parts):

init-env:
    image: ...
    restart: on-failure
    depends_on:
      - ...
    entrypoint: ['/bin/sh','-c']
    command: |
        "
        # A bunch of commands are being executed in this section
        ...
        "
    networks:
      - my_network
    deploy:
      replicas: 1
      placement:
        constraints: [node.labels.server == 1]

What’s the best way to implement my desired scenario? Is the ignore message a false positive?

Using:

  • docker-compose v1.29.2, build unknown
  • docker version 23.0.0, build e92dd87
  • Ubuntu 22.04.2 LTS

If you use Swarm, you don’t need Docker compose so the version is irrelevant. Still thank you for sharing the versions without us asking for it.

I don’t know why it was restarted only once, but as the documentation says

I also searched for the error message: Ignoring unsupported options: restart

and the first result was this:

which refers to this part of the documentation:

That shows Compose file v3 reference where there were restart: and deploy.restart_policy options as well. Using the new compose file specification and Compose v2 you could use the same for Docker Compose and Swarm

https://docs.docker.com/compose/compose-file/deploy/#restart_policy

deploy:
  restart_policy:
    condition: on-failure
    delay: 5s
    max_attempts: 3
    window: 120s

The “condition” can also be “none”, so it will never be restarted.

Ah, I was setting incorrectly. Thanks!