# Java APM agent can't connect to APM server

**URL:** <https://discuss.elastic.co/t/java-apm-agent-cant-connect-to-apm-server/315087>\
**Category:** APM\
**Tags:** docker, java\
**Created:** [September 24, 2022, 7:48pm UTC](https://discuss.elastic.co/t/java-apm-agent-cant-connect-to-apm-server/315087 "2022-09-24T19:48:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![majarianil](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@majarianil](https://discuss.elastic.co/u/majarianil)\
**Post date:** [September 24, 2022, 7:48pm UTC](https://discuss.elastic.co/t/java-apm-agent-cant-connect-to-apm-server/315087/1 "2022-09-24T19:48:07Z")

</div>

**Kibana version** : 7.12.0

**Elasticsearch version** : 7.12.0

**APM Server version** : 7.12.0

**APM Agent language and version** : Java - openjdk:17.0.2

**Browser version** : Google chrome - 05.0.5195.127

**Original install method (e.g. download page, yum, deb, from source, etc.) and version**: Docker image

**Fresh install or upgraded from other version?** Fresh

**Description of the problem including expected versus actual behavior. Please include screenshots (if relevant)**: I have setup Elasticsearch locally and configuring my sample "app" to APM server through apm-java-agent. But i'm getting this error "Elastic APM server [http://localhost:8200/](http://localhost:8200/) is not available (Connection refused)". And i have looked into the elasticsearch troubleshooting docs but not able to solve this issue. Could anyone has idea about this issue!!!

**Provide logs and/or server output (if relevant)**:

```auto
 [main] INFO co.elastic.apm.agent.configuration.StartupInfo - service_name: 'helloworld-service' (source: Java System Properties)  
 [main] INFO co.elastic.apm.agent.configuration.StartupInfo - server_urls: 'http://localhost:8200' (source: Java System Properties)
 [main] INFO co.elastic.apm.agent.configuration.StartupInfo - application_packages: 'com.example' (source: Java System Properties) 
 [elastic-apm-server-healthcheck] WARN co.elastic.apm.agent.report.ApmServerHealthChecker Elastic APM server http://localhost:8200/ is not available (Connection refused)
 [main] INFO co.elastic.apm.agent.impl.ElasticApmTracer - Tracer switched to RUNNING state 
 [elastic-apm-remote-config-poller] ERROR co.elastic.apm.agent.configuration.ApmServerConfigurationSource - Connection refused

```

**docker-compose.yml:**

```auto
version: "3.7"
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.12.0
    container_name: elasticsearch
    restart: always
    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-volume:/usr/share/elasticsearch/data
    ports:
      - "9200:9200"
  kibana:
    container_name: kibana
    image: docker.elastic.co/kibana/kibana:7.12.0
    restart: always
    environment:
      SERVER_NAME: kibana
      ELASTICSEARCH_HOSTS: http://elasticsearch:9200
    ports:
      - "5601:5601"
    depends_on:
      - elasticsearch
  apm-server:
    image: docker.elastic.co/apm/apm-server:7.12.0
    container_name: apm-server
    restart: always
    cap_add: ["CHOWN", "DAC_OVERRIDE", "SETGID", "SETUID"]
    cap_drop: ["ALL"]
    command: >
      apm-server -e
        -E apm-server.rum.enabled=true
        -E setup.apm-server.host=0.0.0.0:8200
        -E setup.kibana.host=kibana:5601
        -E setup.template.settings.index.number_of_replicas=0
        -E apm-server.kibana.enabled=true
        -E apm-server.kibana.host=kibana:5601
        -E output.elasticsearch.hosts=["elasticsearch:9200"]
    environment:
      SERVER_NAME: apm-server
      ELASTICSEARCH_HOSTS: http://elasticsearch:9200
    ports:
      - "8200:8200"
    depends_on:
      - elasticsearch
      - kibana

volumes:
  elasticsearch-data-volume:
    driver: local

```

**Dockerfile (for app which I'm using apm agent with):**

```auto
FROM openjdk:17.0.2

WORKDIR /app

COPY ./target/*.jar /app/

COPY --from=docker.elastic.co/observability/apm-agent-java:1.34.0 /usr/agent/elastic-apm-agent.jar /tmp/elastic-apm-agent.jar

EXPOSE 8080

#set the startup command to execute the jar
ENTRYPOINT ["/bin/sh", "-c", "java -javaagent:/tmp/elastic-apm-agent.jar -Delastic.apm.service_name=helloworld-service -Delastic.apm.application_packages=com.example -Delastic.apm.server_urls=http://localhost:8200 -jar /app/helloworld-*.jar"]
#INFO: i have also tried with the apm_server_url=http://apm-server:8200 where, apm-server is my apm server container name. But gettitng same error

```

---

<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:** [September 25, 2022, 1:02am UTC](https://discuss.elastic.co/t/java-apm-agent-cant-connect-to-apm-server/315087/2 "2022-09-25T01:02:50Z")

</div>

I think it is because your hello world app is running in a separate docker container ... in that container `localhost` does not point to the `localhost` of the container that the apm server is running in. This is a docker networking issue.... remember every container is its own "host"

I suspect if you just ran your hello world from the actual host command line outside of docker it would probably work

And or read something like [this](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach)

and change to something like this...

```auto
ENTRYPOINT ["/bin/sh", "-c", "java -javaagent:/tmp/elastic-apm-agent.jar -Delastic.apm.service_name=helloworld-service -Delastic.apm.application_packages=com.example -Delastic.apm.server_urls=http://host.docker.internal:8200 -jar /app/helloworld-*.jar"]

```

---

<div class="post-metadata">

**Author:** ![majarianil](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@majarianil](https://discuss.elastic.co/u/majarianil)\
**Post date:** [September 25, 2022, 7:14am UTC](https://discuss.elastic.co/t/java-apm-agent-cant-connect-to-apm-server/315087/3 "2022-09-25T07:14:44Z")

</div>

yes, you are right it is because I'm running the app in a seperate container. Now i have changed the apm server url to `(http://host.docker.internal:8200)` and it is working.

Thank you for the suggestions and the solution

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [October 16, 2022, 3:15am UTC](https://discuss.elastic.co/t/java-apm-agent-cant-connect-to-apm-server/315087/4 "2022-10-16T03:15:37Z")

</div>

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.
