title: Docker healthcheck for nginx container
body: <p>I have a project using the official <code>nginx</code> docker container from Docker Hub, launching via Docker Compose. I have healthchecks configured in Docker Compose for each of my containers, and recently the healthcheck for this <code>nginx</code> container has been behaving strangely; on launching with <code>docker-compose up -d</code>, all my containers launch, and begin running healthchecks, but the <code>nginx</code> container looks like it never runs the healthcheck. I can manually run the script just fine if I <code>docker exec</code> into the container, and the healthcheck runs normally if I restart the container.</p>
  
  <p>Example output from <code>docker ps</code>:</p>
  
  <pre><code>CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS                            PORTS                                                                       NAMES
  458a55ae8971        my_custom_image           "/tini -- /usr/local…"   7 minutes ago       Up 7 minutes (healthy)                                                                                        project_worker_1
  5024781b1a73        redis:3.2                 "docker-entrypoint.s…"   7 minutes ago       Up 7 minutes (healthy)            127.0.0.1:6379-&gt;6379/tcp                                                    project_redis_1
  bd405dde8ce7        postgres:9.6              "docker-entrypoint.s…"   7 minutes ago       Up 7 minutes (healthy)            127.0.0.1:15432-&gt;5432/tcp                                                   project_postgres_1
  93e15c18d879        nginx:mainline            "nginx -g 'daemon of…"   7 minutes ago       Up 7 minutes (health: starting)   127.0.0.1:80-&gt;80/tcp, 127.0.0.1:443-&gt;443/tcp                                nginx
  </code></pre>
  
  <p>Example (partial, for brevity) output from <code>docker inspect nginx</code>:</p>
  
  <pre><code>    "State": {
          "Status": "running",
          "Running": true,
          "Paused": false,
          "Restarting": false,
          "OOMKilled": false,
          "Dead": false,
          "Pid": 11568,
          "ExitCode": 0,
          "Error": "",
          "StartedAt": "2018-02-13T21:04:22.904241169Z",
          "FinishedAt": "0001-01-01T00:00:00Z",
          "Health": {
              "Status": "unhealthy",
              "FailingStreak": 0,
              "Log": []
          }
      },
  </code></pre>
  
  <p>The portion of the <code>docker-compose.yml</code> defining the <code>nginx</code> container:</p>
  
  <pre><code>nginx:
    image: nginx:mainline
    # using container_name means there will only ever be one nginx container!
    container_name: nginx
    restart: always
    networks:
      - proxynet
    volumes:
      - /etc/nginx/conf.d
      - /etc/nginx/vhost.d
      - /usr/share/nginx/html
      - tlsdata:/etc/nginx/certs:ro
      - attachdata:/usr/share/nginx/html/uploads:ro
      - staticdata:/usr/share/nginx/html/static:ro
      - ./nginx/healthcheck.sh:/bin/healthcheck.sh
    healthcheck:
      test: ['CMD', '/bin/healthcheck.sh']
      interval: 1m
      timeout: 5s
      retries: 3
    ports:
      # Make the http/https ports available on the Docker host IPv4 loopback interface
      - '127.0.0.1:80:80'
      - '127.0.0.1:443:443'
  </code></pre>
  
  <p>The <code>healthcheck.sh</code> I am loading in as a volume:</p>
  
  <pre><code>#!/bin/bash
  
  service nginx status || exit 1
  </code></pre>
  
  <p>It looks like the problem is just an issue with systemd never returning from the status check when the container initially launches, and at the same time the configured healthcheck timeout does not trigger. Everything else works, and <code>nginx</code> is up and responding, but it would be nice for the healthcheck to function properly without needing to manually restart each time I start up.</p>
  
  <p>Is there something missing in my configuration, or a better check I can run?</p>