Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

COPY package.json - Dockerfile

I did a Dockerfile to a project in Node.js but an error happened.

Here's the Dockerfile:

FROM node:latest
RUN npm install nodemon -g
WORKDIR ./app
COPY package.json ./app
RUN npm install -g
COPY server.js ./app
EXPOSE 3000
CMD ["npm", "start"] 

When I tried building this Dockerfile happen an error like this:

Step 4/8 : COPY package.json ./app
COPY failed: stat /var/lib/docker/tmp/docker-builderXXXXXXXX/package.json: no such file or directory

How can I fix it?

Docker version 17.12.0

like image 687
Welinton Ribeiro Junior Avatar asked Apr 01 '18 03:04

Welinton Ribeiro Junior


People also ask

What is Workdir in Dockerfile?

The WORKDIR command is used to define the working directory of a Docker container at any given time. The command is specified in the Dockerfile. Any RUN , CMD , ADD , COPY , or ENTRYPOINT command will be executed in the specified working directory.

What is difference between CMD and entrypoint?

Differences between CMD & ENTRYPOINT CMD commands are ignored by Daemon when there are parameters stated within the docker run command while ENTRYPOINT instructions are not ignored but instead are appended as command line parameters by treating those as arguments of the command.


2 Answers

Do not ever run nodemon in production (if that's what you tried to do). You should configure your restart in case if app crashes. Preferably, set it to always in docker-compose.yml

The best way to structure Dockerfile in your case:

FROM node:latest
WORKDIR ./app
# please note, you already declared a WORKDIR, 
# therefore your files will be automaticaly pushed to ./app
COPY package.json ./
RUN npm install -g
COPY ./ ./ 
EXPOSE 3000
CMD ["npm", "start"]

Hope, that helps.

like image 101
Sgryt87 Avatar answered Sep 24 '22 11:09

Sgryt87


Make sure you have package.json and server.js files in the same directory of your Dockerfile and it should work.

When you build a docker image, the whole content of the directory becomes your docker build context, and docker will find the files you COPY or ADD from there.

You might sometime wants to prevent some of those files to be sent to the build context, in which case you use the .dockerignore file to specify those files. Good luck!

like image 44
Christophe Schmitz Avatar answered Sep 24 '22 11:09

Christophe Schmitz