0

I intend to create docker build for my node.js app. The build context is a remote git url with a docker file. When I run docker-compose up --build, it builds the app successfully but I get an error, npm ERR! path /code/package.json

My dockerfile

FROM node:16.15.0 as develop-stage
WORKDIR /code
COPY package.json .
# Using package-lock file to fix the version of installed modules.
COPY package-lock.json .
RUN npm install
COPY . .

My docker-compose

version: '3.8'
services:
  my-service:
    container_name: my-service
    build: 
      context: 'https://username:token@bitbucket.org/abc/test-repo'
      dockerfile: Dockerfile
    volumes:
      - .:/code
      - /code/node_modules
    command: nodemon index.js
    networks:
      - my-network
    expose:
      - 9608
    ports:
      - 9608:9608
    restart: on-failure
volumes:
  my-data:
    external: false
networks:
  my-network:
    external: false
power-cut
  • 1,310
  • 1
  • 14
  • 30
  • Why do you need to define two volumes in `my-service` related to `code` folder? – Anatoly Mar 08 '23 at 10:27
  • This docker-compose is intended for development environment. – power-cut Mar 08 '23 at 10:29
  • 1
    You should probably use Node for that; you don't need Docker. The `volumes:` as you have them hide everything that's in the image, so if you don't also have the application checked out locally, you'll replace the image content with an empty directory and get the error you see. – David Maze Mar 08 '23 at 11:32

1 Answers1

1

It seems that you have a problem with the content of your files.

Normally the solution below should be fine:

Dockerfile

FROM node:16.15.0
WORKDIR /code
COPY package.json .
COPY package-lock.json .
RUN npm install
COPY . .
EXPOSE 9608
CMD ["nodemon", "index.js"]

docker-compose.yml

version: '3.8'
services:
  myservice:
    build: .
    volumes:
      - myvolume:/code
    ports:
      - 9608:9608
    networks:
      - mynetwork
    restart: on-failure

volumes:
  myvolume:
networks:
  mynetwork:

I hope this will help you. Keep me informed ^^

mvetois
  • 111
  • 11
  • This works fine..but when I run the dev containers in the VSCode, it doesnt recognize as git repository, though it was copied ? – power-cut Mar 08 '23 at 14:37
  • Only "visible" files and folders will be copied. I think that by adding a line to copy the git folder, in the Dockerfile between line 6 and 7, it will be ok. (`COPY .git .`). If you are satisfied with my answer, could you approve it? ^^ – mvetois Mar 08 '23 at 14:51
  • It says ``` => CACHED [6/7] COPY . . 0.0s => ERROR [7/7] COPY .git . 0.0s ------ > [7/7] COPY .git .: ------ failed to solve: failed to compute cache key: "/.git" not found: not found``` – power-cut Mar 08 '23 at 14:54
  • 1
    In fact I just checked, it is not possible to import a git repo in a Docker container. It can only be done by cloning it directly into the container. https://stackoverflow.com/a/23411161/14671536 – mvetois Mar 08 '23 at 14:57
  • 1
    Thanks for the answer, I am upvoting it for now ! – power-cut Mar 08 '23 at 14:59