0

I am trying to execute a shell script to call the Grafana API on startup, specifically:

curl -X POST -H "Content-Type: application/json" -d '{"name":"Algo Quant"}' http://admin:admin@grafana:3000/api/orgs

However when I do this I am getting the following error: curl: (7) Failed to connect to grafana port 3000: Connection refused

My compose file:

version: '3.7'
services:
  grafana-bootstrap-pre:
    build:
      context: $PWD
      dockerfile: .hack/build/docker/pre/Dockerfile
    env_file: 
    - $PWD/config/__envrc.d/grafana-bootstrap-pre.envrcd
    volumes:
    - $PWD/config/_ops_monitoring_grafana/provisioning.initial/:/etc/grafana/provisioning/provisioning.initial/
    - $PWD/config/_ops_monitoring_grafana/provisioning/current/:/etc/grafana/provisioning/current/
  grafana:
      image: grafana/grafana:9.2.3
      ports:
        - 3000:3000
      restart: unless-stopped
      volumes:
        - _volume__ops_monitoring_grafana:/var/lib/grafana
        - $PWD/config/plugins/:/var/lib/grafana/plugins/
        - $PWD/config/_ops_monitoring_grafana/provisioning/current/:/etc/grafana/provisioning/
        #- $PWD/config/_ops_monitoring_grafana/grafana.ini:/etc/grafana/grafana.ini
      depends_on:
      - "grafana-bootstrap-pre"
  grafana-bootstrap-post:
    build:
      context: $PWD
      dockerfile: .hack/build/docker/post/Dockerfile
    env_file: 
    - $PWD/config/__envrc.d/grafana-bootstrap-post.envrcd
    volumes:
    - $PWD/config/_ops_monitoring_grafana/provisioning.default/:/etc/grafana/provisioning/provisioning.default/
    - $PWD/config/_ops_monitoring_grafana/provisioning/current/:/etc/grafana/provisioning/current/
    depends_on:
      - "grafana"

volumes:
  _volume__ops_monitoring_grafana: {}

networks:
  default:
    external:
      name: services-network

My dockerfile for the offending container (post):

FROM ubuntu:20.04

COPY config/scripts/bootstrap.sh .

RUN apt-get update
RUN apt-get -y install curl

ENTRYPOINT ["/bin/bash","./bootstrap.sh", "--run"]

Which calls the following function:

curl -X POST -H "Content-Type: application/json" -d '{"name":"Algo Quant"}' http://admin:admin@grafana:3000/api/orgs

The strange thing which I cannot work out is that if i do this:

docker exec -it grafana-bootstrap-post-1 sh

Then execute the curl command here then I get the correct response.

  • 1
    This is part of why defining a healthcheck, so Docker doesn't consider a container ready for use until the services in it are finished starting up and actually usable, is important. – Charles Duffy Dec 21 '22 at 14:06
  • 1
    `depends_on:` normally just ensures the other container has started starting, but not that it's fully operational; you can normally expect the DNS name to resolve but if the other container takes some time to initialize it may not be able to handle requests yet. [Docker Compose wait for container X before starting Y](https://stackoverflow.com/questions/31746182/docker-compose-wait-for-container-x-before-starting-y) discusses a couple of approaches to this, including a health check approach as @CharlesDuffy suggests. Does this describe your problem? – David Maze Dec 21 '22 at 14:11

0 Answers0