Automatic way to pick up the hostname inside docker container - node.js

We're running NodeJS application inside docker container hosted on Amazon EC2 instance. To
To enable Monitoring for Node.js app with Datadog we are using datadog-metrics library and integrate it with our application. We basically require to save the below Javascript code into a file called example_app.js
var metrics = require('datadog-metrics');
metrics.init({ **host: 'myhost', prefix: 'myapp.'** });
function collectMemoryStats() {
var memUsage = process.memoryUsage();
metrics.gauge('memory.rss', memUsage.rss);
metrics.gauge('memory.heapTotal', memUsage.heapTotal);
metrics.gauge('memory.heapUsed', memUsage.heapUsed);
metrics.increment('memory.statsReported');
}
setInterval(collectMemoryStats, 5000);
Although, we are able to successfully publish metrics to datadog but we're wondering if this can be automated. We want build this into our docker image, hence require an automatic way to pick up the hostname, at the very least be able to use the docker hosts name if possible..Because till now we're manually specifying "myhost" and "myapp" values manually. Any better way to fetch the AWS instance hostname value into %myhost?

Why not try?
var os = require(“os”);
var hostname = os.hostname();
It will return the docker container's hostname. If you haven't set a hostname explicitly, using something like docker run -h hostname image command then it will return the docker host's hostname.
Alternatively, you could do this using a deployment tool like puppet, ansible, etc. and template the file when you deploy the container.

Related

Feathers.js Docker

I am currently trying to wire up Docker and Feathers.js. I have followed the tutorial showcased from their site: https://docs.feathersjs.com/cookbook/deploy/docker.html#create-an-app
I can't seem to figure out why I am not able to connect to it outside of the container. You can see below that the container is indeed running, but when I try to hit any endpoint I am getting a "There was an error connecting to http://localhost:3030/users". You can view my Dockerfile below in the image (basically a copy from the Feathers site). End goal is to set this up with React, MongoDB and FeathersJS. React/Mongo are working well, but I can't get a connection to register for feathers either internally within docker containers, or through localhost as shown below.
As mentioned in this issue a previous version of the CLI was generating applications that are binding to the wrong hostname. Make sure that you are using the latest version of #feathersjs/cli and your src/index.js is using no hostname and looks like this:
const server = app.listen(port);

How to run bash script in an already created/exisitng VM in GCP using NodeJS?

I have gone through, Nodejs-GCP-Compute-Github doc and used the sample code to create a new VM and list existing VM using NodeJS and Npm module.
Now I want to connect to my existing VM and run a small bash script to invoke a few commands mostly git clone or curl to run files in VM.
I couldn't find a method in #google-cloud/compute to connect to the exisitng VMs and do some stuff.
Do we have any such method?
Is it possible to do this in some other way using Nodejs?
There are two different methods coming to my mind:
You could add your public key to the instance, and then connect to it via ssh using a node ssh library (https://cloud.google.com/compute/docs/instances/adding-removing-ssh-keys)
Set a startup script for the instance when you are creating it. This can be done by setting the second parameter (config) of createVM to something like:
{
os: 'ubunntu',
metadata: {
'startup-script': 'your commands'
}
}

Using System environment variables in node / react with docker

I have a react app (CRA) it will be running inside a docker container for which I will set an environment variable "API_KEY" = "some_value". For the example of windows this is System Properties -> Environment Variables -> System Variables -> Variable = "API_KEY" and Value = "some_value".
I would like to access to get this variable into the application at run time. This is for the API_KEY for Azure App Insights.
https://learn.microsoft.com/en-us/azure/application-insights/app-insights-javascript#add-the-sdk-script-to-your-app-or-web-pages
The above link shows the <your instrumentation key>. I will be deploying the same application to multiple environments. Each instance of the application will need to use its specific App Insights. So the goal here is to specify that API_KEY in the environment variables which will be different for each of the docker containers.
Please note I am aware of nodeJs and process.env.API_KEY but this does not read from the systems environment variables. Is there a way to get the system variable to link up to the process.env for the node instance?
-PS this request is to an API service that will need to fire straight away. So making an API request to get it is out of the question. It will be exposed to the end client as it is logging JavaScript events for each client.
The answer is NO. You can not read system environment variables. You can however set process environment variables in a *.env file. In my case I will generate this env file via a shell script which will be triggered by the Dockerfile. This will then scrap the process for it's environment variables that start with "REACT_APP_#".
Hope this helps others.
Generating shell script
https://medium.com/#vietgoeswest/passing-env-variables-from-the-server-to-your-create-react-app-c87578a45358
Setting up env with CRA
https://medium.com/#tacomanator/environments-with-create-react-app-7b645312c09d
Dockerizing a Node.js web app
https://nodejs.org/en/docs/guides/nodejs-docker-webapp/
Passing environment variables from Docker to Node https://medium.com/#felipedutratine/pass-environment-variables-from-docker-to-my-nodejs-or-golang-app-a1f2ddec31f5
Also helpful
https://github.com/facebook/create-react-app/issues/982

Docker environment variables, dokku-redis

Using redis as my session store in my express.js app. I'm having problems. Narrowed them down to a connection issue.
How do I access a docker environment variable from within an express.js app? I'm using dokku-redis.
It reports that environment variables are automatically set up on the linked app... I've linked my app. running dokku redis:info foo shows that all is linked. I'm trying to pull in REDIS_URL
Thanks, Rob
https://github.com/dokku/dokku-redis
------------
a redis service can be linked to a
container this will use native docker
links via the docker-options plugin
here we link it to our 'playground' app
NOTE: this will restart your app
dokku redis:link lolipop playground
the following environment variables will be set automatically by docker (not on the app itself, so they won’t be listed when calling dokku config)
DOKKU_REDIS_LOLIPOP_NAME=/lolipop/DATABASE
DOKKU_REDIS_LOLIPOP_PORT=tcp://172.17.0.1:6379
DOKKU_REDIS_LOLIPOP_PORT_6379_TCP=tcp://172.17.0.1:6379
DOKKU_REDIS_LOLIPOP_PORT_6379_TCP_PROTO=tcp
DOKKU_REDIS_LOLIPOP_PORT_6379_TCP_PORT=6379
DOKKU_REDIS_LOLIPOP_PORT_6379_TCP_ADDR=172.17.0.1
and the following will be set on the linked application by default
REDIS_URL=redis://lolipop:SOME_PASSWORD#dokku-redis-lolipop:6379
NOTE: the host exposed here only works internally in docker containers. If
you want your container to be reachable from outside, you should use 'expose'.
------------------------------
Edit - sorry, I forgot to add that I have tried process.env
How did you link the redis service with your application? You cannot simply do dokku config:set, and if you did, you should unset it and then use dokku redis:link instead. Once you do that, rebuild your app using dokku ps:rebuild APP and you should get process.env.REDIS_URL set.
you can access environment variables through process.env like the following:
console.log(process.env["DOKKU_REDIS_LOLIPOP_NAME"]);
console.log(process.env["DOKKU_REDIS_LOLIPOP_PORT"]);
console.log(process.env["REDIS_URL"]);
also as long as keys in objects do not harm variable naming conventions you can access them like this too:
console.log(process.env.REDIS_URL);
more info in regards to javascript variable naming rules: What characters are valid for JavaScript variable names?

Azure Elasticsearch and NEST can't add index

In Azure I've created an Elasticsearch and Kibana cluster based on the (VM)template. In my unit test I use the Elasticsearch NEST nuget package to access my azure Electricsearch. A Ping just works fine
var node = new Uri("http://x:5601");
var settings = new ConnectionSettings(node);
var client = new ElasticClient(settings);
var response = client.Ping(new PingRequest());
Assert.IsTrue(response.IsValid);
But when I try to add an index I always get the error "Request must contain an kbn-xsrf header"
I have tried many things and tried to read as many examples but with no succes. Things I would like to know
Which nuget version should be used with the created VM. I figured out the azure enviroment runs ES 1 so I should use the nuget package
1.82
How should I authenticate in my code? I've found SetBasicAuthentication, still this doesn't seem to work beter
How to set or work with kbn-xsrf
Btw my index creating unit test looks like
var node = new Uri("http://x:5601");
var settings = new ConnectionSettings(node);
settings.SetBasicAuthentication("x", "x");
var client = new ElasticClient(settings);
var response = client.CreateIndex("hotelindex");
Assert.IsTrue(response.IsValid);
The Elastic ARM template deploys with either an internal load balancer or an external load balancer (which also deploys an internal load balancer for the following reason).
Kibana communicates with the cluster through the internal load balancer and looking at your Uri, it looks like you're sending the request to the Kibana endpoint. If you need to access the cluster through the REST API (directly or through a client), you also need to deploy an external load balancer as well.
Please note that for Kibana or external load balancer public IP addresses, the template does not configure SSL/TLS for them, so all communication is unencrypted. This is something that you will need to configure yourself.

Resources