# Connection Refused Error while sending metrics from APM-agent \[JAVA\]

**URL:** <https://discuss.elastic.co/t/connection-refused-error-while-sending-metrics-from-apm-agent-java/189860>\
**Category:** APM\
**Tags:** java\
**Created:** [July 10, 2019, 7:52pm UTC](https://discuss.elastic.co/t/connection-refused-error-while-sending-metrics-from-apm-agent-java/189860 "2019-07-10T19:52:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Parth](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@Parth](https://discuss.elastic.co/u/Parth)\
**Post date:** [July 10, 2019, 7:52pm UTC](https://discuss.elastic.co/t/connection-refused-error-while-sending-metrics-from-apm-agent-java/189860/1 "2019-07-10T19:52:00Z")

</div>

I am trying to implement APM stack with existing ElasticSearch & Kibana components.

I had deployed a dockerized APM Server on my [http://localhost:8200](http://localhost:8200) which is successfully talking to Elasticsearch. [I confirmed that it is working from Kibana UI & also by curl-ing it]

I am now trying to deploy a sample application on tomcat [which is a docker image]. My docker container runs successfully on [http://localhost:8080](http://localhost:8080) & my docker file looks like as below:

// FROM tomcat:8.0-alpine  
// LABEL [maintainer="parth.parmar@aeris.net](mailto:maintainer=%22parth.parmar@aeris.net)"  
// ADD sample.war /usr/local/tomcat/webapps/  
// ADD elastic-apm-agent-1.7.0.jar /usr/local/tomcat/  
// ADD setenv.sh /usr/local/tomcat/bin/  
// RUN chmod +x /usr/local/tomcat/bin/setenv.sh  
// EXPOSE 8080  
// CMD ["catalina.sh", "run"]

My setenv.sh file looks like as below:

// export CATALINA\_OPTS="$CATALINA\_OPTS -javaagent:/usr/local/tomcat/elastic-apm-agent-1.7.0.jar"  
// export CATALINA\_OPTS="$CATALINA\_OPTS -Delastic.apm.service\_name=my-apm-agent-test"  
// export CATALINA\_OPTS="$CATALINA\_OPTS -Delastic.apm.application\_packages=org.\*\*\*\*"  
// export CATALINA\_OPTS="$CATALINA\_OPTS -Delastic.apm.server\_urls=http://localhost:8200"

The APM agent seems to have been attached correctly but somehow it is not sending the metrics to APM -server. I am getting the following error:

// ERROR co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Failed to handle event of type METRICS with this error: Connection refused (Connection refused)  
// INFO co.elastic.apm.agent.report.IntakeV2ReportingEventHandler - Backing off for 36 seconds (+/-10%)

I am not sure if I am doing something wrong. Can I please get some help with this?

---

<div class="post-metadata">

**Author:** ![Eyal\_Koren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eyal_koren/32/36830_2.png) [@Eyal\_Koren](https://discuss.elastic.co/u/Eyal_Koren)\
**Post date:** [July 11, 2019, 2:30am UTC](https://discuss.elastic.co/t/connection-refused-error-while-sending-metrics-from-apm-agent-java/189860/2 "2019-07-11T02:30:29Z")

</div>

Hi and welcome to the forum!

Did you verify that the APM server is reachable from within the tomcat container through this URL, eg `curl localhost:8200`?

---

<div class="post-metadata">

**Author:** ![felixbarny](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/felixbarny/32/27341_2.png) [@felixbarny](https://discuss.elastic.co/u/felixbarny)\
**Post date:** [July 11, 2019, 9:22am UTC](https://discuss.elastic.co/t/connection-refused-error-while-sending-metrics-from-apm-agent-java/189860/3 "2019-07-11T09:22:41Z")

</div>

It seems like your application container can't talk to the APM Server container by using `localhost`. Make sure they both are in the same [network](https://docs.docker.com/network/) and to configure the container name as the host name. For example, add the [environment variable](https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file) `ELASTIC_APM_SERVER_URL=http://apm-server:8200`, assuming the container name of the APM Server is `apm-server`.

When using docker, I'd advise to set the configuration via environment variables as opposed to add system properties to the `CATALINA_OPS` environment variable. By doing that, you can use the image with different configuration settings by specifying the environment variables when [starting docker](https://docs.docker.com/engine/reference/commandline/run/#set-environment-variables--e---env---env-file) or by [configuring in a docker-compose.yml file](https://docs.docker.com/compose/environment-variables/).

---

<div class="post-metadata">

**Author:** ![Parth](https://avatars.discourse-cdn.com/v4/letter/p/e56c9b/32.png) [@Parth](https://discuss.elastic.co/u/Parth)\
**Post date:** [July 11, 2019, 3:49pm UTC](https://discuss.elastic.co/t/connection-refused-error-while-sending-metrics-from-apm-agent-java/189860/4 "2019-07-11T15:49:43Z")

</div>

Thanks a lot Eyal & Felix for your quick response. I had later figured out that there was some issue going on with my docker network interface because of which it was not letting apm-agent talk to apm-server. I have now resolved the network interface issue after which it is working fine.

Also, thanks for the recommendation Felix. I would definitely like to go with your idea of replacing system properties with docker environment variable for configuration easiness.

---

<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:** [August 1, 2019, 11:49am UTC](https://discuss.elastic.co/t/connection-refused-error-while-sending-metrics-from-apm-agent-java/189860/5 "2019-08-01T11:49:46Z")

</div>

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