augdog97
augdog97

Reputation: 267

Getting COPY Failed: stat when trying to copy package.json file in Docker on DigitalOcean Droplet

I am trying to create a docker container off of a Docker droplet in DigitalOcean. I am trying to use Git clone to run a Node.js app. I am getting the error COPY failed: stat /var/lib/docker/tmp/docker-builder249248811/package.json: no such file or directory and I am not sure why. I have tried to RUN cd, tried changing the WORKdir, tried changing the path in the COPY. I am running the build command from the /root folder which is where I have created the dockerfile.

FROM node:12.18.3
 #RUN mkdir /root/live \
  #  cd  /root/live \

RUN  git clone https://username:[email protected]/username/Portfolio.git
WORKDIR .
RUN cd  /Portfolio
COPY  package.json /Portfolio
COPY package-lock.json . /Portfolio
COPY . .
RUN npm install

EXPOSE 8080

CMD ["pm2", "start", "./bin/ww"]

Upvotes: 0

Views: 4482

Answers (1)

David Maze
David Maze

Reputation: 159040

COPY copies files from the Docker build context (the directory named in the docker build command, frequently the directory containing the Dockerfile) into the image. It doesn't copy files within an image.

You'll typically want to run this git clone command outside of Docker. There are a couple of reasons for this: it lets you check the Dockerfile into the repository; it lets you build an image out of something you haven't committed yet; Docker layer caching doesn't prevent you from seeing changes in the source repository; you don't want your GitHub credentials from being visible in plain text in docker history.

I'd recommend this (fairly boilerplate) Dockerfile:

FROM node:12.18.3

# This also creates the directory if it doesn't exist.
# (This frequently is "/app".)
WORKDIR /Portfolio

# We ran `git clone` on the host outside of Docker.
# Copy in only the files we need to install dependencies.
COPY package*.json ./
RUN npm install

# Now copy in the rest of the application.
# (`node_modules` should be included in `.dockerignore`.)
COPY . ./
# RUN npm build

# Standard metadata to start the application.
EXPOSE 8080
CMD ["pm2", "start", "./bin/ww"]
# (Do you actually need a process manager inside a Docker container?)
# CMD ["node", "./bin/ww"]

The other problem you're encountering here is that each RUN command runs in its own isolated environment; changes to the current working directory or to the environment are lost at the end of each RUN command. You need to use the WORKDIR directive to change directories, not RUN cd ....

If you wanted to stick with running git clone inside the Dockerfile, you need to change the WORKDIR to what got checked out, but once you've done this, all the files are there and you don't need to COPY anything (or RUN cp ... to move them around within the image).

FROM node:12.18.3
WORKDIR /
# Remember, `docker history` will show this line exactly as here,
# including the credentials.
RUN git clone https://username:[email protected]/username/Portfolio.git

# Change directories into what got checked out.
WORKDIR /Portfolio
# All of the files are already there, so we only need to
RUN npm install
EXPOSE 8080
CMD ["pm2", "start", "./bin/ww"]

Upvotes: 0

Related Questions