Max Kurtz
Max Kurtz

Reputation: 478

Can't build Node container with volume with package.json file to run npm install

How does Docker build containers? I can't figure it out. I want:

I've tried to list content of folders with ls command, but the /src/ is always empty (prints: src) My docker-compose.yml:

version: '3'

services:
  node:
    build:
      context: .
      dockerfile: Dockerfile.node
    volumes:
      - ./src:/src
    command: run develop
    tty: true

My Dockerfile.node:

FROM node:12
WORKDIR /src
COPY ./src/package*.json ./src/
RUN ls
RUN cd ./src
RUN ls
RUN npm install
RUN ls

On the RUN npm install command I got this error:

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

I start project with command docker-compose up --build

My folder structure is:

/
src
  --package.json
docker-compose.yml
Dockerfile.node

Please help, thank you in advance.

Upvotes: 0

Views: 200

Answers (1)

Adiii
Adiii

Reputation: 59916

cd ./src only available in the current RUN command, as Dockerfile each command run in a separate shell, so when it comes to run npm install at this time your working is WORKDIR that is /src not the one you are expecting using cd .src which should be /src/src.

RUN pwd
#/src
RUN cd ./src #here /src/src
RUN ls
#/src <-- back to WORKDIR, while you are expecting /src/src
RUN npm install

In short, there is WORKDIR in dockerfile not cd.

You have to option, change command

RUN cd ./src && npm i

or change the copy command and leave the rest as it is.

COPY ./src/package*.json .

Upvotes: 1

Related Questions