# Can't connect to ElasticSearch from python API in Docker-Compose setup

**URL:** https://discuss.elastic.co/t/cant-connect-to-elasticsearch-from-python-api-in-docker-compose-setup/287031
**Category:** Elasticsearch
**Tags:** docker
**Created:** [October 18, 2021, 9:10pm UTC](https://discuss.elastic.co/t/cant-connect-to-elasticsearch-from-python-api-in-docker-compose-setup/287031 "2021-10-18T21:10:30Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![amace](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@amace](https://discuss.elastic.co/u/amace)
#### Post date: [October 18, 2021, 9:10pm UTC](https://discuss.elastic.co/t/cant-connect-to-elasticsearch-from-python-api-in-docker-compose-setup/287031/1 "2021-10-18T21:10:30Z")

</div>

Here is how I create the ES container in Docker-Compose:

```auto
db:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.15.1
    restart: always
    ports:
      - 9200:9200
      - 9300:9300
    ulimits:
      memlock:
        soft: -1
        hard: -1
    environment:
      discovery.type: single-node
      network.host: db

```

Here is how I'm attempting to connect from the Python API:

```auto
from elasticsearch import Elasticsearch
es = Elasticsearch(
    {'host': 'db', 'port': 9200, 'use_ssl': False},
)

```

localhost:9200 works fine when the container is running, but the connection does not work. Whenever I dont set the network.host ENV the error I get is

```auto
Errno -2: Name or Service not known

```

And when I do set the ENV the error is

```auto
Errno -5: No address associated with this host name

```

Any help or advice would be very, very appreciated. Thank you!

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [October 18, 2021, 10:09pm UTC](https://discuss.elastic.co/t/cant-connect-to-elasticsearch-from-python-api-in-docker-compose-setup/287031/2 "2021-10-18T22:09:52Z")

</div>

Welcome to our community! 😃

Try setting `network.host` to `0.0.0.0`.

---

<div class="post-metadata">

### Author: ![amace](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@amace](https://discuss.elastic.co/u/amace)
#### Post date: [October 18, 2021, 10:12pm UTC](https://discuss.elastic.co/t/cant-connect-to-elasticsearch-from-python-api-in-docker-compose-setup/287031/3 "2021-10-18T22:12:18Z")

</div>

Thanks for the warm welcome warkolm! I did try that previously and unfortunately it gave me the same error as it does with the hostname set to db.

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [October 18, 2021, 10:24pm UTC](https://discuss.elastic.co/t/cant-connect-to-elasticsearch-from-python-api-in-docker-compose-setup/287031/4 "2021-10-18T22:24:58Z")

</div>

Where did you set that and what was the error?

---

<div class="post-metadata">

### Author: ![amace](https://avatars.discourse-cdn.com/v4/letter/a/96bed5/32.png) [@amace](https://discuss.elastic.co/u/amace)
#### Post date: [October 18, 2021, 10:33pm UTC](https://discuss.elastic.co/t/cant-connect-to-elasticsearch-from-python-api-in-docker-compose-setup/287031/5 "2021-10-18T22:33:36Z")

</div>

I set it in the same place I set "network.host: db" and the error was  
"No address associated with this host name"

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [November 15, 2021, 10:33pm UTC](https://discuss.elastic.co/t/cant-connect-to-elasticsearch-from-python-api-in-docker-compose-setup/287031/6 "2021-11-15T22:33:48Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
