Purzynski Purzynski - 1 year ago 130
Node.js Question

docker compose vs docker run - node container

I am trying to setup container for react app. My goal is to use docker-compose (I want to execute one command and have everything working)

The problem is that when I am trying you do it with docker-compose and docker file (which are below) I am getting information that:


version: '2'
build: ./images/react
- "3000:3000"
- "/home/my/path:/app"


FROM node:6.9


RUN npm install


CMD [ "npm", "start" ]


npm WARN enoent ENOENT: no such file or directory, open '/app/package.json'

But when I did it with docker run and volumes mapping I was able to see packages.json and run npm install command.

docker run -it --rm -v /home/my/path:/app node:6.9 bash

Why it is not working with docker compose?

Answer Source

Note that the volume that you're describing in your docker-compose.yml file will be mounted at run time, not at build time. This means that when building the image, there will not be any package.json file there (yet), from which you could install your dependencies.

When running the container image with -v /home/my/path:/app, you're actually mounting the directory first, and subsequent npm install invocations will complete succesfully.

If you intend to mount your application (including package.json) into your container, the npm install needs to happen at run time (CMD), and not at build time (RUN).

The easiest way to accomplish this would be to simply add the npm install statement to your CMD instruction (and drop the RUN npm install instruction):

CMD npm install && npm start
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download