I am trying to dockerize an application for a project. I’m using node.js as my server. In the process of dockerizing it throws this error:
cloud_project2-projectapp-1 | npm ERR! code ENOENT
cloud_project2-projectapp-1 | npm ERR! syscall open
cloud_project2-projectapp-1 | npm ERR! path /projectapp/package.json
cloud_project2-projectapp-1 | npm ERR! errno -2
cloud_project2-projectapp-1 | npm ERR! enoent ENOENT: no such file or directory, open '/projectapp/package.json'
cloud_project2-projectapp-1 | npm ERR! enoent This is related to npm not being able to find a file.
cloud_project2-projectapp-1 | npm ERR! enoent
This is my Dockerfile
:
FROM node:18-alpine
WORKDIR /projectapp
COPY package*.json .
RUN npm ci
COPY . .
ENV PORT = 8080
EXPOSE 8080
CMD ["npm","run","devStart"]
The dockerfile is exactly as I’ve seen in many videos and in the documentation. I believe the error is thrown in the RUN npm ci command and for some reason it cannot find the package.json file even though I copy it in the above command. I also searched for some answers and maybe it’s a priviledges issue, but I’m new to linux and have a hard time understanding these.
I get a couple more errors with mysql and keyrock, but I’ll debug these later.
version: "3.9"
networks:
idm_network:
driver: bridge
#project images
services:
projectapp:
build:
context: ./projectapp
networks:
- idm_network
volumes:
- .:/projectapp #for development phase only
ports:
- "8080:8080"
mysql:
build:
context:
./mysql
networks:
- idm_network
volumes:
- project-mysql-data:/var/lib/mysql
environment:
- MYSQL_ROOT_PASSWORD=idm
- MYSQL_ROOT_HOST=%
keyrock:
image: fiware/idm:latest
networks:
- idm_network
depends_on:
- mysql
environment:
- IDM_DB_HOST=mysql
mongo-orion:
image: mongo:latest
volumes:
- project-mongo-orion-data:/data/db
networks:
- idm_network
command: --nojournal
orion:
image: fiware/orion
links:
- mongo-orion
networks:
- idm_network
command: -dbhost mongo-orion
mongo-data:
build:
context: ./mongoData
volumes:
- project-mongo-data:/data/db
networks:
- idm_network
data-storage-server:
build:
context: ./dataStorageServer
volumes:
- .:/dataserver #for development phase only
depends_on:
- mongo-data
networks:
- idm_network
#pep-proxy-datastorage:
# image: fiware/pep-proxy:latest
# networks:
# - idm_network
#pep-proxy-orion:
#image: fiware/pep-proxy:latest
#networks:
# - idm_network
#project volumes
volumes:
project-mysql-data:
project-mongo-orion-data:
project-mongo-data:
2
Answers
Problem was I had this line:
so that I don't have to restart docker every time I made a change, but it created a problem.
Delete the
volumes:
block underprojectapp
.What this block does is replace your entire application in the image with content from the host system. (Imagine installing a copy of Node normally, then separately checking out Node’s source code and unpacking it over the installed Node you already have.) There’s no particular guarantee or check that the image and the host content "match". Things like the
node_modules
tree that the imagenpm install
s get hidden as well.In your case, you’re building the image out of a
./projectapp
subdirectory, but bind-mounting the current directory.
over the image code. That causes there to not be apackage.json
when you run the container, which produces the error you see.I might make two more changes to this setup. If you delete all of the
networks:
blocks in the file, Compose will create a network nameddefault
and attach all of the containers to it; this will work exactly the same way as the network you have now, but require less setup. You can also use a shorthandbuild: directoryname
which means the same as thebuild: { context: directoryname }
you have now. That, plus deleting the unnecessaryvolumes:
, would get you down toThe
orion
service also has the obsoletelinks:
option and you can just delete that block as well (or change it todepends_on:
); the namemongo-orion
will still resolve using normal Docker networking.