0

I'm trying to run a node image(with nestJS application) inside docker but I have this error:

*$ docker compose build
[+] Building 7.3s (11/11) FINISHED
 => [internal] load build definition from Dockerfile                                                          0.0s
 => => transferring dockerfile: 747B                                                                          0.0s
 => [internal] load .dockerignore                                                                             0.0s
 => => transferring context: 2B                                                                               0.0s
 => [internal] load metadata for docker.io/library/node:12.22.4-alpine                                        6.6s
 => [auth] library/node:pull token for registry-1.docker.io                                                   0.0s
 => [internal] load build context                                                                             0.0s
 => => transferring context: 10.04kB                                                                          0.0s
 => [development 1/6] FROM docker.io/library/node:12.22.4-alpine@sha256:78be4f61c7a0f00cc9da47e3ba2f1bacf9ba  0.0s
 => CACHED [development 2/6] WORKDIR /project/sawtooth-tuna/backend                                           0.0s
 => CACHED [development 3/6] COPY package*.json ./                                                            0.0s
 => CACHED [development 4/6] RUN npm install --only=development                                               0.0s
 => [development 5/6] COPY . .                                                                                0.1s
 => ERROR [development 6/6] RUN cd /project/sawtooth-tuna/backend && npm run build                            0.5s
------
 > [development 6/6] RUN cd /project/sawtooth-tuna/backend && npm run build:
#0 0.507 npm ERR! code ENOENT
#0 0.508 npm ERR! syscall open
#0 0.508 npm ERR! path /project/sawtooth-tuna/backend/package.json
#0 0.509 npm ERR! errno -2
#0 0.510 npm ERR! enoent ENOENT: no such file or directory, open '/project/sawtooth-tuna/backend/package.json'
#0 0.510 npm ERR! enoent This is related to npm not being able to find a file.
#0 0.510 npm ERR! enoent
#0 0.516
#0 0.516 npm ERR! A complete log of this run can be found in:
#0 0.516 npm ERR!     /root/.npm/_logs/2022-05-17T00_58_30_434Z-debug.log
------
failed to solve: executor failed running [/bin/sh -c cd /project/sawtooth-tuna/backend && npm run build]: exit code: 254

I have read all the available posts related to this topic. But no luck -

My docker file

# Download base image
FROM node:12.22.4-alpine As development

# Define Base Directory
WORKDIR /project/sawtooth-tuna/backend

# Copy and restore packages
COPY package*.json ./

RUN npm install --only=development

# Copy all other directories
COPY . .

# Setup base command
RUN npm run build

# # second phase
FROM node:12.22.4-alpine As production

# Declaring working directory
WORKDIR /project/sawtooth-tuna/backend

COPY package*.json ./

RUN npm install --only=production

#Copy build artifacts
COPY --from=builder /project/sawtooth-tuna/backend/dist ./
COPY --from=builder /project/sawtooth-tuna/backend/config ./config

# Start the server
CMD [ "node", "main.js" ]

As I am using docker-compose -

 tunachain-backend:
    build:
      context: .
      target: development
      dockerfile: ./backend/Dockerfile
    image: hyperledger/tunachain-backend
    container_name: tunachain-backend
    volumes:
      - .:/project/sawtooth-tuna/backend
      - /project/sawtooth-tuna/backend/node_modules
    command: npm run start:dev
    ports:
      - 3001:3001
      - 9229:9229

My project structure -

    backend -
        NestJs application code
        Dockerfile
    docker-compose
  • Any suggestion or any hint(how should I debug the issue). Fairly new with docker ---
Amjad
  • 330
  • 7
  • 22
  • You copied the package*.json to ```./``` Is that where it's supposed to be? – ewokx May 17 '22 at 01:10
  • ./ - is the working directory, right? If yes then the packag.json file should be there – Amjad May 17 '22 at 01:14
  • I'm not entirely sure if ```./``` is the working directory. Is it possible to display what ```./``` is right before that line? – ewokx May 17 '22 at 01:17
  • I am not 100% sure how to do that, when you are building an image! – Amjad May 17 '22 at 01:32
  • according to https://stackoverflow.com/questions/34213837/dockerfile-output-of-run-instruction-into-a-variable, you use ```RUN echo $(ls -1 ./)``` – ewokx May 17 '22 at 01:34
  • No, it's not working. Not printing anything – Amjad May 17 '22 at 01:55

1 Answers1

0

Here, the problem is, how and where I was copying my package*.json to the destination folder. As My docker compose file is in the root directory and my Dockerfile is in the backend folder. The copy command should look like backend/package*.json ./.

Also, for debugging the issue. I just find my last successful step image. And sh into it and tried to run the step(which was causing the problem) inside that image, to get more insights.

Hope somone in future will get some help from this.

Amjad
  • 330
  • 7
  • 22