My docker-compose looks like below:

version: "3.0"
services:
  elasticsearch:
    container_name: elastic-container
    image: docker.elastic.co/elasticsearch/elasticsearch:7.13.4
    environment:
      - xpack.security.enabled=false
      - "discovery.type=single-node"
    networks:
      - ek-net
    ports:
      - 9200:9200
networks:
  ek-net:
    driver: bridge

I have another container (created from a simple Dockerfile) which executes a ruby ​​script to connect to http://localhost:9200/however I get the following error:

/usr/local/lib/ruby/3.1.0/socket.rb:1214:in `__connect_nonblock': Failed to open TCP connection to localhost:9200 (Cannot assign requested address - connect(2) for [::1]:9200) (Faraday::ConnectionFailed)
    from /usr/local/lib/ruby/3.1.0/socket.rb:1214:in `connect_nonblock'
    from /usr/local/lib/ruby/3.1.0/socket.rb:56:in `connect_internal'
    from /usr/local/lib/ruby/3.1.0/socket.rb:137:in `connect'
    from /usr/local/lib/ruby/3.1.0/socket.rb:642:in `block in tcp'
    from /usr/local/lib/ruby/3.1.0/socket.rb:227:in `each'
    from /usr/local/lib/ruby/3.1.0/socket.rb:227:in `foreach'
    from /usr/local/lib/ruby/3.1.0/socket.rb:632:in `tcp'
    from /usr/local/lib/ruby/3.1.0/net/http.rb:998:in `connect'
    from /usr/local/lib/ruby/3.1.0/net/http.rb:976:in `do_start'
    from /usr/local/lib/ruby/3.1.0/net/http.rb:965:in `start'
    from /usr/local/bundle/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:138:in `request_via_get_method'
    from /usr/local/bundle/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:129:in `request_with_wrapped_block'
    from /usr/local/bundle/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:122:in `perform_request'
    from /usr/local/bundle/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:66:in `block in call'
    from /usr/local/bundle/gems/faraday-1.10.0/lib/faraday/adapter.rb:50:in `connection'
    from /usr/local/bundle/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:64:in `call'
    from /usr/local/bundle/gems/faraday-1.10.0/lib/faraday/rack_builder.rb:154:in `build_response'
    from /usr/local/bundle/gems/faraday-1.10.0/lib/faraday/connection.rb:516:in `run_request'
    from /usr/local/bundle/gems/elastic-transport-8.0.1/lib/elastic/transport/transport/http/faraday.rb:49:in `block in perform_request'
    from /usr/local/bundle/gems/elastic-transport-8.0.1/lib/elastic/transport/transport/base.rb:298:in `perform_request'
    from /usr/local/bundle/gems/elastic-transport-8.0.1/lib/elastic/transport/transport/http/faraday.rb:36:in `perform_request'
    from /usr/local/bundle/gems/elastic-transport-8.0.1/lib/elastic/transport/client.rb:176:in `perform_request'
    from /usr/local/bundle/gems/elasticsearch-8.3.0/lib/elasticsearch.rb:161:in `elasticsearch_validation_request'
    from /usr/local/bundle/gems/elasticsearch-8.3.0/lib/elasticsearch.rb:85:in `verify_elasticsearch'
    from /usr/local/bundle/gems/elasticsearch-8.3.0/lib/elasticsearch.rb:70:in `method_missing'
    from /usr/local/bundle/gems/elasticsearch-api-8.3.0/lib/elasticsearch/api/actions/search.rb:99:in `search'
    from test_scripts_1/test_es_connection.rb:14:in `<main>'
/usr/local/lib/ruby/3.1.0/socket.rb:1214:in `__connect_nonblock': Failed to open TCP connection to localhost:9200 (Cannot assign requested address - connect(2) for [::1]:9200) (Errno::EADDRNOTAVAIL)
    from /usr/local/lib/ruby/3.1.0/socket.rb:1214:in `connect_nonblock'
    from /usr/local/lib/ruby/3.1.0/socket.rb:56:in `connect_internal'
    from /usr/local/lib/ruby/3.1.0/socket.rb:137:in `connect'
    from /usr/local/lib/ruby/3.1.0/socket.rb:642:in `block in tcp'
    from /usr/local/lib/ruby/3.1.0/socket.rb:227:in `each'
    from /usr/local/lib/ruby/3.1.0/socket.rb:227:in `foreach'
    from /usr/local/lib/ruby/3.1.0/socket.rb:632:in `tcp'
    from /usr/local/lib/ruby/3.1.0/net/http.rb:998:in `connect'
    from /usr/local/lib/ruby/3.1.0/net/http.rb:976:in `do_start'
    from /usr/local/lib/ruby/3.1.0/net/http.rb:965:in `start'
    from /usr/local/bundle/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:138:in `request_via_get_method'
    from /usr/local/bundle/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:129:in `request_with_wrapped_block'
    from /usr/local/bundle/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:122:in `perform_request'
    from /usr/local/bundle/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:66:in `block in call'
    from /usr/local/bundle/gems/faraday-1.10.0/lib/faraday/adapter.rb:50:in `connection'
    from /usr/local/bundle/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:64:in `call'
    from /usr/local/bundle/gems/faraday-1.10.0/lib/faraday/rack_builder.rb:154:in `build_response'
    from /usr/local/bundle/gems/faraday-1.10.0/lib/faraday/connection.rb:516:in `run_request'
    from /usr/local/bundle/gems/elastic-transport-8.0.1/lib/elastic/transport/transport/http/faraday.rb:49:in `block in perform_request'
    from /usr/local/bundle/gems/elastic-transport-8.0.1/lib/elastic/transport/transport/base.rb:298:in `perform_request'
    from /usr/local/bundle/gems/elastic-transport-8.0.1/lib/elastic/transport/transport/http/faraday.rb:36:in `perform_request'
    from /usr/local/bundle/gems/elastic-transport-8.0.1/lib/elastic/transport/client.rb:176:in `perform_request'
    from /usr/local/bundle/gems/elasticsearch-8.3.0/lib/elasticsearch.rb:161:in `elasticsearch_validation_request'
    from /usr/local/bundle/gems/elasticsearch-8.3.0/lib/elasticsearch.rb:85:in `verify_elasticsearch'
    from /usr/local/bundle/gems/elasticsearch-8.3.0/lib/elasticsearch.rb:70:in `method_missing'
    from /usr/local/bundle/gems/elasticsearch-api-8.3.0/lib/elasticsearch/api/actions/search.rb:99:in `search'
    from test_scripts_1/test_es_connection.rb:14:in `<main>'
/usr/local/lib/ruby/3.1.0/socket.rb:1214:in `__connect_nonblock': Cannot assign requested address - connect(2) for [::1]:9200 (Errno::EADDRNOTAVAIL)
    from /usr/local/lib/ruby/3.1.0/socket.rb:1214:in `connect_nonblock'
    from /usr/local/lib/ruby/3.1.0/socket.rb:56:in `connect_internal'
    from /usr/local/lib/ruby/3.1.0/socket.rb:137:in `connect'
    from /usr/local/lib/ruby/3.1.0/socket.rb:642:in `block in tcp'
    from /usr/local/lib/ruby/3.1.0/socket.rb:227:in `each'
    from /usr/local/lib/ruby/3.1.0/socket.rb:227:in `foreach'
    from /usr/local/lib/ruby/3.1.0/socket.rb:632:in `tcp'
    from /usr/local/lib/ruby/3.1.0/net/http.rb:998:in `connect'
    from /usr/local/lib/ruby/3.1.0/net/http.rb:976:in `do_start'
    from /usr/local/lib/ruby/3.1.0/net/http.rb:965:in `start'
    from /usr/local/bundle/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:138:in `request_via_get_method'
    from /usr/local/bundle/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:129:in `request_with_wrapped_block'
    from /usr/local/bundle/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:122:in `perform_request'
    from /usr/local/bundle/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:66:in `block in call'
    from /usr/local/bundle/gems/faraday-1.10.0/lib/faraday/adapter.rb:50:in `connection'
    from /usr/local/bundle/gems/faraday-net_http-1.0.1/lib/faraday/adapter/net_http.rb:64:in `call'
    from /usr/local/bundle/gems/faraday-1.10.0/lib/faraday/rack_builder.rb:154:in `build_response'
    from /usr/local/bundle/gems/faraday-1.10.0/lib/faraday/connection.rb:516:in `run_request'
    from /usr/local/bundle/gems/elastic-transport-8.0.1/lib/elastic/transport/transport/http/faraday.rb:49:in `block in perform_request'
    from /usr/local/bundle/gems/elastic-transport-8.0.1/lib/elastic/transport/transport/base.rb:298:in `perform_request'
    from /usr/local/bundle/gems/elastic-transport-8.0.1/lib/elastic/transport/transport/http/faraday.rb:36:in `perform_request'
    from /usr/local/bundle/gems/elastic-transport-8.0.1/lib/elastic/transport/client.rb:176:in `perform_request'
    from /usr/local/bundle/gems/elasticsearch-8.3.0/lib/elasticsearch.rb:161:in `elasticsearch_validation_request'
    from /usr/local/bundle/gems/elasticsearch-8.3.0/lib/elasticsearch.rb:85:in `verify_elasticsearch'
    from /usr/local/bundle/gems/elasticsearch-8.3.0/lib/elasticsearch.rb:70:in `method_missing'
    from /usr/local/bundle/gems/elasticsearch-api-8.3.0/lib/elasticsearch/api/actions/search.rb:99:in `search'
    from test_scripts_1/test_es_connection.rb:14:in `<main>'

I tried running the 2nd container by doing docker run -p 9200:9200 and EXPOSE 9200 however I’m still seeing the same error. I’m guessing this is an issue that needs to be addressed on the docker-compose side. Please help clarify why this happens and what is the best way to fix it

When you use localhost in the Ruby application like this, it resolves to the address on the container and not on the host.

To gain access to the host instead, you can use host.docker.internal instead of localhosteg http://host.docker.internal:9200/

See this post for a thorough explanation