Start an interactive node.js CLI app with docker-compose up - node.js

I made a node.js interactive CLI app to let communicate our intern work log with jira.
It works without problems, but I want to make it cross platform using docker.
The idea is to start docker and start inside the container the CLI, and than close the CLI and close also the container.
Now, to start my app I just give npm start. I managed to create a docker container and run the app inside, but to start the app I have to docker-compose exec app bash and then npm start
I want to do it during the container start up process, in order to give just docker-compose up and start to use my CLI.
Is that in any way possible?
I tried to add commands: "bash -c npm start" to my docker-compose file, and it works, but the output is not interactive. I see my app in the docker log but I can not interact with it.
Ideas?

Related

How to run "npm run start" from release pipeline Azure DevOps to start production build in NUXTJS?

I want to create CI/CD for my nuxt application to deploy it on windows server. I have done all the things like copying files, npm install, npm run build. But the last step is to start the server by npm run start command and after running this command nuxt will show some related information like port no. etc and the command remains in the executable form (non-terminated command).
So when I run it from my pipeline the task is not terminating because of it.
https://nuxtjs.org/docs/get-started/commands#production-deployment
I had tried different little hacks like
Created batch file and run it as Power-shell command from start-process command
Open new cmd window to execute npm run start and close the previous one from this command "start cmd /k echo npm run start"
I use pm2 (it's free, https://pm2.keymetrics.io/) to daemonize nuxt on production servers. It works very well with nuxt and it respawns the process if for some reason crashes. That way you will exec a command on startup that will launch nuxt as a daemon in the background, but the command will terminate... that may solve your issue I think.

How to execute sails js in docker

I am using docker for my sails js project. Any idea how to check which command was used to run sails.js. I have tried History command but its not giving me previous command which was run
Can anybody tell me how to check the which command was used before to execute the sailsjs? I need to restart my sails.js
To check what command used to run to make it started in docker, try
docker ps -a --no-trunc --format "{{.ID}}: {{.Command}}"
It will show the full command for sailsjs for the container.

Building a custom Node-RED image

I would like to make my own Node-RED docker image so when I start it the flows are loaded and Node-RED is ready to go.
The flow I want to load is placed in a 'flows.json' file. And when I import it manually via the interface it works fine.
The Node-RED documentation for docker suggests the following line for starting Node-RED with a custom flow
$ docker run -it -p 1880:1880 -e FLOWS=my_flows.json nodered/node-red-docker
However when I try to do this the flow ends up empty.
I suspect this has to do something with the fact that the flow I'm trying to load is using the 'node-red-node-mongodb' plug-in, which is not installed by default.
How can I build a Node-RED image where the 'node-red-node-mongodb' is already installed?
If anymore information is required please ask.
UPDATE
I made the following Dockerfile:
FROM nodered/node-red-docker
RUN npm install node-red-node-mongodb
Then I build it with:
docker build -t testenvironment/nodered .
And started it with:
docker run -d -p 1880:1880 -e FLOWS=flows.json --name node-red testenvironment/nodered
But when I go to the Node-RED interface there is no flow. Also I don't see the MongoDB node in the sidebar.
The documentation on the Node-RED site includes instructions for how to customise a Docker image and add extra nodes. You can either do it by logging into the existing image using docker exec and installing the node by hand with npm
# Open a shell in the container
docker exec -it mynodered /bin/bash
# Once inside the container, npm install the nodes in /data
cd /data
npm install node-red-node-mongodb
exit
# Restart the container to load the new nodes
docker stop mynodered
docker start mynodered
Else you can extend the image by creating your own Docker file:
FROM nodered/node-red-docker
RUN npm install node-red-node-mongodb
And then build it with
docker build -t mynodered:<tag> .

Create docker container with Node.js/NPM preinstalled but no package.json

I am looking for a Docker image that is just some *nix flavor with NPM and Node.js installed.
This image
https://hub.docker.com/_/node/
requires that a package.json file is present, and the Docker build uses COPY to copy the package.json file over, and it also looks for a Node.js script to start when the build is run.
...I just need a container to run a shell script using this technique:
docker exec mycontainer /path/to/test.sh
Which I discovered via:
Running a script inside a docker container using shell script
I don't need a package.json file or a Node.js start script, all I want is
a container image
Node.js and NPM installed
Does anyone know if there is an a Docker image for Node.js / NPM that does not require a package.json file? Perhaps I should just use a plain old container image and just add the code to install Node myself?
Alright, I tried to make this a simple question, unfortunately nobody could provide a simple answer...until now!
Instead of using this base image:
FROM node:5-onbuild
We use this instead:
FROM node:5
I read about onbuild and could not figure out what it's about, but it adds more than I needed for my use case.
the below code is in our Dockerfile
# 1. start with this image as a base
FROM node:5
# 2. copy the script from real-life into the container (magic)
COPY script.sh /usr/src/app/
# 3. define container entry point which will run our script
ENTRYPOINT ["/bin/bash", "/usr/src/app/script.sh"]
you build the docker image like so:
docker build -t foo .
then you run the image like so, which will "run the entrypoint":
docker run -it --rm foo
The container stdout should stream to the terminal where you ran docker run which is good (am I asking too much?).

How to restart Node on a docker container without restarting the whole container?

I have a container ==> FROM node:5
Node should restart after each change in the code.
But there is no way for me restart the Node server without restarting the whole docker container.
I have many npm install on dockerfile that runs each time I restart the container, and it's annoying to wait for all of them after each change in the code.
I'm already using shared folder to have the latest code in my container.
If you just need the Node.js server to restart after changes, instead of starting the server with the node command you can use a tool like node-dev or nodemon. To leverage these, you'd need to update the command in your Dockerfile, e.g. CMD [ "node-dev", "server.js" ].
Since you already have your local files as a volume in the Docker container, any change to those files will restart the server.
Here's how I did it. In your docker-compose.yml, in the entry for the appropriate service, add an entrypoint to run npx nodemon /path/to/your/app
This will work even if you don't have nodemon installed in the image.
e.g.
services:
web:
image: foo
entrypoint: ["npx", "nodemon", "/usr/src/app/app.js"]
I think that's not the optimal way to Docker. You should try to build your own Docker image which includes your code changes. In your own Docker image you could make the npm install step part of the container build so you do not need to run this command on container startup.

Resources