Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid CORS origin error with docker and elastic search image

I contrived to bypass the CORS origin error client side by setting up an apache web server with docker so as to use modules. Nevertheless, when I add elasticsearch service to my docker-compose and I try to get my json response from it I still have it. It says "Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:9200/"

my docker-compose :

version: '2'

services:
  apache:
    image: 'bitnami/apache:latest'
    ports:
      - '80:8080'
      - '443:8443'
    volumes:
      - .:/app
  es01:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.7.0
    container_name: es01
    environment:
      - node.name=es01
      - cluster.name=es-docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - data01:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - elastic

volumes:
  data01:
    driver: local


networks:
  elastic:
    driver: bridge

my index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
</head>
<body>

<div class="home">
    <h5>Elastic home</h5>
    <p> Cluster name:</p>
    <p id="cluster_name"></p>
    <p> Cluster version: </p>
    <p id="cluster_version"></p>
</div>

<link rel="stylesheet" href="style.css">
<script type="application/javascript">
    fetch('http://localhost:9200')
        .then(async (response) => {
            let data = await response.json()
            document.getElementById('cluster_name').innerText = data.cluster_name
            document.getElementById('cluster_version').innerText = data.version.number
        })
</script>
</body>
</html>
like image 927
ezakrazer Avatar asked Mar 03 '23 09:03

ezakrazer


1 Answers

I found out how, I changed my environment elastic service to

   environment:
     - discovery.type=single-node
     - node.name=es01
     - cluster.name=es-docker-cluster
     - bootstrap.memory_lock=true
     - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
     - http.host=0.0.0.0
     - http.port=9200
     - "http.cors.allow-origin=http://localhost"
     - "http.cors.enabled=true"
     - "http.cors.allow-headers=X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization"
     - "http.cors.allow-credentials=true"
like image 127
ezakrazer Avatar answered Mar 05 '23 04:03

ezakrazer