Datadog agent + app stages in same dockerfile

Hi ! Following @Rafal 's suggestion in thread #Recommended way of connecting datadog-agent I tried running a datadog agent and an app in the same Dockerfile (two stages since we have FROM datadog/agent:7 for the agent and FROM node:16.6.1-alpine3.14 for the app). Locally it works perfectly fine, both stages are working. But on railway, only the second one is ran : the app one. The datadog agent stage is discarded, any idea why ? Here is the dockerfile :
FROM datadog/agent:7

ENV DD_APM_ENABLED=true
ENV NON_LOCAL_TRAFFIC=true
ENV DD_APM_NON_LOCAL_TRAFFIC=true
ENV DD_DOGSTATSD_NON_LOCAL_TRAFFIC=true
ENV DD_HOSTNAME=test
ENV DD_TRACE_STARTUP_LOGS=true
ENV DD_SITE=datadoghq.eu

ARG DD_API_KEY
ENV DD_API_KEY=$DD_API_KEY

FROM node:16.6.1-alpine3.14

# Copy your application files to the Docker container
COPY . /app
WORKDIR /app

# Install your application's dependencies
RUN npm install

# Set the Docker container to run your application when it starts
CMD ["node", "index.js"]

EXPOSE 3000
FROM datadog/agent:7

ENV DD_APM_ENABLED=true
ENV NON_LOCAL_TRAFFIC=true
ENV DD_APM_NON_LOCAL_TRAFFIC=true
ENV DD_DOGSTATSD_NON_LOCAL_TRAFFIC=true
ENV DD_HOSTNAME=test
ENV DD_TRACE_STARTUP_LOGS=true
ENV DD_SITE=datadoghq.eu

ARG DD_API_KEY
ENV DD_API_KEY=$DD_API_KEY

FROM node:16.6.1-alpine3.14

# Copy your application files to the Docker container
COPY . /app
WORKDIR /app

# Install your application's dependencies
RUN npm install

# Set the Docker container to run your application when it starts
CMD ["node", "index.js"]

EXPOSE 3000
Solution:
try this again but use service datadog-agent start instead of the systemctl thing
Jump to solution
56 Replies
Percy
Percyβ€’2y ago
Project ID: 6bd9c9dd-822c-457b-a37e-0abf3c4579fb
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
6bd9c9dd-822c-457b-a37e-0abf3c4579fb
luna
lunaβ€’2y ago
Doesnt using FROM start a fresh layer without anything from the one before? πŸ€” From the docker docs.
With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image. To show how this works, you can adapt the Dockerfile from the previous section to use multi-stage builds.
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
Yes I think so, and that's not a problem. I tried this locally and it worked with this exact dockerfile. But it seems like on railway the first stage is simply not launched, whereas locally it is.
luna
lunaβ€’2y ago
Are you sure on that? Ive never seen a dockerfile act the way you're describing.
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
I think you're right, I might have misunderstood the problem
Brody
Brodyβ€’2y ago
im with luna on this, anything that you install or launch in the previous FROM derivative is not carried over to the next FROM unless you explicitly copy it over
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
Ok good thing we got that out of the way, thank you. I removed the app's stage and only kept the datadog one here is the docker file :
FROM datadog/agent:7

ENV DD_APM_ENABLED=true
ENV NON_LOCAL_TRAFFIC=true
ENV DD_APM_NON_LOCAL_TRAFFIC=true
ENV DD_DOGSTATSD_NON_LOCAL_TRAFFIC=true
ENV DD_HOSTNAME=test-mother-production.up.railway.app
ENV DD_LOGS_ENABLED=true

ARG DD_API_KEY
FROM datadog/agent:7

ENV DD_APM_ENABLED=true
ENV NON_LOCAL_TRAFFIC=true
ENV DD_APM_NON_LOCAL_TRAFFIC=true
ENV DD_DOGSTATSD_NON_LOCAL_TRAFFIC=true
ENV DD_HOSTNAME=test-mother-production.up.railway.app
ENV DD_LOGS_ENABLED=true

ARG DD_API_KEY
The agent is running fine but I don't see any activity of the container in my datadog dashboard, why is that ? When running this exact one locally I see the container on my dashboard.
luna
lunaβ€’2y ago
When running it locally are you passing in the docker socket by chance?
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
What do you mean ?
luna
lunaβ€’2y ago
Whatre you actually trying todo? I have a feeling you may not need the agent at all for this.
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
So I have this app :
const tracer = require("dd-trace").init();
const express = require("express");
const app = express();

app.get("/", (req, res) => {
res.send("Hello World!");
});

app.listen(3000, () => {
console.log("Example app listening on port 3000!");
});
const tracer = require("dd-trace").init();
const express = require("express");
const app = express();

app.get("/", (req, res) => {
res.send("Hello World!");
});

app.listen(3000, () => {
console.log("Example app listening on port 3000!");
});
And the docker agent container running on my computer. This lets me monitor the app. Now I'm only trying to run the container on railway without the app to see if I see it on the datadog dashboard
luna
lunaβ€’2y ago
I see
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
Just like I see it when i simply run the container locally
luna
lunaβ€’2y ago
Okay so you will need the agent just inside the container with your node app. Or πŸ€” https://railway.app/template/78cMS9
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
theoretically this is what I want The thing is before doing that, I'm trying to understand why does the container running right now on railway isn't detected by my dashboard I have an idea, is it possible to edit the docker run command used to run the container on railway ?
Brody
Brodyβ€’2y ago
no
luna
lunaβ€’2y ago
You might need to use their alpine container and then install node on that.
Brody
Brodyβ€’2y ago
or install the agent in the node image with data dog's install script
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
Local
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
Railway
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
I see some differences in the logs do you have any idea why that's happening ? Could you give me more details about how to do that ?
Brody
Brodyβ€’2y ago
UNTESTED think of this as a pseudo dockerfile
FROM node:16.6.1

ENV DD_INSTALL_ONLY=1
ENV DD_SITE=datadoghq.com

RUN bash -c "$(curl -L https://s3.amazonaws.com/dd-agent/scripts/install_script_agent7.sh)"

WORKDIR /app

# Copy your application files to the Docker container
COPY . .

# Install your application's dependencies
RUN npm install

# Set the Docker container to run your application when it starts
CMD systemctl restart datadog-agent.service && node index.js
FROM node:16.6.1

ENV DD_INSTALL_ONLY=1
ENV DD_SITE=datadoghq.com

RUN bash -c "$(curl -L https://s3.amazonaws.com/dd-agent/scripts/install_script_agent7.sh)"

WORKDIR /app

# Copy your application files to the Docker container
COPY . .

# Install your application's dependencies
RUN npm install

# Set the Docker container to run your application when it starts
CMD systemctl restart datadog-agent.service && node index.js
luna
lunaβ€’2y ago
GitHub
GitHub - gdraheim/docker-systemctl-replacement: docker systemctl re...
docker systemctl replacement - allows to deploy to systemd-controlled containers without starting an actual systemd daemon (e.g. centos7, ubuntu16) - GitHub - gdraheim/docker-systemctl-replacement...
Brody
Brodyβ€’2y ago
🀷
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
That's clever let me try that thanks
Brody
Brodyβ€’2y ago
it likely wont work
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
due to that ?
Brody
Brodyβ€’2y ago
I don't know what that link is complaining about have you set your DD_API_KEY in the service variables
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
Yes I did "/bin/sh: 1: systemctl: not found" :/
Brody
Brodyβ€’2y ago
well luna did warn us lol and i did say it would likely not work
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
I'mma try her approach So the app runs : Example app listening on port 3000! But the agent doesn't (at least I don't have anything on the DD dashboard). i'm in local is there a way to see the logs of the agent ?
FROM node:16.6.1

ENV DD_INSTALL_ONLY=1
ENV DD_APM_ENABLED=true
ENV NON_LOCAL_TRAFFIC=true
ENV DD_APM_NON_LOCAL_TRAFFIC=true
ENV DD_DOGSTATSD_NON_LOCAL_TRAFFIC=true
ENV DD_HOSTNAME=test-mother-production.up.railway.app
ENV DD_LOGS_ENABLED=true

ARG DD_API_KEY
ENV DD_API_KEY=${DD_API_KEY}

ARG DD_SITE
ENV DD_SITE=${DD_SITE}

# Download the systemctl replacement script
RUN apt-get update && apt-get install -y wget python && rm -rf /var/lib/apt/lists/*
RUN wget https://raw.githubusercontent.com/gdraheim/docker-systemctl-replacement/master/files/docker/systemctl.py -O /usr/bin/systemctl
RUN chmod +x /usr/bin/systemctl

RUN bash -c "$(curl -L https://s3.amazonaws.com/dd-agent/scripts/install_script_agent7.sh)"

WORKDIR /app

# Copy your application files to the Docker container
COPY . .

# Install your application's dependencies
RUN npm install

# Set the Docker container to run your application when it starts
CMD systemctl restart datadog-agent.service && node index.js

EXPOSE 3000
FROM node:16.6.1

ENV DD_INSTALL_ONLY=1
ENV DD_APM_ENABLED=true
ENV NON_LOCAL_TRAFFIC=true
ENV DD_APM_NON_LOCAL_TRAFFIC=true
ENV DD_DOGSTATSD_NON_LOCAL_TRAFFIC=true
ENV DD_HOSTNAME=test-mother-production.up.railway.app
ENV DD_LOGS_ENABLED=true

ARG DD_API_KEY
ENV DD_API_KEY=${DD_API_KEY}

ARG DD_SITE
ENV DD_SITE=${DD_SITE}

# Download the systemctl replacement script
RUN apt-get update && apt-get install -y wget python && rm -rf /var/lib/apt/lists/*
RUN wget https://raw.githubusercontent.com/gdraheim/docker-systemctl-replacement/master/files/docker/systemctl.py -O /usr/bin/systemctl
RUN chmod +x /usr/bin/systemctl

RUN bash -c "$(curl -L https://s3.amazonaws.com/dd-agent/scripts/install_script_agent7.sh)"

WORKDIR /app

# Copy your application files to the Docker container
COPY . .

# Install your application's dependencies
RUN npm install

# Set the Docker container to run your application when it starts
CMD systemctl restart datadog-agent.service && node index.js

EXPOSE 3000
here is the dockerfile
Brody
Brodyβ€’2y ago
what do the deployment logs look like
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
Brody
Brodyβ€’2y ago
the railway deployment logs
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
I didn't deploy on railway Let me deploy that config
Brody
Brodyβ€’2y ago
where did you deploy it
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
that's local the container on my pc
Brody
Brodyβ€’2y ago
well get to deploying lol
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
Not surprising
Brody
Brodyβ€’2y ago
doesn't look like the agent was started
Solution
Brody
Brodyβ€’2y ago
try this again but use service datadog-agent start instead of the systemctl thing
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
i think it's working https://test-mother-production.up.railway.app/ but can't access the app url
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
+ it worked locally
Brody
Brodyβ€’2y ago
is it working because you used service datadog-agent start
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
Yes I think so Thanks for that + it simplifies the dockerfile
Brody
Brodyβ€’2y ago
what web framework are you using
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
express js
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
It works !
Brody
Brodyβ€’2y ago
does datadog work too? i know the agent starts, but do logs show in the datadog site
Anthony πŸ¦…
Anthony πŸ¦…OPβ€’2y ago
Yes i have the logs too Thanks a lot @Brody @ImLunaHey
Brody
Brodyβ€’2y ago
no problem!
Want results from more Discord servers?
Add your server