Search code examples
dockerelasticsearchdocker-composedocker-container

How to prevent conflict between two separate docker-compose


I have two separate projects in two separate folders. when I run one of them, the second one cannot run because of conflict between ports.

The problem is for ElasticSearch image. Followings are two docker-compose files:

# /home/foder_1/
version: '3'
services:
  elasticsearch_ci:
    image: elasticsearch:7.14.2
    restart: always
    expose:
      - 9200
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=false
    env_file:
      - ./envs/ci.env
    container_name: elasticsearch_ci_pipeline

Second one:

# /home/folder_2/
version: '3'
services:
  
  elasticsearch:
    image: elasticsearch:7.14.2
    expose:
      - 9200
    volumes:
      - elastic_search_data_staging:/var/lib/elastic_search/data/
    environment:
      - discovery.type=single-node
      - xpack.security.enabled=false

When I run docker ps, I see the second ElasticSearch container created but it doesn't show its ports.
How can I solve the problem?

Update:
The problem is in this situation, my web application (django-base) cannot connect to the second elastic search instance.

Also, when I change port number in the second docker-compose for ES, (for example adding 9500 as Expose), again the port numbers of ES is the default ports (9200, 9300) plus my new port (9500) and my web application cannot connect to none of them.


Solution

  • Finally I found what the problem is.
    My server has only 4 GBs of RAM and when one elasticsearch is running, other instances of elasticsearch cannot start because the first instance consumes the most of RAM.

    if you want to run two separate instances of elasticsearch, you shoul consider at least 6 GBs of RAM per instance.