title: How to simply scale a docker-compose service and pass the index and count to each?
body: <p>I'm looking for a way to scale up a docker-compose service &amp; saw the --scale option but could not find any method to get the index &amp; count within each container.</p>
  
  <p>Here's a simplified compose file:</p>
  
  <pre><code>version: '2.1'
  services:
    my_thing:
      restart: always
      build: .
      entrypoint: /entry.sh
      depends_on:
        - database
    database:
      restart: always
      image: postgres:12.1-alpine
      ...
  </code></pre>
  
  <p>If I did <code>docker-compose up my_thing --scale 5</code> within the entry I'd like to be able
  to see which instance I am (1 to 5) &amp; how many instances there are in total (5 in this example).</p>
  
  <p>I need this as the API 'my_thing' connects to needs this information to delegate events to each instance.</p>
  
  <p>For example my entry could be </p>
  
  <pre><code>echo "Hello I'm container $index of $count";
  </code></pre>
  
  <p>&amp; then when I run <code>docker-compose up my_thing --scale 5</code> (note that I'd prefer not to hard-code the the count)</p>
  
  <p>Then each container would respectively run the entry &amp; output:</p>
  
  <pre><code>Hello I'm container 1 of 5
  Hello I'm container 2 of 5
  Hello I'm container 3 of 5
  ... and so on
  </code></pre>
  
  <p>If any container crashed I'd like it restarted knowing it's index.</p>
  
  <p>Is this possible or do I need to figure out some alternative or build my own tooling?</p>
  
  <p>Edit:
  If there's any example of how to do something like this with docker swarm that may help too.</p>