Run ElasticSearch on different port through docker

By default, ElasticSearch runs on port 9200 for Rest API, using which we can communicate with ElasticSearch.

There are two ways we can run the ElasticSearch on different ports through docker.

Through docker-compose.yml

We can directly update the environment configuration in docker-compose.yml and run it on a different port.

The changes have to be made at two different places, the port at which elasticsearch run and the port at which docker will run it.

version: "3.7"
services:
  # Elasticsearch Docker Images: https://www.docker.elastic.co/
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.10.2
    container_name: elasticsearch
    environment:
      - xpack.security.enabled=false
      - discovery.type=single-node
      - http.port=8080 #different port
    ulimits:
      memlock:
        soft: -1
        hard: -1
      nofile:
        soft: 65536
        hard: 65536
    cap_add:
      - IPC_LOCK
    volumes:
      - elasticsearch-data:/usr/share/elasticsearch/data
    ports:
      - 8080:8080 #different port
      - 9300:9300

volumes:
  elasticsearch-data:
    driver: local

Through elasticsearch.yml

We can have the complete ElasticSearch-related configuration abstracted to a different file and then import it in docker-compose.yml.

elasticsearch.yml

http.port: 8080

docker-compose.yml

version: "3.7"
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.10.2
    container_name: elasticsearch
    environment:
      - xpack.security.enabled=false
      - discovery.type=single-node
    ulimits:
      memlock:
        soft: -1
        hard: -1
      nofile:
        soft: 65536
        hard: 65536
    cap_add:
      - IPC_LOCK
    volumes:
      - elasticsearch-data:/usr/share/elasticsearch/data
      - ./elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml #import elasticsarch config
    ports:
      - 8080:8080 #different port
      - 9300:9300

volumes:
  elasticsearch-data:
    driver: local

The docker-compose.yml and elasticsearch.yml are placed in the same directory that is why it is imported as ./elasticsearch.yml and then copied to /usr/share/elasticsearch/config/elasticsearch.yml