Junior — Middle
How can you automate container restart in case of crash?
sobes.tech AI
Answer from AI
To automate the restart of a container upon its crash in Docker, you can use the --restart parameter when starting the container.
Main options for the --restart value:
no— do not restart the container (default).on-failure— restart the container only if it exits with an error (non-zero exit code).always— always restart the container, regardless of the reason for stopping.unless-stopped— restart the container unless it was manually stopped.
Example of starting a container with automatic restart on failure:
docker run --restart on-failure:3 my-container-image
Here, the container will be restarted up to 3 times upon crash.
If using Docker Compose, you can add to docker-compose.yml:
restart: on-failure
This will automatically manage the container's lifecycle without additional intervention.