Docker Community Forums

Share and learn in the Docker community.

How to terminate nodejs process inside container

I have a container that must or must not start nodejs app inside container depending on .env settings.

docker-compose.yml
version: “3.5”

services:
  php:
    build:
      context: ./
      dockerfile: docker/php/Dockerfile
    ports:
      - 9234:9000
    restart: always
    working_dir: /var/www
    volumes:
      - ./:/var/www:cached

docker/php/Dockerfile:

FROM php:7.1-fpm
WORKDIR /var/www

RUN apt-get update -yq \
    && apt-get install curl gnupg -yq \
    && curl -sL https://deb.nodesource.com/setup_10.x | bash \
    && apt-get install nodejs -yq

CMD ["node", "appNodejs/server.js"]

I need to exec or discard
CMD [“node”, “appNodejs/server.js”]
command depending on env file params. I didn’t find out how to get it with Dockerfile, so I check env file inside my server.js and do
process.exit(0)
if node server isn’t required. But docker continuously restarting server.js and i’m getting:

app_php_1 exited with code 0
app_php_1 exited with code 0
app_php_1 exited with code 0
app_php_1 exited with code 0
app_php_1 exited with code 0
app_php_1 exited with code 0
app_php_1 exited with code 0

Also I’m getting on startup:

Error: Cannot find module '/var/www/appNodejs/server' 

I think it’s because of volumes are attached after CMD [“node”, “appNodejs/server.js”] - how to fix it?
How to terminate node process so it wouldn’t be starting again?

Hi, volumes are attached “on boot”, so it will be there when the CMD is exectuted, but, it could be the “:cached” in your volume mount, try and remove that part.

Is restarted because you made it as a service, so docker will continue to try to get your container up and running, also becaues you defined: restart: always
And it exits because of the error you mentioned.

Thanks! But how can I shutdown a node service or better is to not to start it depending on env file parameter?

What do you mean by stopping it? Why do you want to stop it? at what conditions?

In the docker/php/Dockerfile I want to start php-fpm as main service. But CMD [“node”, “appNodejs/server.js”] should or should not starting based on my env file. So I need something like this:

FROM php:7.1-fpm
WORKDIR /var/www
# here I want to start php-fpm as it my main service

RUN apt-get update -yq \
    && apt-get install curl gnupg -yq \
    && curl -sL https://deb.nodesource.com/setup_10.x | bash \
    && apt-get install nodejs -yq

# start nodejs app by condition only
if (condition) { 
    CMD ["node", "appNodejs/server.js"]
}

I didn’t find how to add if statement like this in dockerfile, so maybe terminating server.js inside itself is workaround.

Okay, the best way to do this, would be to create a custom start script.
So you in the start shell script check what process should be run and execute.

Sorry, it’s about entrypoint script?

yes it is, like you create a bash script in /startup.sh, and in that you check and and execute what you need.
and then define /startup.sh as the entrypoint