Cannot Connect Elasticsearch in Docker

I am trying to launch my app using docker that connects to elasticsearch but I am not able to do so. I get the following exception:

vaibhavsaxena@Vaibhavs-MacBook-Pro Final Project % docker-compose up                  
Creating network "finalproject_elastic" with driver "bridge"
Pulling elastic (docker.elastic.co/elasticsearch/elasticsearch-oss:7.5.1)...
7.5.1: Pulling from elasticsearch/elasticsearch-oss
c808caf183b6: Pull complete
cb741d01a765: Pull complete
76d572f17adb: Pull complete
0c73850c446a: Pull complete
200be7ea5d3a: Pull complete
d26eb6addf17: Pull complete
8847b79c210c: Pull complete
Digest: sha256:d83fc37781fcb20a03c9ee7d5783ee1082e8156e75e6b2c417d1d9655db89a9c
Status: Downloaded newer image for docker.elastic.co/elasticsearch/elasticsearch-oss:7.5.1
Creating finalproject_elastic_1 ... done
Creating finalproject_myimage_1 ... done
Attaching to finalproject_myimage_1, finalproject_elastic_1
elastic_1  | OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.
elastic_1  | {"type": "server", "timestamp": "2020-05-21T04:58:35,302Z", "level": "WARN", "component": "o.e.b.ElasticsearchUncaughtExceptionHandler", "cluster.name": "docker-cluster", "node.name": "becbe6f2a65a", "message": "uncaught exception in thread [main]", 
elastic_1  | "stacktrace": ["org.elasticsearch.bootstrap.StartupException: java.lang.IllegalStateException: cannot downgrade a node from version [7.7.0] to version [7.5.1]",
elastic_1  | "at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:163) ~[elasticsearch-7.5.1.jar:7.5.1]",
elastic_1  | "at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:150) ~[elasticsearch-7.5.1.jar:7.5.1]",
elastic_1  | "at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-7.5.1.jar:7.5.1]",
elastic_1  | "at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:125) ~[elasticsearch-cli-7.5.1.jar:7.5.1]",
elastic_1  | "at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-7.5.1.jar:7.5.1]",
elastic_1  | "at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:115) ~[elasticsearch-7.5.1.jar:7.5.1]",
elastic_1  | "at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-7.5.1.jar:7.5.1]",
elastic_1  | "Caused by: java.lang.IllegalStateException: cannot downgrade a node from version [7.7.0] to version [7.5.1]",
elastic_1  | "at org.elasticsearch.env.NodeMetaData.upgradeToCurrentVersion(NodeMetaData.java:94) ~[elasticsearch-7.5.1.jar:7.5.1]",
elastic_1  | "at org.elasticsearch.env.NodeEnvironment.loadOrCreateNodeMetaData(NodeEnvironment.java:426) ~[elasticsearch-7.5.1.jar:7.5.1]",
elastic_1  | "at org.elasticsearch.env.NodeEnvironment.<init>(NodeEnvironment.java:304) ~[elasticsearch-7.5.1.jar:7.5.1]",
elastic_1  | "at org.elasticsearch.node.Node.<init>(Node.java:273) ~[elasticsearch-7.5.1.jar:7.5.1]",
elastic_1  | "at org.elasticsearch.node.Node.<init>(Node.java:253) ~[elasticsearch-7.5.1.jar:7.5.1]",
elastic_1  | "at org.elasticsearch.bootstrap.Bootstrap$5.<init>(Bootstrap.java:221) ~[elasticsearch-7.5.1.jar:7.5.1]",
elastic_1  | "at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:221) ~[elasticsearch-7.5.1.jar:7.5.1]",
elastic_1  | "at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:349) ~[elasticsearch-7.5.1.jar:7.5.1]",
elastic_1  | "at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:159) ~[elasticsearch-7.5.1.jar:7.5.1]",
elastic_1  | "... 6 more"] }
finalproject_elastic_1 exited with code 1

The python function thats calling this step is:

def get_data_es():
    ES_HOST = {
            "host": "elastic",
            "port": 9200
            }
    
    ES_INDEX = "radius_ml_posts"
    ES_TYPE = {'author': 'vaibhav'}
    
    es = Elasticsearch(hosts=[ES_HOST,])
    count = 0
    while True:
        try:
            es.ping()
            print("Elasticsearch server bounded at url: {}".format(es.cat.master().split()[-2]))
            results_gen = elasticsearch.helpers.scan(
                es,
                query={"query": {"match_all": {}}},
                index=ES_INDEX
            )
            break
        except elasticsearch.exceptions.ConnectionError:
            time.sleep(15)
            count += 1
            print("Trying to connect to elasticsearch server. Retry number: {}".format(count))
            print("Connections info: {}".format(es.transport.connection_pool.connections))
    return list(results_gen)

The docker-compose.yml file looks as follows:

    version: '2.2'
    services:
      elastic:
        image: docker.elastic.co/elasticsearch/elasticsearch-oss:7.5.1
        container: elastic
        environment:
          - discovery.type=single-node
          - bootstrap.memory_lock=true
          - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
        ulimits:
          memlock:
            soft: -1
            hard: -1
        volumes:
          - data01:/usr/share/elasticsearch/data
        ports:
          - 9200:9200
        networks:
          - elastic

      myimage:
        image: myimage:myversion
        ports:
          - 8080:8080
        expose:
          - 8080
        networks:
          - elastic


volumes:
  data01:
    driver: local

networks:
  elastic:
    driver: bridge

I run the following commands to execute this:

docker build -t myimage:myversion .

docker-compose up

I would really appreciate some help here.

Thanks.

volumes:
          - data01:/usr/share/elasticsearch/data

I would guess that your data01 dir contains data from a previous attempt where you tried to install 7.7. Can you bind mount a new dir so that you can start fresh ?

Thanks for your reply.
How can I achieve this?

Hi ,

Please check https://docs.docker.com/compose/compose-file/#volume-configuration-reference as this is not an elastic specific question. In summary, use something else than data01