skip to Main Content

I have api (net-core) and react-app projects in root folder. I start dockerize my project from react application. If docker-compose and docker file in same react-app folder, dockerize works correct. But i want in future start two docker from root folder. This my folder structure:

root
      ├ client-app
      │  ├ node_modules
      │  ├ public
      │  ├ src
      │  │  ├ etc folders and files
      │  ├ Dockerfile
      │  ├ package.json
      │  └ package-lock.json
      ├ api folder
      └ docker-compose.yml

This is dockerfile:

FROM node:alpine
WORKDIR /client-app
ENV PATH="./node_modules/.bin:$PATH"
COPY ./client-app/package.json /client-app/
RUN npm install
COPY ./client-app/ /client-app/
RUN npm run build

This is docker-compose:

version: "3.4"
services:
  app:
    build:
      context: .
      dockerfile: client-app/Dockerfile
    volumes:
      - .:/client-app
    ports:
      - "3000:3000"
    image: app/react
    container_name: app_container
    command: npm start

Then i start dockerize from root folder docker-compose up:

 => exporting to image                                                                                                                           4.4s 
 => => exporting layers                                                                                                                          4.4s 
 => => writing image sha256:38172c4db5efba2b309f84f3c0b6c02e1ecbae89e611f1899205f663a805646e                                                     0.0s 
 => => naming to app/react                                                                                                                       0.0s 

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
[+] Running 2/1
 - Network larchkiktest_default     Created                                                                                                      0.0s 
 - Container app_container  Created                                                                                                              0.1s
Attaching to app_container
app_container  | npm ERR! code ENOENT
app_container  | npm ERR! syscall open
app_container  | npm ERR! path /client-app/package.json
app_container  | npm ERR! errno -2
app_container  | npm ERR! enoent ENOENT: no such file or directory, open '/client-app/package.json'
app_container  | npm ERR! enoent This is related to npm not being able to find a file.
app_container  | npm ERR! enoent
app_container  |
app_container  | npm ERR! A complete log of this run can be found in:
app_container  | npm ERR!     /root/.npm/_logs/2022-04-10T18_00_24_224Z-debug-0.log
app_container exited with code 254

If i change folders in dockerfile, like this:
COPY package.json ./ or COPY . . then docker fail install packages or build project because can’t find files. How i can fix build react app from root folder? Thank you.

2

Answers


  1. Chosen as BEST ANSWER

    I resolved error. Need to change Workdir /usr/src/app. Now my files are like this:

    FROM node:alpine
    WORKDIR /usr/src/app
    ENV PATH="./node_modules/.bin:$PATH"
    COPY ./client-app/package*.json .
    RUN npm install
    COPY ./client-app/ .
    RUN npm run build
    
    version: "3.4"
    services:
      app:
        build:
          context: .
          dockerfile: client-app/Dockerfile
        volumes:
          - ./:/app
        ports:
          - "3000:3000"
        image: app/react
        container_name: app_container
        command: npm start
    

  2. Try changing the build context for your client application:

    version: "3.4"
    services:
      app:
        build:
          context: ./client-app/
          dockerfile: client-app/Dockerfile
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search