I'm a little bit lost with Docker. I try to start my NodeJS app via PM2 process manager. The general syntax is pm2 start app.js
.
This works:
First logging into the running docker container:
docker exec -it mongodb-plus /bin/bash
Then inside the container, run pm2:
root@367a1f9d1XXX:/# pm2 start app.js
This fails:
But when I try to reach the same effect without the interactive terminal session:
docker exec mongodb-plus /bin/bash -c "pm2 start app.js"
...it fails with bash: pm2: command not found
Question: Why can't bash find the pm2 executable for the second variant?
For reference - my Dockerfile. (It's based on the mongo image, then install adminMongo.):
FROM mongo
#Install basic tools via apt-get
RUN apt-get update &&\
apt-get install -y nano git curl &&\
#Install NVM and latest LTS NodeJS vesion
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash &&\
export NVM_DIR="$HOME/.nvm" &&\
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" &&\
nvm install --lts &&\
nvm use --lts &&\
nvm alias default lts/* &&\
#Install adminMongo
mkdir -p /home/srvuser/apps/adminMongo && cd /home/srvuser/apps/adminMongo && git clone https://github.com/mrvautin/adminMongo.git && mv adminMongo/* . && ls -la &&\
npm install &&\
#Install PM2 and autostart
npm install -g pm2 &&\
pm2 startup
COPY ./app.json /home/srvuser/apps/adminMongo/config
#Expose mongoDB, adminMongo
EXPOSE 27017 1234
Actually I'm starting the container with docker run --rm --name mongodb-plus -v mongodata:/data/db -p 27017:27017 -p 1234:1234 mongodb-rcore --auth
My target aim is to automatically run the app.js from adminMongo when the container starts.
I guess a better solution would be as below instead of using export and instead of writing the node version in multiple line which make it harder to modify:
ENV NVM_DIR /root/.nvm
ENV NODE_VERSION v0.33.2
ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules #Ensure that this is the actual path
ENV PATH $NVM_DIR/versions/node/v$NODE_VERSION/bin:$PATH
# Then use the NODE_VERSION do download the nodejs version you want