title: Docker-compose: Why restart does not work with docker-compose run or when there's only one container in docker-compose file
body: <p>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 <code>restart: on-failure</code> would automatically restart the container if it failed.</p>
  <p>The command looks like that</p>
  <pre><code>docker-compose run --rm \
              -e VAR1=value1 \
              [...] \
              web flask tasks my_failing_task
  </code></pre>
  <p>My <code>docker-compose.yml</code> looks like that</p>
  <pre><code>version: &quot;3&quot;
  
  services:
    web:
      user: root
      image: my-image
      network_mode: &quot;host&quot;
      environment:
        APPLICATION: &quot;web&quot;
        GOOGLE_APPLICATION_CREDENTIALS: &quot;mysecret.json&quot;
      volumes:
        - ../../../stuff/:/stuff/
      restart: on-failure:3
  </code></pre>
  <p>I have noticed that the container does not restart when I use <code>docker-compose run</code>.</p>
  <p>I have then tried to move the command inside the <code>docker-compose.yml</code>, like this:</p>
  <pre><code>version: &quot;3&quot;
  
  services:
    web:
      user: root
      image: my-image
      network_mode: &quot;host&quot;
      command: flask tasks my_failing_task
      environment:
        APPLICATION: &quot;web&quot;
        GOOGLE_APPLICATION_CREDENTIALS: &quot;mysecret.json&quot;
        VAR1: value1
      volumes:
        - ../../../stuff/:/stuff/
      restart: on-failure:3
  </code></pre>
  <p>And execute <code>docker-compose up</code>, but same result.</p>
  <p>It seems that <code>restart</code> only works with <code>docker-compose up</code> when I add another container, like a redis for example</p>
  <pre><code>version: &quot;3&quot;
  
  services:
    web:
      user: root
      image: my-image
      network_mode: &quot;host&quot;
      command: flask tasks my_failing_task
      environment:
        APPLICATION: &quot;web&quot;
        GOOGLE_APPLICATION_CREDENTIALS: &quot;mysecret.json&quot;
        VAR1: value1
      volumes:
        - ../../../stuff/:/stuff/
      restart: on-failure:3
  
    redis:
      hostname: redis
      image: redis
      ports:
        - &quot;6379:6379&quot;
  </code></pre>
  <p>Then it actually restarts up to 3 times if fails.</p>
  <p>So my questions are:</p>
  <ul>
  <li>Why doesn't <code>restart</code> work with <code>run</code></li>
  <li>Why does <code>restart</code> only work with <code>up</code> IF there are more than 1 container in the <code>docker-compose.yml</code> file</li>
  </ul>
  <p>Thanks!</p>