dockernpm

npm install doesn't work in Docker


This is my Dockerfile:

FROM node:7

RUN apt-get update && apt-get install -y --no-install-recommends \
    rubygems build-essential ruby-dev \
    && rm -rf /var/lib/apt/lists/*

RUN npm install -gq gulp bower

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app

RUN npm install

CMD ["gulp", "start:dev"]

When I build the image, the npm install command executes with little output and really quickly. I actually build it through docker-compose which does have a volume mounted - and I cannot see the node_modules folder being created on my hose. When I launch a container on this image, I can see there is no node_modules folder. I then execute npm install and things start working - it takes 2-3 minutes to install all the packages and the node_modules folder is indeed created.

What is happening here? What am I doing wrong? Why doesn't npm install work at build time, but then it works at run time?


Solution

  • The npm install should have worked based on your Dockerfile. You can see the created files if you run the image without a mounted volume (DIRNAME: where your docker-compose.yml is located):

    docker run --rm -it DIRNAME_node ls -ahl /usr/src/app

    With docker build, all data is stored in the image. So, it's intended that you don't see any files created on your host.

    If you mount a volume (generally in Linux, also in a Docker container), it overlays the directory. So you can't see the node_modules created in the build step.

    I suggest you do your tests based on the Docker image itself and don't mount the volume. Then you have an immutable Docker image which is better for deployment.