docker logs doesn't log detached exec - node.js

My Dockerfile is
FROM node:4
RUN npm install -g yarn
WORKDIR /app
I run docker run -d and mount my current working directory as a volume. All the deps are installed by yarn. I have a npm script to lint the files.
If I do docker exec -it [container] npm run lint it works as expected and I can see all the logs. But if I do docker exec -itd [container] npm run lint, it exits immediately which is expected. But I can't see the logs by running docker logs [container]. How do I reattach the exec or just see to the logs?
I tried docker attach [container] it goes to the repl of nodejs. Why is that?

As mentioned in "Docker look at the log of an exited container", you can use docker logs
docker logs -t <container>
That will show stdout/stderr (with timestamps because of the -t option).
For that last 50 lines of those logs:
docker logs -t <container id> | tail -n 50
Note: that would work only if npm run lint is run by your container (docker run <image> npm run lint)
If your docker exec exits immediately, then yes, there would be no logs produces by the container itself.

Related

Docker: Docker run does not report error but docker file has errors, how to view them?

Here is my docker file a very simple docker file
FROM node:19-alpine
COPY package.json /app/
COPY src /app/
WORKDIR /app
RUN npm install
RUN ls
CMD [ "node", "src/index.js" ] //it has error, it should be index.js
When I try to execute docker build command it shoes the container id however docker ps does not show any containers, which means container was not launched successfully and there is problem with docker file.
the error is reported if I try to run container from docker client GUI.
How to view the errors from command line if docker file has a problem and it wasn't reported by docker build command?
docker ps only shows running containers.
If you do docker ps -a you should see the container and that it has the 'exited' status.
You can then do docker logs <container name> to see any error messages.
You need first to be clear on these two points:
1. docker file is used to build an image.
Use this command to build an image
you can also refer here
docker build -t node:test --progress=plain .
if run successly, your will get the image ID
2. but you also need to start a container based by this image.
docker run -itd --name=node-test node:test
docker ps | grep node-test
Also, check this container if or not based by your image

Error Response Contrainer is not running

I am trying to Create and Join channel by entering into cli container using command : docker exec -it cli bash
But, i am getting following error response :
Error Response from daemon : Container dasdjha343343xxxxx is not running.
First stop all your running containers and remove them, try to rerun the exact container, and lastly, when you try to bash to explicit container on Windows.10 use $winpty
$docker stop $(docker ps -a -q)
$docker ps -qa|xargs docker rm
$cd fabric-samples/first-network
$docker-compose -f docker-compose-cli.yaml up -d
$winpty docker exec -it cli bash
In your working folder, update to latest version by:
$ git clone https://github.com/hyperledger/fabric-samples.git
Newer version should resolve this issue.

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;'

cannot pm2 list in docker containers

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.

Resources