简体   繁体   中英

CURL not working well with docker-compose, Elasticsearch and Windows 10

My docker-compose.yml file is listed below. This is what I want:

  • Spin up an Elasticsearch instance through docker-compose
  • Run a setup file in the container of the Elasticsearch image
  • The setup file should be 'copied' from my host machine (so using volumes)

This is what I got so far:

version: '3'

services:
  b-elastic:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.11.1
    container_name: b-elastic
    environment:
      - discovery.type=single-node
    volumes:
      - ./dockerelastic:/usr/share/elasticsearch/data
      - ./:/project
    ports:
      - 9200:9200 

I can verify that the setupfile is present:

$ docker exec -it b-elastic vi /project/database/elasticsearch/setup.json

gives:

{
    "settings": {
        "index" : {
            "max_ngram_diff": "3"
        },
        "analysis": {
            "analyzer": {
                "username_analyzer": {
                    "tokenizer": "username_tokenizer",
                    "filter": [
                        "lowercase"
                    ]
                }
            },
            "tokenizer": {
                "username_tokenizer": {
                    "type": "ngram",
                    "min_gram": "1",
                    "max_gram": "4"
                }
            }
        }
    },
    "mappings": {
        "properties": {
            "_all" : { "enabled" : false },
            "username": {
                "type": "text",
                "analyzer": "username_analyzer"
            }
        }
    }
}

Now I want to execute the setup.json on Elasticsearch:

$ docker exec -it b-elastic curl -X PUT http://localhost:9200/users -H  "Content-Type: application/json" --data-binary @project/database/elasticsearch/setup.json

This is what Docker returns:

Warning: Couldn't read data from file
Warning: "project/database/elasticsearch/setup.json", this makes an empty
Warning: POST.
{"acknowledged":true,"shards_acknowledged":true,"index":"users"}

It looks like it is acknowledged, but when I try to search for users, I get this error:

[match] analyzer [username_analyzer] not found

I don't get this error when copy paste the contents of setup.json in Postman and execute the request there.

How can I execute a file through a volume in an Elasticsearch container on Windows?

Edit: The accepted answer works, but note that it failed for me when executing the command with PowerShell. Command Prompt worked.

Looks like curl wasn't able to read your setup.json file, so it created an empty index.
Notice that when you are using vi, you are using the absolute path (/ in the start), but when you are using curl you didn't. Try

docker exec -it b-elastic curl -X PUT http://localhost:9200/users -H "Content-Type: application/json" --data-binary @/project/database/elasticsearch/setup.json

This instead and see if it works

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM