Getting COPY Failed:尝试在DigitalOcean Droplet上的Docker中复制packag



我正试图在DigitalOcean中用docker液滴创建一个docker容器。我正在尝试使用Git克隆来运行Node.js应用程序。我得到错误COPY failed:stat/var/lib/doker/tmp/doker-builder249248811/package.json:没有这样的文件或目录,我不知道为什么。我试过运行cd,试过更改WORKdir,试过在COPY中更改路径。我正在/root文件夹中运行build命令,我在该文件夹中创建了dockerfile。

FROM node:12.18.3
#RUN mkdir /root/live 
#  cd  /root/live 
RUN  git clone https://username:password@github.com/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"]

COPY将Docker构建上下文(docker build命令中命名的目录,通常是包含Dockerfile的目录(中的文件复制到映像中。它不会复制图像中的文件。

您通常希望在Docker之外运行此git clone命令。这有几个原因:它允许您将Dockerfile检查到存储库中;它可以让你从你还没有承诺的事情中建立一个形象;Docker层缓存不会阻止您看到源存储库中的更改;您不希望您的GitHub凭据在docker history中以纯文本显示。

我推荐这个(相当样板(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"]

您在这里遇到的另一个问题是,每个RUN命令都在自己的隔离环境中运行;对当前工作目录或环境的更改在每个CCD_ 6命令结束时丢失。您需要使用WORKDIR指令来更改目录,而不是RUN cd ...

如果你想坚持在Dockerfile中运行git clone,你需要将WORKDIR更改为已签出的内容,但一旦完成,所有文件都在那里,你不需要COPY任何内容(或RUN cp ...在图像中移动它们(。

FROM node:12.18.3
WORKDIR /
# Remember, `docker history` will show this line exactly as here,
# including the credentials.
RUN git clone https://username:password@github.com/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"]

相关内容

最新更新