# Create two Elasticsearch hosts in Docker

**URL:** <https://discuss.elastic.co/t/create-two-elasticsearch-hosts-in-docker/369694>\
**Category:** Elasticsearch\
**Tags:** docker\
**Created:** [October 28, 2024, 7:16pm UTC](https://discuss.elastic.co/t/create-two-elasticsearch-hosts-in-docker/369694 "2024-10-28T19:16:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![louisdeveloper](https://avatars.discourse-cdn.com/v4/letter/l/b9bd4f/32.png) [@louisdeveloper](https://discuss.elastic.co/u/louisdeveloper)\
**Post date:** [October 28, 2024, 7:16pm UTC](https://discuss.elastic.co/t/create-two-elasticsearch-hosts-in-docker/369694/1 "2024-10-28T19:16:36Z")

</div>

I tried creating two elasticsearch 8 virtual machines in Docker, but I end up having two problems:

- One of the machines always stops
- The server always has a high CPU consumption

I did it this way:

```auto
docker pull docker.elastic.co/elasticsearch/elasticsearch:8.2.3

docker network create elastic
docker run --name es01 --net elastic -p 9200:9200 -p 9300:9300 -it docker.elastic.co/elasticsearch/elasticsearch:8.2.3

docker network create elastic02
docker run --name es02 --net elastic02 -p 9201:9200 -p 9301:9300 -it docker.elastic.co/elasticsearch/elasticsearch:8.2.3

```

As soon as I start the second `docker run` command the CPU goes from a Load Average of 0.2 to 30.00

And this is not a production environment, there are only 4 people using it.

---

<div class="post-metadata">

**Author:** ![stephenb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stephenb/32/40856_2.png) [@stephenb](https://discuss.elastic.co/u/stephenb)\
**Post date:** [October 28, 2024, 7:37pm UTC](https://discuss.elastic.co/t/create-two-elasticsearch-hosts-in-docker/369694/2 "2024-10-28T19:37:46Z")

</div>

First why such an older version?

What are your Docker resources? If Docker resources are set to unlimited on the machine, both containers will try to claim 50% of the host memory and will most likely fail.

Peraps look at the docs [here](https://www.elastic.co/guide/en/elasticsearch/reference/8.2/docker.html#_setting_jvm_heap_size) that describe exactly what you are seeing

BTW those containers will each start as their own elasticsearch cluster... not as part of a single cluster..

So perhaps you should use docker compose... that is in the docs as well...

> **[Install Elasticsearch with Docker | Elasticsearch Guide \[8.2\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.2/docker.html#docker-compose-file)**

---

<div class="post-metadata">

**Author:** ![Justin\_Castilla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/justin_castilla/32/135379_2.png) [@Justin\_Castilla](https://discuss.elastic.co/u/Justin_Castilla)\
**Post date:** [October 28, 2024, 7:54pm UTC](https://discuss.elastic.co/t/create-two-elasticsearch-hosts-in-docker/369694/3 "2024-10-28T19:54:55Z")

</div>

Edit: I would definitely check out the link Stephen shared for best practices. My post is just an expansion of what you have to get you up and running.

Hello, there's a couple things to try that might get both ES running. I can verify that your current docker commands won't work for what you want.

- Both containers must be on the same Docker network (`elastic`) to communicate.
- Set `discovery.seed_hosts` to point to the other nodes in the cluster.
- Define `cluster.initial_master_nodes` to include all master-eligible nodes when starting a new cluster.
- You can disable security features for a non-production environment by setting `xpack.security.enabled=false`. (Not recommended for production.)
- If you prefer to keep security enabled, you need to configure SSL certificates and authentication settings as per the [Elasticsearch security documentation](https://www.elastic.co/guide/en/elasticsearch/reference/8.2/security-settings.html).  
-Set the `ES_JAVA_OPTS` environment variable to allocate less memory. [Documentation here](https://www.elastic.co/guide/en/elasticsearch/reference/current/advanced-configuration.html)

```auto
# Create a single network for both containers
docker network create elastic

# Start the first node
docker run --name es01 --net elastic -p 9200:9200 -p 9300:9300 \
  -e "node.name=es01" \
  -e "cluster.name=es-docker-cluster" \
  -e "discovery.seed_hosts=es02" \
  -e "cluster.initial_master_nodes=es01,es02" \
  -e "xpack.security.enabled=false" \
  -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
  -it docker.elastic.co/elasticsearch/elasticsearch:8.2.3

# Start the second node
docker run --name es02 --net elastic -p 9201:9200 -p 9301:9300 \
  -e "node.name=es02" \
  -e "cluster.name=es-docker-cluster" \
  -e "discovery.seed_hosts=es01" \
  -e "cluster.initial_master_nodes=es01,es02" \
  -e "xpack.security.enabled=false" \
  -e "ES_JAVA_OPTS=-Xms512m -Xmx512m" \
  -it docker.elastic.co/elasticsearch/elasticsearch:8.2.3

```

Then run a quick health status check of your cluster:

```auto
curl -X GET "localhost:9200/_cluster/health?pretty"

```

I was able to get two instances running and verified on my end. Let me know if this works!

---

<div class="post-metadata">

**Author:** ![louisdeveloper](https://avatars.discourse-cdn.com/v4/letter/l/b9bd4f/32.png) [@louisdeveloper](https://discuss.elastic.co/u/louisdeveloper)\
**Post date:** [October 31, 2024, 9:39pm UTC](https://discuss.elastic.co/t/create-two-elasticsearch-hosts-in-docker/369694/4 "2024-10-31T21:39:44Z")

</div>

That was the problem. Restricting RAM works.
