10

I have a docker-compose.yml file with a following:

services:
  kafka_listener:
    build: .
    command: bundle exec ./kafka foreground
    restart: always
  # other services

Then I start containers with: docker-compose up -d

On my amazon instance kafka-server (for example) fails to start sometimes, so ./kafka foregound script fails. When typing docker ps I see a message: Restarting (1) 11 minutes ago. I thought docker should restart failed container instantly, but it seems it doesn't. After all, container has been restarted in about 30 minutes since first failed attempt.

Is there any way to tell Docker-Compose to restart container instantly after failure?

nattfodd
  • 1,790
  • 1
  • 17
  • 35

1 Answers1

9

You can use this policy :

  • on-failure

The on-failure policy is a bit interesting as it allows you to tell Docker to restart a container if the exit code indicates error but not if the exit code indicates success. You can also specify a maximum number of times Docker will automatically restart the container. like on-failure:3 It will retry 3 times.

  • unless-stopped

The unless-stopped restart policy behaves the same as always with one exception. When a container is stopped and the server is rebooted or the Docker service is restarted, the container will not be restarted.

Hope this will help you in this problem.

Thank you!

chintan thakar
  • 1,440
  • 1
  • 15
  • 25
  • 1
    Is this `docker-compose.yml` 2.0 or 3.0? – nattfodd Mar 27 '17 at 07:28
  • Its version 3.0 – chintan thakar Mar 27 '17 at 07:34
  • It looks like these options are available only for `deploy:` key, which is for Swarm only. I need these to be available using `docker-compose up` command. – nattfodd Mar 27 '17 at 07:50
  • @nattfodd you can use any of these options with `docker-compose up` `no`,`on-failure`,`unless-stopped`,`always` – chintan thakar Mar 27 '17 at 07:59
  • 1
    I already use this option. It works, but it restarts service after 30 minutes, not instantly. That's the question. And other options tends to not be available with 2.0 config file verson. – nattfodd Mar 27 '17 at 09:36
  • If its frequently restart after every 30 minutes than you need to monitor your application logs. is it something wrong from application that cause the container to stop on some particular events. Have you verified your application logs ? – chintan thakar Mar 27 '17 at 09:40