Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
179 views
in Technique[技术] by (71.8m points)

Docker-compose: Why restart does not work with docker-compose run or when there's only one container in docker-compose file

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In the code, docker-compose run always implies restart: no. The GitHub issue docker/compose#6302 describes this a little bit further.

docker-compose run is used to run a "one-off" container, running a single alternate command with mostly the same specification as what's described in docker-compose.yml. Imagine Compose didn't have this override. If you did docker-compose run a command that failed, it would restart, potentially forever. If it ran in the background, you'd leak a restarting container; if it ran in the foreground, you'd have to go to another terminal to kill off the container. That's a harsh penalty for typing docker-compose run web bsah by accident.

Otherwise, most of the options, including restart:, get passed through directly to the Docker API. It shouldn't make a difference running docker-compose up if there's only one container or multiple.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...