为什么我无法将我的文件复制到 docker 容器中?

Why I'm unable to COPY my files into docker container?

我正在尝试将我的文件移动到 apache 图像的 htdocs 文件夹中。 她是我的 dockerfile 的样子:

FROM httpd:2.4
MAINTAINER Ankit
COPY ./public-html/ /usr/local/apache2/htdocs/

但是 COPY 命令没有将文件复制到容器中。 这是输出 - index.html 默认情况下存在,我要复制的是 public-html 文件夹:

root@8453b6ffa6fd:/usr/local/apache2/htdocs# ls
index.html

有什么我遗漏的吗?

更新: public-html 文件夹未被复制,但文件 index.html 被复制到那里,这种行为的原因是什么?

更新 2:这是我最新的 dockerfile、构建命令和 运行 容器。

Dockerfile:
FROM httpd:2.4
MAINTAINER Ankit
COPY public-html/ /usr/local/apache2/htdocs/public-html/

docker build -f Dockerfile -t apache .
docker run -d --name apws -v /Users/ankitsahu/workspace/docker_practi
ce/public-html:/usr/local/apache2/htdocs/ -p 80:80 apache

当我使用 exec 命令检查内容时:

root@b54e53a231b7:/usr/local/apache2/htdocs# ls -a
.  ..  index.html  sample.html  try

在将文件 COPY 保存到根以外的不同目录之前,您必须先创建它们。

将您的 Dockerfile 更新为以下内容

FROM httpd:2.4 MAINTAINER Ankit 
MKDIR -p /usr/local/apache2/htdocs/ # Create the directory with '-p' option. 
COPY ./public-html/ /usr/local/apache2/htdocs/

编辑 1

根据文档 Docker CP Command,如果

DEST_PATH exists and is a directory and if

SRC_PATH does not end with /. (that is: slash followed by dot) the source directory is copied into this directory

SRC_PATH does end with /. (that is: slash followed by dot) the content of the source directory is copied into this directory

在您的情况下 /public-html/ 确实以斜杠结尾,因此必须只复制了内容。将其更新为 ./public-html 应该可以解决问题。

希望对您有所帮助。

如果您试图将目录 public-html 复制到目录 /usr/local/apache2/htdocs 中以获得 .../htdocs/public-html/ 目录,请使用以下命令:

COPY public-html/ /usr/local/apache2/htdocs/public-html/

默认情况下,复制目录会复制该目录的内容,因此您需要在目标中为其命名,以便目录出现。


编辑:您的 运行 命令包含一个卷,将替换此目录的图像内容:

docker run -d --name apws -v /Users/ankitsahu/workspace/docker_practice/public-html:/usr/local/apache2/htdocs/ -p 80:80 apache

如果您想查看图像内部的内容,请不要使用音量:

docker run -d --name apws -p 80:80 apache

如果您想使用该卷,请在您的 docker 主机上修改 /Users/ankitsahu/workspace/docker_practice/public-html 的内容。