Docker 构建无法为 COPY 命令找到源文件夹
Docker build fails to find source folder for COPY command
我有一个多模块 maven 项目,我想为每个模块创建和发布一个映像,但公共实用模块除外,它是所有其他模块的依赖项。
项目结构是这样的:
project_root
common_module
-pom.xml
module_a
-pom.xml
-Dockerfile
module_b
-pom.xml
-Dockerfile
-pom.xml
-docker-compose.yml
要构建 module_a fox 示例,我需要将 common_module 文件夹也复制到容器中,然后先将 运行 mvn install
复制到公共文件夹中。如果构建成功,我也可以在 module_a pom.xml
上 运行 mvn install
。
我是 Docker 的新手,这就是我一直在尝试的方法,根据 documentation of the COPY command,我认为应该可行,其中指出:
Multiple src resources may be specified but the paths of files and directories will be interpreted as relative to the source of the context of the build.
我在 module_a 中有以下 Docker 文件:
FROM openjdk:8-jre-alpine
# Install Maven
# (skipped for brevity)
# Create the project root
RUN mkdir -p /usr/src/project_root
WORKDIR /usr/src/project_root
# Copy all the necessary files for the packaging
# Copy the common module
COPY common_module /usr/src/project_root/common_module
# Copy the service registry module (src + config folder)
COPY module_a /usr/src/project_root/module_a
# Run maven install
RUN cd common_module
RUN mvn clean install
RUN cd ../module_a
RUN mvn clean install
然后我从 project_root 文件夹 发出此命令 (使其成为构建的上下文?):
docker build -t image_name:4.0 ./module_a
构建在第一个 COPY
命令处失败,说明:
COPY 失败:stat /var/lib/docker/tmp/docker-builder380120612/common_module:没有那个文件或目录
我什至不确定 RUN
命令是否有效,因为构建在此之前就失败了。
我做错了什么? 如何在 Docker 文件位置之外复制 folders/files?
顺便说一句,我正在 运行宁 Docker 版本 18.03.1-ce,构建 9ee9f40,在 Windows 10 Pro 上。
当你运行命令
docker build -t image_name:4.0 ./module_a
您将构建上下文定义为 module_a
目录。然后你尝试 COPY
目录 common_module
进入你的容器,但这是不可能的,因为它不在构建上下文中。
改为使用此命令:
docker build -t image_name:4.0 -f ./module_a/Dockerfile .
这样 module_a
、common_module
和 module_b
将包含在构建上下文中。
我有一个多模块 maven 项目,我想为每个模块创建和发布一个映像,但公共实用模块除外,它是所有其他模块的依赖项。
项目结构是这样的:
project_root
common_module
-pom.xml
module_a
-pom.xml
-Dockerfile
module_b
-pom.xml
-Dockerfile
-pom.xml
-docker-compose.yml
要构建 module_a fox 示例,我需要将 common_module 文件夹也复制到容器中,然后先将 运行 mvn install
复制到公共文件夹中。如果构建成功,我也可以在 module_a pom.xml
上 运行 mvn install
。
我是 Docker 的新手,这就是我一直在尝试的方法,根据 documentation of the COPY command,我认为应该可行,其中指出:
Multiple src resources may be specified but the paths of files and directories will be interpreted as relative to the source of the context of the build.
我在 module_a 中有以下 Docker 文件:
FROM openjdk:8-jre-alpine
# Install Maven
# (skipped for brevity)
# Create the project root
RUN mkdir -p /usr/src/project_root
WORKDIR /usr/src/project_root
# Copy all the necessary files for the packaging
# Copy the common module
COPY common_module /usr/src/project_root/common_module
# Copy the service registry module (src + config folder)
COPY module_a /usr/src/project_root/module_a
# Run maven install
RUN cd common_module
RUN mvn clean install
RUN cd ../module_a
RUN mvn clean install
然后我从 project_root 文件夹 发出此命令 (使其成为构建的上下文?):
docker build -t image_name:4.0 ./module_a
构建在第一个 COPY
命令处失败,说明:
COPY 失败:stat /var/lib/docker/tmp/docker-builder380120612/common_module:没有那个文件或目录
我什至不确定 RUN
命令是否有效,因为构建在此之前就失败了。
我做错了什么? 如何在 Docker 文件位置之外复制 folders/files?
顺便说一句,我正在 运行宁 Docker 版本 18.03.1-ce,构建 9ee9f40,在 Windows 10 Pro 上。
当你运行命令
docker build -t image_name:4.0 ./module_a
您将构建上下文定义为 module_a
目录。然后你尝试 COPY
目录 common_module
进入你的容器,但这是不可能的,因为它不在构建上下文中。
改为使用此命令:
docker build -t image_name:4.0 -f ./module_a/Dockerfile .
这样 module_a
、common_module
和 module_b
将包含在构建上下文中。