Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart Docker Containers when they Crash Automatically

I want to restart a container if it crashes automatically. I am not sure how to go about doing this. I have a script docker-compose-deps.yml that has elasticsearch, redis, nats, and mongo. I run this in the terminal to set this up: docker-compose -f docker-compose-deps.yml up -d. After this I set up my containers by running: docker-compose up -d. Is there a way to make these containers restart if they crash? I noticed that docker has a built in restart, but I don't know how to implement this.

After some feedback I added restart: always to my docker-compose file and my docker-compose-deps.yml file. Does this look correct? Or is this how you would implement the restart always?

docker-compose sample

myproject-server:
  build: "../myproject-server"
  dockerfile: Dockerfile-dev
  restart: always
  ports:
  - 5880:5880
  - 6971:6971
  volumes:
  - "../myproject-server/src:/src"
  working_dir: "/src"
  external_links:
  - nats
  - mongo
 - elasticsearch
 - redis
myproject-associate:
  build: "../myproject-associate"
  dockerfile: Dockerfile-dev
  restart: always
  ports:
  - 5870:5870
  volumes:
  - "../myproject-associate/src:/src"
  working_dir: "/src"
  external_links:
  - nats
  - mongo
  - elasticsearch
  - redis

docker-compose-deps.yml sample

nats:
  image: nats
  container_name: nats
  restart: always
  ports:
  - 4222:4222
mongo:
  image: mongo
  container_name: mongo
  restart: always
  volumes:
  - "./data:/data"
  ports:
  - 27017:27017
like image 397
daveskylark Avatar asked Oct 18 '16 13:10

daveskylark


2 Answers

If you're using compose, it has a restart flag which is analogous to the one existing in the docker run command, so you can use that. Here is a link to the documentation about this part - https://docs.docker.com/compose/compose-file/

When you deploy out, it depends where you deploy to. Most container clusters like kubernetes, mesos or ECS would have some configuration you can use to auto-restart your containers. If you don't use any of these tools you are probably starting your containers manually and can then just use the restart flag just as you would locally.

like image 164
Yaron Idan Avatar answered Sep 22 '22 07:09

Yaron Idan


Looks good to me. What you want to understand when working on Docker policies is what each one means. always policy means that if it crashes for any reason automatically restart.

So if it stops for any reason, go ahead and restart it.

So why would you ever want to use always as opposed to say on-failure?

In some cases, you might have a container that you always want to ensure is running such as a web server. If you are running a public web application chances are you want that server to be available 100% of the time.

So for web application I expect you want to use always. On the other hand if you are running a worker process on a file and then naturally exit, that would be a good use case for the on-failure policy, because the worker container might be finished processing the file and you probably want to let it close out and not have it restart.

Thats where I would expect to use the on-failure policy. So not just knowing the syntax, but when to apply which policy and what each one means.

like image 27
Daniel Avatar answered Sep 26 '22 07:09

Daniel