cannot pm2 list in docker containers - node.js

I build a Docker image with Node.js and pm2. I started the container with:
docker run -d --name test -p 22 myImage
Then I go inside the container with:
docker exec -it test /bin/bash
In the container, exec the command:
pm2 list
And it stuck here:
P.s.: My application works well in the Docker container, if I add CMD pm2 start app.js in the Dockerfile.

If your dockerfile CMD is a pm2 command, you have you include --no-daemon arg option so pm2 runs in the foreground and so your docker container continues to run.
An example Dockerfile CMD:
CMD ["pm2", "start", "app.js", "--no-daemon"]
Otherwise, without --no-daemon, pm2 launches as a background process and docker thinks the execution of the pm2 command is done running and stops.
See https://github.com/Unitech/PM2/issues/259

CMD ["pm2-docker", "pm2.yaml"]
This is the new approach.
Please do not use previous approaches.

Related

Docker container don't start

Hi I've got a problem with docker. I'm using it on s390x Debian, everything was working fine but now i can't start my containers. Old containers are working but when i create new container using for example: docker run ubuntu then i'm trying docker start [CONTAINER] my container don't start. When i use docker ps -a I've got all of my containers, but after when I use docker ps i can't see my new container. As you can see on scr. I created container with name practical_spence and ID 3e8562694e9f but when i use docker start, it's not starting. Please help.
As you do not specify a CMD or entrypoint to run, the default is used which is set to "bash". But you are not running the container in interactive terminal mode, so the bash just exits. Run:
docker run -it ubuntu:latest
to attach the running container to you terminal. Or specify the command you want to run in the container.
You container did start but exit instantly as it has nothing to do. You can start like this docker run -d ubuntu sleep infinity. Then use docker ps to see the running container. You can of course exec into it to do something docker exec -it <container> bash. You can stop it docker stop <container>. Re-start it docker start <container>. Finally delete (stopped) it as you don't need it anymore docker container rm <container>.

Why app in docker container doesn't restart?

I've deployed some docker containers with golang apps. One of them I need to start by this command:
docker run --restart unless-stopped -it myapp /bin/bash
The next step I enter the container and edit some config files, then I run
go build main.go
and ./main
After that I press ctrl+q and leave it out.
Everything works perfectly and all my containers restart perfectly after restarting server. But there is one issue, when myapp container restarts, the golang application doesn't run while container still works. I have to enter this again and run ./main. How can I fixed it?
Dockerfile
FROM golang:1.8
WORKDIR /go/src/app
COPY . .
RUN go-wrapper download # "go get -d -v ./..."
RUN go-wrapper install # "go install -v ./..." RUN ["apt-get","update"]
RUN ["apt-get","install","-y","vim"]
EXPOSE 3000
CMD ["app"]
When you create a container and pass in /bin/bash as the command, that's as far as Docker cares. When the container restarts, it will start up another instance of /bin/bash.
Docker doesn't watch your shell session and see what things you do after it starts the command. If you want to actually run ./main as the command of the container, then you'll need to pass in /go/src/app/main as the command instead of /bin/bash.
Additionally, compiling code is something better done during the image build phase instead of at container runtime.

Forever monitor fails to run inside a docker container [duplicate]

i have a problem when start node with forever in docker container, if i launch manually works, instead the same command in Dockerfile, when build and start the container, exited.
The command works in bash:
docker run -it container_name bash forever start -c 'node --harmony' /my/path/app.js
I tried to put command in Dockerfile but the container don't start
CMD forever start -c 'node --harmony' /my/path/app.js
Google Group discussion
Forever start script.js runs in the background. To run forever in the foreground, try forever script.js.
This starts forever in the foreground, which is what Docker needs. Remember a container is "alive" only as long as the process defined in CMD is up and running. Since forever starts as a daemon, the command itself exits and docker will exit also.
CMD forever -c 'node --harmony' /my/path/app.js
Try using the array syntax:
CMD ["forever", "start", "-c", "node --harmony", "/my/path/app.js"]
I'm now trying to use forever in docker. This works:
CMD ["forever", "src/app.js"]
Put in your Dockerfile :
CMD forever app.js

How to stop running node in docker

I have just installed dockers and installed node.
I am able to run a basic express site. My issue now is I can't stop it. Control-C is not doing anything.
Temporarily what I did to exit was:
Close the docker's terminal.
Open a new one.
Search for all docker containers that is running.
Then docker stop [container]
Is this the proper way?
As described here: github: docker-node best practice
You can add the --init flag to your docker run command.
docker run -it --init -p 3000:3000 --name nodetest mynodeimage
I don't know if this is too late. But the correct way to do this is to catch the SIGINT (interrupt signal in your javascript).
var process = require('process')
process.on('SIGINT', () => {
console.info("Interrupted")
process.exit(0)
})
This should do the trick when you press Ctrl+C
I came across this same problem today, and struggled to find an explanation/solution. I discovered (through trial and error) that this only occurs when the CMD in the Dockerfile is set to:
CMD [ "node", "server.js" ]
However, Ctrl+C works fine when the CMD is changed to:
CMD [ "npm", "start" ]
The npm start script in my package.json file is set to node server.js, so I have no idea why this change works, but hopefully this helps.
A docker run should have gave you back the prompt, avoiding the need for CTRL+C, or closing the docker terminal.
Once you log back in that terminal, a docker ps -a + docker stop should be enough to make your container exit (you still need to remove it before trying to launch it again)
If you just want to stop node without stopping the container, you could go inside the container and run:
$ ps aux | grep node #to obtain process ID (value in second column)
$ kill <process ID>
As a part of solution, you can open your package.js and add 3 new commands/scripts :
"scripts": {
"docker-build-and-run": "docker build -t image-dev-local . && docker run -p 3001:3001 --name container-dev-local image-dev-local",
"docker-stop-and-clear": "(docker stop container-dev-local || true) && (docker rm container-dev-local || true)",
"docker-run": "npm run docker-stop-and-clear && npm run docker-build-and-run"
}
and just simply run in the terminal :
npm run docker-run
to up your app on 3001 port in docker and have fun. Every next run will clear previous and build/run again.
To stop and delete it, just run :
npm run docker-stop-and-clear
docker stop <containerName/containerId>
docker kill --signal=SIGINT <containerName/containerId>
docker rm -f <containerName/containerId>
From what I can gather you need both -t and -i for Ctrl-C to work as expected. Command like this would be helpful i believe.
Simple example which i can think of this below
Case 1 to retain container:
$ ID=$(sudo docker run -t -d ubuntu /usr/bin/top -b)
$ sudo docker attach $ID
Control-C
$ sudo docker ps
Case 2 to terminate the container:
$ ID=$(sudo docker run -t -i -d ubuntu /usr/bin/top -b)
$ sudo docker attach $ID
Control-C
$ sudo docker ps
The solution is to use in the Dockerfile this command for starting the application
CMD ["/bin/sh", "-c", "node app.js"]
then we can listen in the app.js with
process.on('SIGTERM', () => {
console.log('SIGTERM received, shutting down...');
});
and we have to run the dockerfile with the --init flag
docker run --init -p 3000:3000 --name nodetest mynodeimage
or we can add in docker-compose beginning from version 3.7 the entry
init: true
to the desired service.
For the app to receive the signal you should use docker stop nodetest or docker-compose down. Shutting down with Ctrl+C does not send the SIGTERM signal.
Inside the node console, after running docker run -it node,
you can exit with the following:
Enter .exit
Press two times
ctrl+c
ctrl+d
If the node container is started in detached mode docker run -d node,
you can stop it with docker stop <CONTAINER_ID or CONTAINER_NAME>.
For example, assuming you want to kill the newest node container:
docker stop $(docker ps | grep node | awk 'NR == 1 { print $1}')

Start node app when running docker container from cli

I'm relatively new to Docker and have a node web server which I have added to a docker image. My image is built using packer, so I don't have a Dockerfile.
My question is when running the docker container on the command line with docker run -it -d <imageId> is there a way to pass in the command to run my web server that resides in the container?
So something like docker run -it -d <imageId> npm start
Got it working with
docker run -it -d -w /path/to/code/folder <imageName:version> node server.js 'daemon off;'

Resources