docker build no such file or directory

在我构建新的镜像的时候, 发生 了  no such file or directory 的错误。  这个错误找了半天, 没头绪, 后来灵光一现, 原来是我的文件夹名字写错了

我的目录结构是这样的

[root@host sample]# find ./ -type f
./enginx/global.conf
./enginx/nginx.conf
./Dockerfile
[root@host sample]# find ./ -type d
./
./enginx
[root@host sample]# 

sample 文件夹中有一个Dockerfile 文件和一个nginx文件夹, nginx文件夹中有global.conf  和 nginx.conf

cd 进 sample   下 使用如下命令构建镜像

[root@host sample]# docker build -t addictions/nginx .

后面的“.”  表示当前目录, 会从当前目录去找Dockfile文件, 当前目录会被上传到docker进程作为构建上下文目录,  只有构建上下文目录的文件才可以被ADD  或者 COPY 到镜像中。 

[root@host sample]# docker build -t addictions/nginx .
Sending build context to Docker daemon 17.41 kB
Sending build context to Docker daemon 
Step 0 : FROM ubuntu
---> 70d53b6cf65a
Step 1 : MAINTAINER jxl "2013143154@qq.com"
---> Using cache
---> 0be2be8c6d47
Step 2 : ENV REFRESHED_AT 2018-10-21
---> Using cache
---> 48ead1b5bfb6
Step 3 : RUN apt-get -yqq update && apt-get -yqq install nginx
---> Using cache
---> 3c834a6164b3
Step 4 : RUN mkdir -p /var/www/html/website
---> Using cache
---> 5044773f5e09
Step 5 : ADD nginx/global.conf /etc/nginx/conf.d/
nginx/global.conf: no such file or directory

以上是我报的错, 原因是我我在Dockerfie中的ADD 命令中的文件夹名字写成了nginx,  而构建上下文目录中的文件夹是enginx。  不一致, 所以在上下文目录中找不到这个文件夹, 自然也找不到文件夹下的目录

解决方案:  将构建上下文目录中的enginx 改成 nginx 即可, 或者将Dockerfile中的ADD 命令后面的nginx 改成enginx,   以下是构建成功之后的输出

[root@host sample]# docker build -t addictions/nginx .
Sending build context to Docker daemon 4.608 kB
Sending build context to Docker daemon 
Step 0 : FROM ubuntu
 ---> 70d53b6cf65a
Step 1 : MAINTAINER jxl "2013143154@qq.com"
 ---> Using cache
 ---> 0be2be8c6d47
Step 2 : ENV REFRESHED_AT 2018-10-21
 ---> Using cache
 ---> 48ead1b5bfb6
Step 3 : RUN apt-get -yqq update && apt-get -yqq install nginx
 ---> Using cache
 ---> 3c834a6164b3
Step 4 : RUN mkdir -p /var/www/html/website
 ---> Using cache
 ---> 5044773f5e09
Step 5 : ADD nginx/global.conf /etc/nginx/conf.d/
 ---> cfee19df85f2
Removing intermediate container 0eddbc45f54d
Step 6 : ADD nginx/nginx.conf /etc/nginx/nginx.conf
 ---> dbf25a20d66d
Removing intermediate container 8fbdd8c0eb26
Step 7 : EXPOSE 80
 ---> Running in 0e4ef7402519
 ---> 2b909217eb99
Removing intermediate container 0e4ef7402519
Successfully built 2b909217eb99
[root@host sample]# 
原文地址:https://www.cnblogs.com/jxlsblog/p/9826974.html