1

I want to run a react app in a docker container with the help of a docker-compose and docker file. It is showing package.json file is missing but I do have that file in my local directory which I am trying to map with the docker container.

I have successfully built the image by running docker-compose build command. But while I am trying to run docker-compose up command it is showing below error

PS E:\Project\MyProfile\my-profile> docker-compose up   
Starting myprofile_web_1 ... done
Attaching to myprofile_web_1
web_1  | npm ERR! code ENOENT
web_1  | npm ERR! syscall open
web_1  | npm ERR! path /app/package.json
web_1  | npm ERR! errno -2
web_1  | npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
web_1  | npm ERR! enoent This is related to npm not being able to find a file.
web_1  | npm ERR! enoent
web_1  |
web_1  | npm ERR! A complete log of this run can be found in:
web_1  | npm ERR!     /root/.npm/_logs/2020-06-03T16_54_28_610Z-debug.log
myprofile_web_1 exited with code 254 

My machine is Windows 10 and let me know if you all need any further information.

My Docker File:

FROM node:alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD [ "npm","run","start" ]

Docker Compose File:

version: '3'
services: 
    web:
        build:
            context: .
            dockerfile: Dockerfile.dev
        ports: 
            - 3000:3000
        volumes: 
            - /app/node_modules
            - .:/app

UPDATE 1:

As Peter suggested I look into 'app' directory in the Docker container I can see package.json file there.

App Directory in Container

Update 2

File Permissions log

PS E:\Project\MyProfile\my-profile> docker run website ls -lah
total 724K
drwxr-xr-x    1 root     root        4.0K Jun  6 16:03 .
drwxr-xr-x    1 root     root        4.0K Jun  6 16:07 ..
-rwxr-xr-x    1 root     root          17 Jun  5 16:55 .dockerignore
drwxr-xr-x    7 root     root        4.0K Apr 20 16:02 .git
-rwxr-xr-x    1 root     root         310 Oct 26  1985 .gitignore
drwxr-xr-x    2 root     root        4.0K Apr 18 10:53 .vscode
-rwxr-xr-x    1 root     root         190 Jun  4 18:08 Dockerfile.dev
-rwxr-xr-x    1 root     root       52.6K Sep 21  2019 MyProfile.png
-rwxr-xr-x    1 root     root        2.8K Oct 26  1985 README.md
drwxr-xr-x    3 root     root        4.0K Apr 16 17:34 build
drwxr-xr-x    3 root     root        4.0K Oct  2  2019 config
-rwxr-xr-x    1 root     root         274 Jun  4 17:54 docker-compose.yml
-rwxr-xr-x    1 root     root         610 Apr 24 05:11 eg.js
drwxr-xr-x 1056 root     root       36.0K Jun  6 15:59 node_modules
-rw-r--r--    1 root     root      562.9K Jun  6 15:59 package-lock.json
-rwxr-xr-x    1 root     root        3.8K Apr  5 08:34 package.json
drwxr-xr-x    2 root     root        4.0K Apr 20 05:03 public
drwxr-xr-x    2 root     root        4.0K Oct  2  2019 scripts
drwxr-xr-x    6 root     root        4.0K Apr 15 16:44 src

Solution:

I was able to resolve this issue by moving my Project to C:/User/{User_Name} folder. Explaination for the same can be found here (Docker volumes and package.json not found)

If you do not want to move folders just like me you can do that as well checkout this (How to mount local volumes in docker machine)

Heet Shah
  • 105
  • 1
  • 3
  • 10

7 Answers7

1

Steps to do:

1. Do this (it will remove your docker volumes) in case already cached:

 docker-compose down -v --rmi "all"

And then "docker-compose up" again.

2. Another problem you might have is permissions of the file.

What user own the file "package.json" ?

Also make sure you run "docker-compose up" in the same folder you have this folder or point to this folder correctly in COPY statement ("COPY package*.json ./")

Use:

docker exec  <name of container> ls -lah

Where <name of container> is your name or container ID. you can see this if you write down the command "docker ps" it will show you a list like this bellow with this titles and below the containers active, if not empty

CONTAINER ID | IMAGE | COMMAND | CREATED | STATUS | PORTS | NAMES

to see that your package.json is not there or permission not correct for the file. Remember "docker run" create a container from a specific image. "docker exec" execute a command using a already live container.

Insted of printed you "ls" folder "ls -lah" will be more helpful.

3. Your docker-compose is first excluding node_modules then mount your root root directory (where node_modules is) try it like this below instead (switch the order):

     volumes: 
        - .:/app
        - /app/node_modules
MikZuit
  • 684
  • 5
  • 17
  • Try to run below command but getting some issue on that as well ```docker-compose down -v --rmi 'all'``` ``` PS E:\Project\MyProfile\my-profile> docker-compose down -v --rmi 'all' Removing network myprofile_default WARNING: Network myprofile_default not found. Removing image myprofile_web ERROR: Failed to remove image for service web: 409 Client Error: Conflict ("conflict: unable to remove repository reference "myprofile_web" (must force) - container 9c10fa123439 is using its referenced image 07d1f74228f1") ``` – Heet Shah Jun 03 '20 at 18:32
  • That's telling u you already remove default network , for any reason already deleted that network. anyway the only thing u need is to remove volumes so. "docker-compose down -v" and then "docker-compose up" should be enought to recreate volumes. if the problem persist, then might be permissions. let me know what happens. also check update my answer. – MikZuit Jun 03 '20 at 19:00
  • Also try step 3 and let me know your results – MikZuit Jun 04 '20 at 11:02
  • Hi @MikZuit, Can you please explain what is the in Step 2. It will be helpful if you can provide more detail on that. I ran the docker-compose up on the terminal where package.json file is present – Heet Shah Jun 04 '20 at 15:41
  • Thanks for updating the answer. How can I get the container id as docker container ls is showing none container are running. I think this makes sense because the docker-compose up is throwing error it will not run and force exit itself – Heet Shah Jun 04 '20 at 16:39
  • some comands to know: "docker-compose up" start container/services in yout yml file. so then "docker ps" or "docker container ls" will show you those containers. what error do you have on "docker-compose up"? – MikZuit Jun 04 '20 at 17:18
  • please refer to my original post. Since I'm getting an error (package.json file is not found) while docker-compose up is run. – Heet Shah Jun 04 '20 at 17:37
  • I had, since u don't provide more info. I answer u with 3 possible problems. we cannot blame docker here, npm is the one with a problem, and is because the file could not be copied , override or without proper permissions in container. – MikZuit Jun 04 '20 at 19:56
  • Hi @MikZuit, What type of permissions you are talking about? Because I'm working on a local environment and can you please tell me the steps to check the same. I am not able to proceed with Step 2 because docker compose up itself is throwing error that package.json file is not found – Heet Shah Jun 05 '20 at 17:23
  • for step 2 you don't need to "docker-compose up" , you just need your container up like this "docker build -t my-container-name ." (don't forget the dot) and then "docker run my-container-name ls -lah" with that you will be able to look your files permissions inside the container. If in local your file system use a user like "me" this is what your files will be own inside docker container. I think this is a nice reading for you to understand missing files when u use windows https://blog.gougousis.net/file-permissions-the-painful-side-of-docker/ – MikZuit Jun 05 '20 at 20:35
  • Yes @MikZuit I have the file permission issue so I moved my project for temporary to the user directory in Windows and gave necessary permission to the container. I am trying to give permission to the Linux to access my original project through GUI of Virtualbox but unfortunately, it is not working. Do you have any idea about that? – Heet Shah Jun 06 '20 at 16:17
1

I was able to resolve this issue by moving my Project to C:/User/{User_Name} folder. The explanation for the same can be found here (Docker volumes and package.json not found).

If you do not want to move folders just like me you can do that as well checkout this (How to mount local volumes in docker machine)

Heet Shah
  • 105
  • 1
  • 3
  • 10
0

Change your copy line like this

COPY package*.json .
Sam
  • 4,046
  • 8
  • 31
  • 47
0

Hello and welcome to Stack Overflow

Your line COPY package*.json ./ is copying your package.json to a file named ./

If you were to run the container with a custom entrypoint, as below you would see a file named accordingly.

You can do the below to get an interactive shell, that allows you to inspect the filesystem:

docker run -it --entrypoint /bin/sh your-image-name
Peter Reid
  • 5,139
  • 2
  • 37
  • 33
  • 1
    Thanks Peter but I could see package.json is present in 'app' directory – Heet Shah Jun 03 '20 at 18:24
  • @HeetShah Could you try remove the CMD from your dockerfile, then rebuild and re-run compose up. Then exec docker exec -it containerid /bin/sh, replacing containerid with the id sourced from a docker ps. Then do an ls -alh to confirm what you have on your file system – Peter Reid Jun 03 '20 at 19:16
  • I commented the CMD in my docker file after that should I run `docker-compose build`? – Heet Shah Jun 04 '20 at 15:20
  • Yeah should get it up and running so you can exec in :) – Peter Reid Jun 04 '20 at 15:21
  • HI Peter, Thanks for the quick reply I re-run the compose file but I am not able to see container id while I executed docker container ls command – Heet Shah Jun 04 '20 at 15:48
  • Ah interesting, if you do a `docker ps -a` does it give anything back as stopped very recently after running the `docker-compose build`? – Peter Reid Jun 04 '20 at 15:50
  • PS E:\Project\MyProfile\my-profile> docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4e9e59993b32 myprofile_web "docker-entrypoint.s…" 22 minutes ago Exited (0) 22 minutes ago myprofile_web_1 – Heet Shah Jun 04 '20 at 15:56
0

I have another approach to achieve this. in this approach you have to build your react app outside the container and pass the build to the container.

UI App Spin-up using Ngnix

docker-compose.yml

version: "3"
services:
  uiApp:
    build:
      context: .
    image: uiApp:latest
    container_name: uiApp
    volumes:
      - [ path of ui app build ]:/usr/share/nginx/html
      - ./conf:/etc/nginx/conf.d/ #map conf dir in project to ngnix container conf.d
    ports:
      - 80:80

dockerfile

FROM nginx
LABEL APP_ID="ui-App"
RUN rm /etc/nginx/conf.d/default.conf
CMD ["nginx", "-g", "daemon off;"]

ngnix.conf

location: ./conf/ngnix.conf

server {    
    listen 80;  
    server_name host.docker.internal;   

    location / {    
            root   /usr/share/nginx/html;   
            index  index.html index.htm;    
    }       
}

I know this this is not the approach you want, But this is another workaround to spinup UI applications using Ngnix

Dupinder Singh
  • 7,175
  • 6
  • 37
  • 61
  • It's actually quite a good practice to keep your build inside your docker build in the way they currently have it, rather than relying on an external tool. It anyone allows your download your code and quickly run it – Peter Reid Jun 04 '20 at 16:09
0

Another approach that you are working in build React App inside the docker container

dockerfile

FROM node:10
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "npm", "start" ]

docker-compose.yml

version: '3'
services:
  uiApp:
    build:
      context: .
    ports:
      - 4680:3000
    command: npm start

Command to build and run the container

docker-compose build uiApp
docker-compose up uiApp
Dupinder Singh
  • 7,175
  • 6
  • 37
  • 61
0

I research on your problem which was

web_1 | npm ERR! enoent ENOENT: no such file or directory

According to answers I found on GitHub threads and Stackoverflow like https://stackoverflow.com/a/52222749/5108695

If you already have package-lock.json file just delete it and try again.

So please run exec docker container and rm (Remove) package-lock.json. and then try to run your npm task

The problem comes when your docker-compose copy all the content from . location to app dir of the container.

So what you can do, remove package-lock.json every time container spin-up which is not a good practice

Use of .dockerignore

place at the same location where docker-compose and dockerfile is.


.dockerignore

#add your file which you want to ignore while docker processing
package-lock.json

Try these steps your problem will be fixed.

have any doubts please comment

Dupinder Singh
  • 7,175
  • 6
  • 37
  • 61