Home > Back-end >  Dockerfile can not see package.json file
Dockerfile can not see package.json file

Time:12-15

I've got this structure of the project:

- project
-- apps
--- microservice-one
---- Dockerfile
-- package.json
-- docker-compose.yml

Here is my Dockerfile from the microservice-one:

FROM node:12.13-alpine As development

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install 
COPY . .

RUN npm run build

CMD ["node", "dist/main"]

and docker-compose.yml

services:
  microservice-one:
    container_name: microservice-one
    build:
      context: ./apps/microservice-one
      dockerfile: Dockerfile
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    ports:
      - 3000:3000
    expose: 
      - '3000'
    command: npm run start

why after run docker-compose build, my console throw me this error:

#12 0.725 npm ERR! code ENOENT
#12 0.726 npm ERR! syscall open
#12 0.728 npm ERR! path /usr/src/app/package.json
#12 0.729 npm ERR! errno -2
#12 0.734 npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'

I think that the problem is in my Dockerfile and COPY package*.json ./, is a chance that my Dockerfile can not see package.json?

Thanks for any help!

CodePudding user response:

From your filesystem structure the directory hosting Dockerfile is the build context -> microservice-one.

When you perform a docker build ..., the build context and all it's content gets wrapped up and sent over to the docker daemon, which then tries to build your container. At that time there is only access to the build context, nothing outside. ( https://docs.docker.com/engine/reference/commandline/build/#build-with-path )

So unless you move/copy your file over it will remain invisible.

CodePudding user response:

When your context is ./apps/microservice-one, the Dockerfile can only copy files from that directory and directories below it.

Your Dockerfile is written as if it assumes that the context is the current directory, so if you change the context and the dockerfile values in the docker-compose file, it should work.

services:
  microservice-one:
    container_name: microservice-one
    build:
      context: .
      dockerfile: ./apps/microservice-one/Dockerfile
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    ports:
      - 3000:3000
    expose: 
      - '3000'
    command: npm run start
  • Related