I want to execute a command using of a docker-compose file, and the code sometimes fails because of connection timeouts. I thought that adding restart: on-failure
would automatically restart the container if it failed.
The command looks like that
docker-compose run --rm
-e VAR1=value1
[...]
web flask tasks my_failing_task
My docker-compose.yml
looks like that
version: "3"
services:
web:
user: root
image: my-image
network_mode: "host"
environment:
APPLICATION: "web"
GOOGLE_APPLICATION_CREDENTIALS: "mysecret.json"
volumes:
- ../../../stuff/:/stuff/
restart: on-failure:3
I have noticed that the container does not restart when I use docker-compose run
.
I have then tried to move the command inside the docker-compose.yml
, like this:
version: "3"
services:
web:
user: root
image: my-image
network_mode: "host"
command: flask tasks my_failing_task
environment:
APPLICATION: "web"
GOOGLE_APPLICATION_CREDENTIALS: "mysecret.json"
VAR1: value1
volumes:
- ../../../stuff/:/stuff/
restart: on-failure:3
And execute docker-compose up
, but same result.
It seems that restart
only works with docker-compose up
when I add another container, like a redis for example
version: "3"
services:
web:
user: root
image: my-image
network_mode: "host"
command: flask tasks my_failing_task
environment:
APPLICATION: "web"
GOOGLE_APPLICATION_CREDENTIALS: "mysecret.json"
VAR1: value1
volumes:
- ../../../stuff/:/stuff/
restart: on-failure:3
redis:
hostname: redis
image: redis
ports:
- "6379:6379"
Then it actually restarts up to 3 times if fails.
So my questions are:
- Why doesn't
restart
work with run
- Why does
restart
only work with up
IF there are more than 1 container in the docker-compose.yml
file
Thanks!
question from:
https://stackoverflow.com/questions/65855229/docker-compose-why-restart-does-not-work-with-docker-compose-run-or-when-there 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…