title: How do I create a (dockerized) Elasticsearch index using a python script running in a docker container?
body: <p>I'm trying to index a containerized Elasticsearch db using the Python client <a href="https://github.com/elastic/elasticsearch-py" rel="nofollow noreferrer">https://github.com/elastic/elasticsearch-py</a> called from a script (running in a container too).</p>
  <p>By looking at existing pieces of code, it seems that <code>docker-compose</code> is a useful tool to use for my purpose. My dir structure is</p>
  <pre><code>docker-compose.yml
  indexer/
  - Dockerfile
  - indexer.py
  - requirements.txt
  elasticsearch/
  - Dockerfile
  </code></pre>
  <p>My <code>docker-compose.yml</code> reads</p>
  <pre class="lang-yaml prettyprint-override"><code>version: '3'
  
  services:
    elasticsearch:
      build: elasticsearch/
      ports: 
        - 9200:9200
      networks:
        - deploy_network
      container_name: elasticsearch
  
    indexer:
      build: indexer/
      depends_on:
        - elasticsearch
      networks:
        - deploy_network
      container_name: indexer
    
  networks:
    deploy_network:
      driver: bridge
  </code></pre>
  <p><code>indexer.py</code> reads</p>
  <pre class="lang-py prettyprint-override"><code>from elasticsearch import Elasticsearch
  from elasticsearch.helpers import bulk
      
  es = Elasticsearch(hosts=[{&quot;host&quot;:'elasticsearch'}]) # what should I put here?
  
  actions = [
      {
      '_index' : 'test',
      '_type' : 'content',
      '_id' : str(item['id']),
      '_source' : item,
      }
  for item in [{'id': 1, 'foo': 'bar'}, {'id': 2, 'foo': 'spam'}]
  ]
      
  # create index
  print(&quot;Indexing Elasticsearch db... (please hold on)&quot;)
  bulk(es, actions)
  print(&quot;...done indexing :-)&quot;)
  </code></pre>
  <p>The Dockerfile for the elasticsearch service is</p>
  <pre><code>FROM docker.elastic.co/elasticsearch/elasticsearch-oss:6.1.3
  EXPOSE 9200
  EXPOSE 9300
  </code></pre>
  <p>and that for the indexer is</p>
  <pre><code>FROM python:3.6-slim
  WORKDIR /app
  ADD . /app
  RUN pip install -r requirements.txt
  ENTRYPOINT [ &quot;python&quot; ]
  CMD [ &quot;indexer.py&quot; ]
  </code></pre>
  <p>with <code>requirements.txt</code> containing only <code>elasticsearch</code> to be downloaded with pip.</p>
  <p>Running with <code>docker-compose run indexer</code> gives me the error message at <a href="https://pastebin.com/6U8maxGX" rel="nofollow noreferrer">https://pastebin.com/6U8maxGX</a> (<code>ConnectionRefusedError: [Errno 111] Connection refused</code>).
  elasticsearch is up as far as I can see with <code>curl -XGET 'http://localhost:9200/' </code>or by running <code>docker ps -a</code>.</p>
  <p>How can I modify my <code>docker-compose.yml</code> or <code>indexer.py</code> to solve the problem?</p>
  <p>P.S. A (working) version (informed by the answers below) of the code can be found here, for completeness' sake: <a href="https://github.com/davidefiocco/dockerized-elasticsearch-indexer" rel="nofollow noreferrer">https://github.com/davidefiocco/dockerized-elasticsearch-indexer</a>.</p>