How to copy files and execute pm2 with docker

I have the follow structure

[vagrant@localhost]$ ls
api         package.json yarn.lock
client      favicon.ico
Dockerfile  ecosystem.config.js

With follow Dockerfile

FROM keymetrics/pm2-docker-alpine
RUN npm install pm2 -g
CMD ["pm2-docker", "ecosystem.config.js"]

So my question is.. how to run this image and keep runner with pm2 ?

docker run -d -p 3000:3000 --name backend backend:v3

[vagrant@localhost swarmbot]$ docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS                      NAMES
f28aa7459527        backend:v3          "pm2-docker ecosys..."   10 minutes ago      Exited (1) 9 minutes ago                              backend

Status Exited .. why?

Start a personal dev blog on your domain for free and grow your readership.

3.4K+ developers have started their personal blogs on Hashnode in the last one month.

Write in Markdown 路 Publish articles on custom domain 路 Gain readership on day zero 路 Automatic GitHub backup and more

Comments (2)

Anthony Lapenna's photo

You could have a look at the logs first... It might help you figure out what's the problem:

docker logs backend

You can try to use the following Dockerfile:

FROM keymetrics/pm2-docker-alpine

RUN npm install pm2 -g

COPY . /pm/

CMD ["pm2-docker", "ecosystem.config.js"]

The WORKDIR instruction will create a new /pm folder in the container and set this folder as the working directory for the next COPY and CMD commands, see:

In the CMD instruction, there is no need to specify the path to the ecosystem.config.js file as it should be copied in the /pm directory which is the working directory.

Give it a try.

Riderman de Sousa Barbosa's photo

Thanks Anthony Lapenna .. i found the error. Was a error on connection with MongoDB. I need to link mongodb container with my container.

But thanks.