In my Dockerfile in docker/my-project-node/
folder I have RUN npm i --legacy-peer-deps --only=production --no-optional
.
I want to run docker build locally and see if everything works fine.
When I run docker build -t my-project-image .
I get this npm error:
#9 0.913 npm ERR! code ENOENT
#9 0.913 npm ERR! syscall open
#9 0.914 npm ERR! path /app/package.json
#9 0.914 npm ERR! errno -2
#9 0.916 npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
#9 0.917 npm ERR! enoent This is related to npm not being able to find a file.
#9 0.917 npm ERR! enoent
Dockerfile:
FROM node:16-alpine
RUN apk update && apk add --no-cache ca-certificates bash
WORKDIR '/app'
COPY . .
RUN npm i --legacy-peer-deps --only=production --no-optional
CMD ["npm","run", "start:prod"]
What's wrong in my docker file?
CodePudding user response:
You are passing the wrong build context
to docker.
The build context
is the directory Docker starts working from and all the COPY
commands find the files from this path.
There are 2 ways to properly specify this:
Run the command from the root of your project and specify the Dockerfile path explicitly if its not on the root.
docker build -t my-web-app -f docker/my-project-node/Dockerfile
In the docker command, specify the root you want.
docker build -t my-web-app ../..
(as your root is 2 folders up)
Finally, please look at docker-compose
. All these pesky docker commands can be beautifully represented in a docker-compose file like this:
# docker-compose.yaml placed at root of the project
version: '3'
services:
web:
container_name: "example1"
build:
context: .
dockerfile: docker/my-project-node/Dockerfile
ports:
- "8000:8000"
Once the file is created, simply run docker-compose up -d
to create and run the containers, and docker-compose down
to shutdown and remove the containers :)