How to import Kibana dashboard using docker-compose file?

Viewed 1739

I exported Kibana dashboard with cURL command:

curl -XGET localhost:5601/api/kibana/dashboards/export?dashboard=[uuid] > my-dashboards.json

Now I want to import this dashboard using docker-compose, but I get this error:

test.kibana           | Warning: Couldn't read data from file "my-dashboards.json", this makes an 
test.kibana           | Warning: empty POST.
test.kibana           |   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
test.kibana           |                                  Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed connect to 0.0.0.0:5601; Connection refused

This is docker-compose file:

version: '2'
services:
  elasticsearch-docker:
    image: docker.elastic.co/elasticsearch/elasticsearch:5.6.3
    container_name: test.elastic
    ports:
      - 9200:9200
      - 9300:9300
    volumes:
      - ./elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=false
  kibana:
    image: docker.elastic.co/kibana/kibana:5.6.3
    container_name: test.kibana
    command: curl -XPOST test.elastic:5601/api/kibana/dashboards/import -H 'kbn-xsrf:true' -H 'Content-type:application/json' -d @my-dashboards.json
    ports:
      - 5601:5601
    depends_on: 
      - elasticsearch-docker
    volumes:
      - ./kibana.yml:/usr/share/kibana/config/kibana.yml
      - ./my-dashboards.json:/usr/share/kibana/config/my-dashboards.json
2 Answers

Once both Elasticsearch and Kibana have started up, run that curl command, from the host, in the directory that has the JSON dump.

Programmatically telling that both have started up is surprisingly tricky. In principle you can do something like this by writing an ENTRYPOINT script that does the first-time setup before it launches the actual service, but that's easier if the configuration is in the filesystem (IIRC Kibana actually stores this sort of data in ES). If you read through the way, say the official mysql image does this, it involves starting the service, waiting for it to actually come up, doing the initialization, and then stopping it again before exec "$@" in the end.

You can't do everything via Docker Compose, and if your first-time setup depends on both a host-local file and the service actually running, it's probably easier to not try to shoe-horn it in.

The curl command being executed cannot find my-dashboards.json in the working directory of the container. You can go to the path /usr/share/kibana/config by setting the working_dir property inside the compose file as such:

 ...
 kibana:
    image: docker.elastic.co/kibana/kibana:5.6.3
    container_name: test.kibana
    working_dir: /usr/share/kibana/config
 ...
Related