title: Defining tasks in a Docker compose file
body: <p>I have dockerized an older system that I work on. I have a docker compose file referencing an image built on postgres and an image built on tomcat.  This compose file is working well for starting and stopping a test version of the application.</p>
  
  <h3>Compose file</h3>
  
  <pre><code>services:
    db:
      image: mypostgres
      container_name: db
      networks:
        - mynetwork
    tomcat:
      image: mytomcat
      container_name: tomcat
      networks:
        - mynetwork
      environment:
        - myhost=localhost
      volumes:
        - "mydata:/mydata"
      depends_on:
        - db
  </code></pre>
  
  <h3>Running tasks within one of the containers</h3>
  
  <p>I have some additional command line tasks that I would like to perform on the system.  Some of these tasks are compute intensive.  These tasks operate on files in the mydata volume.</p>
  
  <p>Currently, I use the following command to run this task within the tomcat container.</p>
  
  <pre><code>docker exec tomcat /bin/my-script.sh param
  </code></pre>
  
  <h3>Could these tasks be defined with docker-compose?</h3>
  
  <p>I think that it could make sense to create a separate container to run this task.  If I was to define this as a service, it would look like the following.</p>
  
  <pre><code>  mycli:
      image: mytomcat
      networks:
        - mynetwork
      environment:
        - myhost=tomcat
      volumes:
        - "mydata:/mydata"
      depends_on:
        - db
        - mycli
      entrypoint: /bin/myscript.sh
      command:
        - param
  </code></pre>
  
  <p>What would the best practice for such a configuration?</p>
  
  <h3>What seems good about adding this to the compose file</h3>
  
  <ul>
  <li>The long-running task runs in a separate container.</li>
  <li>The availability of the task is nicely defined.</li>
  <li>This could be a nice way to perform a data initialization task.</li>
  </ul>
  
  <h3>What seems bad about adding this to the compose file</h3>
  
  <ul>
  <li>Some action must take place when the service is started.  Once that action is completed, the container is in an exited status.</li>
  <li>If I run a task with <code>docker-compose run</code>, the exited tasks seem to be hanging around after execution.</li>
  </ul>