我正在尝试从DigitalOcean中的Docker Drop创建一个Docker容器。我正在尝试使用Git克隆来运行Node.js应用程序。 我收到错误COPY失败:stat /var/lib/docker/tmp/docker-builder249248811/package.json:没有这样的文件或目录,我不确定为什么。我尝试运行cd,尝试更改WORKdir,尝试更改COPY中的路径。 我正在从创建dockerfile的/ root文件夹中运行build命令。
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"]
答案 0 :(得分:0)
COPY
将文件从Docker build上下文(在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
命令都在其自己的隔离环境中运行;在每个RUN
命令的末尾,对当前工作目录或环境所做的更改都将丢失。您需要使用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"]