COPY package.json - Dockerfile

docker node js development
docker copy
docker nodejs
docker-compose nodejs
docker js
docker node hello world
docker yarn
how to run 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
COPY package.json ./app
RUN npm install -g
COPY server.js ./app
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

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
# please note, you already declared a WORKDIR, 
# therefore your files will be automaticaly pushed to ./app
COPY package.json ./
RUN npm install -g
COPY ./ ./ 
CMD ["npm", "start"]

Hope, that helps.

Dockerizing a Node.js web app, This is a common pattern in Dockerfiles (in all languages). The npm install step takes a long time, but you only need to run it when the package  Shouldn't that be COPY ../package*.json ./ since you your Dockerfile is located in a subfolder? – jBuchholz Aug 30 '19 at 20:42 @scorpioo590 yes, dockerfile is in a subfolder and package.json in root folder.

My suggestion, move all files to the WORKDIR than execute your npm install

FROM node:latest
RUN npm install nodemon -g
ADD . /app
RUN npm install
CMD ["npm", "start"] 

Why COPY package*.json ./ precedes COPY . .?, For example, assume we have like this Dockerfile: WORKDIR /app COPY . /app RUN npm install. And we will change source code very  The Dockerfile is the starting point for creating a Docker image. The file format provides a well-defined set of directives that allow you to copy files or folders, run commands, set environment variables, and do other tasks required to create a container image. It’s really important to craft your Dockerfile well to keep the resulting image secure, small, quick to build, and quick to update.

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!

Why copy package.json separately?, Line 3-5: Copy application to /app directory and install dependencies. If you add the package.json first and run npm install later, Docker won't  Docker’s official documentation notes that COPY should always be the go-to instruction as it is more transparent than ADD. If you need to copy from the local build context into a container, stick to using COPY. The Docker team also strongly discourages using ADD to download and copy a package from a URL.

How to Dockerize a Node.js application, json file will not be generated. # Install app dependencies # A wildcard is used to ensure both package.json AND package-lock.json are copied # where available​  Which will use the RisingStack Alpine Node.JS Docker image, copy the package.json into our container, installs dependencies, copies the source files and runs the start command as specified in the package.json. In order to install private packages, you may think that we could just add a line before we run npm install, using the ENV parameter:

Create a Dockerfile for Node.js Applications, If you had the following Dockerfile: FROM risingstack/alpine:3.3-v4.3.1-3.0.1 COPY package.json package.json RUN npm install # Add your source files COPY . As Xavier Lucas [extremely helpful] answer has stated, you cannot use COPY or ADD from a directory outside of your build context (the folder you run "docker build" from, should be the same directory as your .Dockerfile).

Docker and private modules, The first line of the Dockerfile defines what is the base image that your uncomment if using npm 5 or newer # COPY package-lock.json . Here’s another best practice adjustment: Copy your package.json and package-lock.json before you copy your code into the container. Docker will cache installed node_modules as a separate layer, then, if you change your app code and execute the build command, the node_modules will not be installed again if you did not change package.json.