Purzynski
Purzynski

Reputation: 1227

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:

docker-compose.yml

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

Dockerfile

FROM node:6.9

WORKDIR /app

RUN npm install

EXPOSE 3000

CMD [ "npm", "start" ]  

Result

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?

Upvotes: 2

Views: 1355

Answers (1)

helmbert
helmbert

Reputation: 38014

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

Upvotes: 5

Related Questions