Running arbitrary node.js code in a docker?


Recommended Posts

Greetings all.  Between various home automation scripts and some web things, I keep encountering the need to run various node.js code, usually as "glue" for this and that.  It's pretty rare than anyone bothers to put together a docker for this stuff, let alone something that runs out of the box in unraid.

 

I've spent some time going down the rabbit hole trying to build something myself, but I seem to keep getting hung up on getting the actual code into the docker in quite the same way as other packaged dockers I've run.

 

Does anyone have any pointers or, ideally, any specific method they've used to do this in the past.  Basically, I need to bring in a node.js docker, import some arbitrary code (ideally I'd like this code to live in the appdata share so I can modify it without updating the docker), and be able to run that code on the docker launch.

 

Any thoughts?  Thanks! 

Link to comment
On 7/19/2019 at 9:54 AM, peng1can said:

Greetings all.  Between various home automation scripts and some web things, I keep encountering the need to run various node.js code, usually as "glue" for this and that.  It's pretty rare than anyone bothers to put together a docker for this stuff, let alone something that runs out of the box in unraid. 

 

I've spent some time going down the rabbit hole trying to build something myself, but I seem to keep getting hung up on getting the actual code into the docker in quite the same way as other packaged dockers I've run.

  

Does anyone have any pointers or, ideally, any specific method they've used to do this in the past.  Basically, I need to bring in a node.js docker, import some arbitrary code (ideally I'd like this code to live in the appdata share so I can modify it without updating the docker), and be able to run that code on the docker launch.

 

Any thoughts?  Thanks! 

Hi @peng1can

I'm not saying this is the very best way, but this is what I'd do...

Create the folder (in appdata if you'd like) with your node.js code files inside it

run a node.js container that executes your code:

docker run -d --rm -v /mnt/user/appdata/my_nodejs:/my_nodejs node:lts-alpine node /my_nodejs/peng1can.js

change "my_nodejs" to the folder name you made, change "pen1can.js" do your node js file you need to execute

 

Very simple example but it can be expanded depending on what you're doing with node.

Edited by dee31797
Link to comment

Another thing you could think about is using Node-Red instead of just a node.js script. I've found I could put the vast majority of my home automation related node.js scripts in Node-Red. For me that is easier to support than a random js script running somewhere, but your needs may differ.

Link to comment
  • 6 months later...

I made a dir for my little node app called "wx":

cd /mnt/user/appdata; mkdir wx; cd wx

 

Then I made a simple Dockerfile with the following contents:

FROM node:6.8.1

WORKDIR /usr/src/app

COPY package.json .

RUN npm install

COPY server.js .

EXPOSE 3006
CMD [ "node", "server.js" ]

 

Then I created the docker image:

docker build -t foo/wx .

 

Then I started it with:

docker run --net=nginx -h wx --name wx -p 3006:3006 -d foo/wx

 

nginx is the net I created following the spaceinvaderone letsencrypt/nginx video.

 

After starting this container it shows up in the docker tab in unraid but all you can really do is stop or remove it.

 

How can I autostart it?

Edited by uek2wooF
added "with the following contents"
Link to comment
  • 4 months later...
On 2/4/2020 at 9:08 PM, uek2wooF said:

I made a dir for my little node app called "wx":

cd /mnt/user/appdata; mkdir wx; cd wx

 

Then I made a simple Dockerfile with the following contents:

FROM node:6.8.1

WORKDIR /usr/src/app

COPY package.json .

RUN npm install

COPY server.js .

EXPOSE 3006
CMD [ "node", "server.js" ]

 

Then I created the docker image:

docker build -t foo/wx .

 

Then I started it with:

docker run --net=nginx -h wx --name wx -p 3006:3006 -d foo/wx

 

nginx is the net I created following the spaceinvaderone letsencrypt/nginx video.

 

After starting this container it shows up in the docker tab in unraid but all you can really do is stop or remove it.

 

How can I autostart it?

How did you solve your problem? Did you add something /boot/config/go to start your app automatically?

Link to comment
  • 2 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.