Nodejs Kubernetes Deployment keeps crashing

10/18/2017

I'm pulling my hair out for a week but I am close to giving up. Please share your wisdom.

This is my Docker file:

FROM node
RUN apt-get update

RUN mkdir -p /var/www/stationconnect
RUN mkdir -p /var/log/node


WORKDIR /var/www/stationconnect
COPY stationconnect /var/www/stationconnect
RUN chown node:node /var/log/node
COPY ./stationconnect_fromstage/api/config /var/www/stationconnect/api/config
COPY ./etc/stationconnect /etc/stationconnect
WORKDIR /var/www/stationconnect/api 
RUN cd /var/www/stationconnect/api
RUN npm install

RUN apt-get install -y vim nano 
RUN npm install supervisor forever -g


EXPOSE 8888

USER node

WORKDIR /var/www/stationconnect/api

CMD ["bash"]

It works fine in docker alone running e.g.

docker run -it  6bcee4528c7c

Any advice?

-- Vick Krishna
docker
dockerfile
kubernetes
node.js

3 Answers

10/19/2017

You could add an ENTRYPOINT command to your Dockerfile that executes something that is run in the background indefinitely, say, for example, you run a script my_service.sh. This, in turn, could start a webserver like nginx as a service or simply do a tail -f /dev/null. This will keep your pod running in kubernetes as the main task of this container is not done yet. In your Dockerfile above, bash is executed, but once it runs it finishes and the container completes. Therefore, when you try to do kubectl run NAME --image=YOUR_IMAGE it fails to connect because k8s is terminating the pod that runs your container almost immediately after the new pod is started. This process will continue like this infinitely.

Please see this answer here for a in-line command that can help you run your image as is for debugging purposes...

-- hmacias
Source: StackOverflow

10/18/2017

this is kind of obvious. You are running it with interactive terminal bash session with docker run -it <container>. When you run a container in kube (or in docker without -it) bash will exit immediately, so this is what it is doing in kube deployment. Not crashing per say, just terminating as expected.

Change your command to some long lasting process. Even sleep 1d will do - it will die no longer. Nor will your node app work though... for that you need your magic command to launch your app in foreground.

-- Radek 'Goblin' Pieczonka
Source: StackOverflow

11/19/2017

When create a container, you should have a foreground process to keep the container alive.

What i’ve done is add a shell script line while true; do sleep 1000; done at the end of my docker-entrypoint.sh, and refer to it in ENTRYPOINT [/docker-entrypoint.sh]

Take a look at this issue to find out more.

There’s an example how to make a Nodejs dockerfile, be sure to check it out.

-- Quang Keu
Source: StackOverflow