title: Docker-Compose Entrypoint Using Command Exit Code As A Failure Detection Mechanism For A Dependent Service, Always Exits With 0
body: <p>I am using <em>docker-compose</em> to start a <em>minio</em> service and a <em>minio/mc</em> service (<em>createbuckets</em>) to create a default bucket. For <em>minio</em> failure detection, I am using the exit code of the <em>mc</em> command that adds a host to the config for failure detection. The <em>docker-compose</em> file is listed below. </p>
  
  <p>I cannot seem to be able to get the <em>createbuckets</em> service to return an exit code of any value other than 0 when the <em>mc add host</em> command fails in the <em>entrypoint</em> command string. Can anyone spot why this error is occurring? I am running this in a MacOS environment. I have found that the expected behaviour almost works in a Linux OS Environment. Please refer to added comment below.</p>
  
  <p>For info, the entrypoint checks the success of the command to add minio as a host to the mc command client config. I have specified a restart policy of <em>on-failure</em> so that I can signal a failure exit code of 1 if this fails, thus restarting the <em>createbuckets</em> service until it can connect to minio. </p>
  
  <pre><code>version: "3"
  services:
    minio:
      image: minio/minio
      ports:
        - "9000:9000"
      volumes:
        - ./docker/minio-data:/export
        - ./docker/minio-config:/root/.minio
      environment:
        - "MINIO_ACCESS_KEY=accesskey"
        - "MINIO_SECRET_KEY=secretkey"
      command: server /export
      healthcheck:
          test: ["CMD", "curl", "-f", "http://localhost:9000"]
          interval: 30s
          timeout: 10s
          retries: 5
  
    createbuckets:
      image: minio/mc
      depends_on:
        - minio
      restart: on-failure
      entrypoint: &gt;
          /bin/sh -c "
          mc config host add myminio http://minio:9000 accesskey secretkey;
          success=$$?;
          if [ $$success -ne 0 ]; then \
            echo error encountered;
          else \
            /usr/bin/mc rm -r --force myminio/uploads;
            /usr/bin/mc mb myminio/uploads;
            /usr/bin/mc policy download myminio/uploads; \
          fi
          echo the variable value is $$success;
          if [ $$success -ne 0 ]; then exit 1; else exit 0; fi
        "
  </code></pre>
  
  <h1>Solved using netcat for failure detection of minio</h1>
  
  <p>Based on <a href="https://stackoverflow.com/a/48215226/8325270">https://stackoverflow.com/a/48215226/8325270</a> and <a href="https://8thlight.com/blog/dariusz-pasciak/2016/10/17/docker-compose-wait-for-dependencies.html" rel="nofollow noreferrer">https://8thlight.com/blog/dariusz-pasciak/2016/10/17/docker-compose-wait-for-dependencies.html</a> I discarded the use of a command's <em>exit</em> status as a failure detection method for connection to minio. This was done in favour of the <em>netcat</em> utility. The docker compose file is listed below. Furthermore, I removed the <em>healthcheck</em> section in the <em>minio</em> service since conditional <em>depends_on</em> was removed in v3 of the docker-compose file specification.</p>
  
  <pre><code>version: "3.4"
  services:
    minio:
      image: minio/minio
    ports:
      - "9000:9000"
    volumes:
      - ./docker/minio-data:/export
      - ./docker/minio-config:/root/.minio
    environment:
      - "MINIO_ACCESS_KEY=accesskey"
      - "MINIO_SECRET_KEY=secretkey"
    command: server /export
  
  
  createbuckets:
    image: minio/mc
    links:
      - minio
    restart: on-failure
    entrypoint: &gt;
      /bin/sh -c "
        echo Waiting for minio service to start...;
        while ! nc -z minio 9000;
        do
          sleep 1;
        done;
        echo Connected!;
        mc config host add myminio http://minio:9000 accesskey secretkey;
        /usr/bin/mc rm --recursive --force myminio/uploads;
        /usr/bin/mc mb myminio/uploads;
        /usr/bin/mc policy download myminio/uploads;
        /usr/bin/mc policy upload myminio/uploads;
        exit 0;
      "
  </code></pre>