Docker Community Forums

Share and learn in the Docker community.

Run nodejs script on startup

docker

#1

Hi all,

I hope someone can help me. I will start by explaining that I am very much still learning docker.

I am using a docker image downloaded from dockerhub which runs a particular app called cgate. I have created a container in my QNAP container station and everything seems to work.

However I also want to run a nodejs script called cgateweb on the same docker container. I can run ‘nodejs /usr/local/bin/cgateweb/index.js’ and it does as expected however I cannot for the life of me work out how to load it on startup.

I initially started by trying init.d, rc.local and cron @reboot but I soon found none of these worked. As mentioned, quite new to docker and soon realised my understanding of it was lacking and that generally you can’t use the normal startup methods in a container.

I tried to instead use CMD or ENTRYPOINT but I am having issues doing this. Currently the cgate container I downloaded has the following ‘command’ (Qnap’s GUI field for CMD):

/bin/sh -c “/etc/init.d/cgatesh start && /bin/bash”

Is there an easy way I can either add to the CMD above (worked out how to do on QNAP) without needed to create a dockerfile? Unfortunately the author of the docker image I downloaded doesn’t have a dockerfile for me to modify so I am doing an export/import of the container which allows me to change the CMD. I have also installed some extra applications such as nodejs which I am unsure on how to do with the dockerfile if I had it.

Sorry this may not make much sense (struggling myself) but if anyone can give me an easy way I can add the auto startup of the .js file I am wanting to use, you’ll be my friend forever.

Thanks.

Locky


(Gary Forghetti) #2

The easiest and simplest way is to create your own custom docker image from the image in Docker Hub which is running that cgate application. Your Dockerfile uses that image as it’s base image (Dockerfile FROM statement). The custom docker image you build will inherit everything from that base image. You just need to code a CMD or ENTRYPOINT statement to override the same that are in the base image.

FROM --specify the base cgate image here--
CMD  --specify your command here--

#3

Anyone got any advice? Thanks.


#4

Thanks @gforghetti - I ended up just creating a separate docker container using node and ran the script in there.