0

Quick question: Is it possible to create new containers on error? I know that when a container exits we can restart it (given the correct parameter) but instead of a restart I would like to recreate because I don’t want to maintain the state that was on the container. Any help is appreciated. Thanks!

Manuel
  • 17
  • 1
  • 5
  • What difference will it make by creating a new container? – Dashrath Mundkar Mar 18 '21 at 17:33
  • Creating a new container means that we don't reuse a container that already as a state. As an example, using logstash the pipelines will try to continue with files that are on /tmp. Recreating the container avoids this issue – Manuel Mar 18 '21 at 18:11

2 Answers2

0

Basically when you run a docker image, it get run into a docker container. It's kind of an abstraction. So If you want to recreate the container then just run the image again.

To run a container you just need to provide the image that it uses: docker run <docker_image>

One thing is need to remember, if you use the same docker image to create multiple docker container then all of them will function same, so if you want different function then you need to create new docker image and then run that image.

Sahadat Hossain
  • 3,583
  • 2
  • 12
  • 19
  • someone downvoted. not me. How can i run the image again, in an automatic way upon error on the container? Docker run with the restart option always, puts a container up automatically but its the same one that failed. I need a method similar to that but that recreates the container and not only restarts it. Any such tool or approach is feasible? – Manuel Mar 18 '21 at 18:09
  • There is no such automatic way, but you can do this if you run your containers in kubernetes. – Sahadat Hossain Mar 18 '21 at 18:13
0

It might not be ideal but you can have the docker container set to --restart=no and an external cron job set to run something like:

docker inspect --format '{{json .State.Running}}' container-name

If it comes back false, manually run the commands to recreate the container, something like:

docker-compose rm container-name
docker-compose up -d container-name
j3ko
  • 81
  • 1
  • 9