Is it possible to create new containers on error?

461 Views Asked by At

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!

2

There are 2 best solutions below

2
Sahadat Hossain On

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.

0
j3ko On

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