# APM-Server not connecting to my application

**URL:** <https://discuss.elastic.co/t/apm-server-not-connecting-to-my-application/281475>\
**Category:** APM\
**Tags:** docker, nodejs, server\
**Created:** [August 15, 2021, 7:43am UTC](https://discuss.elastic.co/t/apm-server-not-connecting-to-my-application/281475 "2021-08-15T07:43:55Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Piyush\_Mittal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/piyush_mittal/32/92656_2.png) [@Piyush\_Mittal](https://discuss.elastic.co/u/Piyush_Mittal)\
**Post date:** [August 15, 2021, 7:43am UTC](https://discuss.elastic.co/t/apm-server-not-connecting-to-my-application/281475/1 "2021-08-15T07:43:55Z")

</div>

Hey, I just started using APM to monitor my application. I done successful setup of APM-server along with my nodejs apm-agent on my local machine. But, When I go to setup the same for production, my APM-server is not connecting to my application.

I created a simple nodejs application and create it's docker image and run the docker container in my AWS EC2 instance and then install Elasticsearch, Kibana and APM-Server on my instance and everything is working fine and I am also successfully entered into my dashboard but When I try to launch APM, It's not launching it. And when I check, I found my apm-server is running successfully on my ec2 and my application also but they are not connecting with each other. I don't know why this happen on production. Beacuse same thing is working fine on my machine. So, Please tell me How I can solve this issue??

If you guy's need any info regarding configuration, please tell. I will share here.

**I am attaching my nodejs code here, pls tell if u need anything else to solve the issue.**

```auto
  serviceName: "nodeAPMSerivce",
  serverUrl: "http://localhost:8200",
  metricsInterval: "0s",
});

const express = require("express");
const connectDB = require("./config/db");
const dotenv = require("dotenv");
const User = require("./models/userModel");

dotenv.config();

const app = express();

connectDB();

app.use(express.json());

app.get("/", (req, res) => {
  res.send("Hey, I'm here!!");
});

app.get("/users", async (req, res) => {
  try {
    const users = await User.find({});

    res.status(200).json({
      users,
    });
  } catch (err) {
    console.log(err);
  }
});

app.post("/user", async (req, res) => {
  try {
    const savedUser = await User.create({ ...req.body });

    res.status(201).json({
      savedUser,
    });
  } catch (err) {
    console.log(err);
  }
});

app.listen(8080, () => {
  console.log("Hello I'm listening on PORT 8080");
});

```

Thanks

---

<div class="post-metadata">

**Author:** ![simitt](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simitt/32/106406_2.png) [@simitt](https://discuss.elastic.co/u/simitt)\
**Post date:** [August 16, 2021, 6:26am UTC](https://discuss.elastic.co/t/apm-server-not-connecting-to-my-application/281475/2 "2021-08-16T06:26:48Z")

</div>

Your configuration shows `serverUrl: "http://localhost:8200"`, which only works as long as your APM instrumented app and the APM Server run alongside on the same host. Please ensure that the configured APM Server URL is in fact reachable from within your app. The agent logs should provide more information about whether or not the server can be reached.

I suggest to also consider setting up an [`auth.secret_token`](https://www.elastic.co/guide/en/apm/server/current/configuration-process.html#config-secret-token) for the APM Server and [configure it within the Node.js app](https://www.elastic.co/guide/en/apm/agent/nodejs/3.x/configuration.html#secret-token).

---

<div class="post-metadata">

**Author:** ![Piyush\_Mittal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/piyush_mittal/32/92656_2.png) [@Piyush\_Mittal](https://discuss.elastic.co/u/Piyush_Mittal)\
**Post date:** [August 18, 2021, 7:31pm UTC](https://discuss.elastic.co/t/apm-server-not-connecting-to-my-application/281475/3 "2021-08-18T19:31:40Z")

</div>

@simitt I checked in my apm-server.yml file host is localhost:8200 and my apm-server is also running.

**As you can see in screenshot that APM server is running** :

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/a/3/a3dfec79887d31fba634fc210f0ee541774390c4.png)

The issues which I am facing is:

- My app which is running on same instance unable to connect to my apm-server:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/6/a/6a4ba8d2d14d7f2461914177730e732bc0118d2a.png)

- In my apm-server.yml If I uncomment my elasticsearch host then my apm-server running failed.

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/2/8/2843cc1a2b883a963e6bc548633123362a82a28b.png)

Can you Please tell me what is the issue here and How I can solve this. Please tell.

Thanks.

---

<div class="post-metadata">

**Author:** ![Piyush\_Mittal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/piyush_mittal/32/92656_2.png) [@Piyush\_Mittal](https://discuss.elastic.co/u/Piyush_Mittal)\
**Post date:** [August 19, 2021, 4:37pm UTC](https://discuss.elastic.co/t/apm-server-not-connecting-to-my-application/281475/4 "2021-08-19T16:37:33Z")

</div>

@axw and @trentm Please tell a solution of this issue, please help me to to came out of this issue.

---

<div class="post-metadata">

**Author:** ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)\
**Post date:** [August 19, 2021, 5:58pm UTC](https://discuss.elastic.co/t/apm-server-not-connecting-to-my-application/281475/5 "2021-08-19T17:58:46Z")

</div>

> [@Piyush\_Mittal](#):
>
> My app which is running on same instance unable to connect to my apm-server

@Piyush_Mittal Your app and apm-server may be running on the same EC2 instance, but if they are running in separate docker containers, then they have separate networks for "localhost". Often (if using docker "bridge" networking) docker containers will be run with `-p 8200` or similar to expose ports from the container to the docker host ([Docker run reference | Docker Docs](https://docs.docker.com/engine/reference/run/#expose-incoming-ports)). If your app is also running in a Docker container on that same EC2 host, then you'll want to setup a docker network ([docker network | Docker Docs](https://docs.docker.com/engine/reference/commandline/network/)) all your containers can share to talk with each other.

See [Docker run reference | Docker Docs](https://docs.docker.com/engine/reference/run/#network-settings).

As @simitt mentioned above, please confirm _from where your app is running_ (e.g. if it is in a separate container), that it can reach apm-server.

---

<div class="post-metadata">

**Author:** ![Piyush\_Mittal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/piyush_mittal/32/92656_2.png) [@Piyush\_Mittal](https://discuss.elastic.co/u/Piyush_Mittal)\
**Post date:** [August 20, 2021, 7:08am UTC](https://discuss.elastic.co/t/apm-server-not-connecting-to-my-application/281475/6 "2021-08-20T07:08:09Z")

</div>

@trentm I have one EC2 instance on which my docker container and apm-server, elasticsearch and kibana is running.

The structure is like:-

1. One docker container on which my nodejs application is running.

2. My apm-server, elastic search and kibana is running on my ec2 machine not in any docker container.

SO, I need to setup any network bridge or not ??

Thanks.

---

<div class="post-metadata">

**Author:** ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)\
**Post date:** [August 20, 2021, 4:09pm UTC](https://discuss.elastic.co/t/apm-server-not-connecting-to-my-application/281475/7 "2021-08-20T16:09:01Z")

</div>

@Piyush_Mittal You could try using "host" networking for your application's container by starting it with `docker run --network=host ...`. See [Use host networking | Docker Documentation](https://docs.docker.com/network/host/)

---

<div class="post-metadata">

**Author:** ![Piyush\_Mittal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/piyush_mittal/32/92656_2.png) [@Piyush\_Mittal](https://discuss.elastic.co/u/Piyush_Mittal)\
**Post date:** [August 20, 2021, 7:47pm UTC](https://discuss.elastic.co/t/apm-server-not-connecting-to-my-application/281475/8 "2021-08-20T19:47:14Z")

</div>

@trentm Thanks for your help, I will definitely try your solution and If I got any error I will get back here.

---

<div class="post-metadata">

**Author:** ![Piyush\_Mittal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/piyush_mittal/32/92656_2.png) [@Piyush\_Mittal](https://discuss.elastic.co/u/Piyush_Mittal)\
**Post date:** [August 24, 2021, 7:41pm UTC](https://discuss.elastic.co/t/apm-server-not-connecting-to-my-application/281475/9 "2021-08-24T19:41:10Z")

</div>

@trentm I exposed 8200 post in my docker file and then run this command **sudo docker run -d -p 8080:8080 -p 8200:8200 --name elkContainer piyush2002/elkapplication:latest** and got his error:-

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/9/1/91f2cf62b64c819c06be9e6e402e204afdf841f5.png)

And I also used this command **sudo docker run -d --network=host --name elkContainer piyush2002/elkapplication:latest** but my apm agent not started as I checked logs of my application.

Please tell what else I can do.

---

<div class="post-metadata">

**Author:** ![Piyush\_Mittal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/piyush_mittal/32/92656_2.png) [@Piyush\_Mittal](https://discuss.elastic.co/u/Piyush_Mittal)\
**Post date:** [August 24, 2021, 7:44pm UTC](https://discuss.elastic.co/t/apm-server-not-connecting-to-my-application/281475/10 "2021-08-24T19:44:51Z")

</div>

@trentm I am also sharing my DockerFile:-

```auto
FROM node:alpine

WORKDIR /usr/app

COPY package.json .

RUN npm install

COPY . .

EXPOSE 8080

EXPOSE 8200

CMD ["npm", "run", "start"]

```

Please tell if any changes needed.

---

<div class="post-metadata">

**Author:** ![Piyush\_Mittal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/piyush_mittal/32/92656_2.png) [@Piyush\_Mittal](https://discuss.elastic.co/u/Piyush_Mittal)\
**Post date:** [August 25, 2021, 4:15pm UTC](https://discuss.elastic.co/t/apm-server-not-connecting-to-my-application/281475/11 "2021-08-25T16:15:14Z")

</div>

Hey, @trentm Pleaseeee try to reply as soon as possible.

---

<div class="post-metadata">

**Author:** ![nugusbayevkk](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nugusbayevkk/32/126683_2.png) [@nugusbayevkk](https://discuss.elastic.co/u/nugusbayevkk)\
**Post date:** [August 25, 2021, 4:39pm UTC](https://discuss.elastic.co/t/apm-server-not-connecting-to-my-application/281475/12 "2021-08-25T16:39:17Z")

</div>

Hi @Piyush_Mittal , why are you trying expose 8200 port? If you look at error message, this port is already in use.  
How did you run apm-server, elasticsearch?

---

<div class="post-metadata">

**Author:** ![trentm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/trentm/32/77647_2.png) [@trentm](https://discuss.elastic.co/u/trentm)\
**Post date:** [August 25, 2021, 5:16pm UTC](https://discuss.elastic.co/t/apm-server-not-connecting-to-my-application/281475/13 "2021-08-25T17:16:29Z")

</div>

@Piyush_Mittal As @nugusbayevkk has mentioned, you don't want to "EXPOSE 8200" from your application's Dockerfile, because port 8200 is the port used by _apm-server_.

---

<div class="post-metadata">

**Author:** ![Piyush\_Mittal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/piyush_mittal/32/92656_2.png) [@Piyush\_Mittal](https://discuss.elastic.co/u/Piyush_Mittal)\
**Post date:** [August 28, 2021, 8:08pm UTC](https://discuss.elastic.co/t/apm-server-not-connecting-to-my-application/281475/14 "2021-08-28T20:08:56Z")

</div>

@trentm and @nugusbayevkk Initially, I just trying it with not exposing the port then it's not working and I also use `--network=host` as suggested by @trentm but if I use this then my apm-agent is not starting in my application. And I also clone my application on my ec2 machine and run it and it still no connecting to apm-server. This thing not happened with me when I run my application locally and also my apm-server on my PC. I don't know like What's wrong going in this. Please help me out like if you want to me to share some more details about this stuff , I am more than happy to share with you but please help me to figure this out.

---

<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:** [September 18, 2021, 4:09pm UTC](https://discuss.elastic.co/t/apm-server-not-connecting-to-my-application/281475/15 "2021-09-18T16:09:05Z")

</div>

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