Hasan Can Saral
Hasan Can Saral

Reputation: 3288

Does Docker support restarting containers every X seconds

I have a Logstash container that keeps two data sources sync. When it runs, it queries non-synced entries in one database and posts them into the other. I would like to run this container say every 10 seconds.

What I have been doing is to specify --restart=always so that when the container exits, it restarts itself, around which takes around 5 seconds, which is a bit too often for this use case.

Does Docker support what I want to achieve (waiting X seconds between restarts, or any kind of scheduling) or should I remove the restart policy and schedule it with cron to run every 10 seconds?

Upvotes: 3

Views: 8775

Answers (1)

michael_bitard
michael_bitard

Reputation: 4212

If your container exits succesfully, it will be restarted immediately with --restart=always

An ever increasing delay (double the previous delay, starting at 100 milliseconds) is added before each restart to prevent flooding the server. This means the daemon will wait for 100 ms, then 200 ms, 400, 800, 1600, and so on until either the on-failure limit is hit, or when you docker stop or docker rm -f the container.

Here is your part I guess:

If a container is successfully restarted (the container is started and runs for at least 10 seconds), the delay is reset to its default value of 100 ms.

What you can do is:

  • Restart your container with a cron every 10 seconds
  • Configure a cron inside your container and launch logstash every 10 seconds
  • Use a shell script which, in a loop, launch logstash then sleep 10
  • Maybe logstash has something like that already built-in? (I know that for example the jdbc-input-plugin has some schedule params)

Upvotes: 3

Related Questions