在多步骤构建中复制时,Docker构建失败

时间:2019-07-19 08:46:07

标签: docker

在我的Dockerfile中使用COPY --from=reference时出现错误。我创建了一个最小的示例:

FROM alpine AS build

FROM scratch
COPY --from=build / /

这将导致以下构建输出:

$ docker build .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM alpine AS build
 ---> b7b28af77ffe
Step 2/3 : FROM scratch
 ---> 
Step 3/3 : COPY --from=build / /
failed to copy files: failed to copy directory: Error processing tar file(exit status 1): Container ID 165578 cannot be mapped to a host ID

该版本在CI中运行良好,但在运行Ubuntu 18:04的笔记本电脑上失败。是什么原因导致此问题?

2 个答案:

答案 0 :(得分:0)

该错误表明您已在Ubuntu docker主机上启用了userns,但没有uid 165578的映射。这些映射应由/etc/subuid控制。

Docker's userns documentation包含配置此文件的更多示例。

您还可以修改源图像,查找165578拥有的所有文件,并将其更改为您期望的范围内。

答案 1 :(得分:0)

我刚遇到这个问题。我想通过多阶段构建将标准节点映像的二进制文件复制到我的映像中。

在本地工作良好。在BitBucket管道中无效。

如@BMitch所述,问题是使用userns

对于BitBucket,userns设置为100000:65536,(据我了解),这意味着“安全”用户ID必须在100000到165536之间。

您在源文件上拥有的userID不在该范围内,但这并不意味着它是userID165578。不要问我为什么,但是该userID实际上比报告的值低165536,因此165578- 100000-65536 = 42。

我要解决的方法是将源文件的user:group所有权更改为root:root,将它们复制到我的映像中,然后再设置user:group所有权(尽管在键入时,我已经还没做完,因为我还不是100%,所以有必要。)

ARG NODE_VERSION
FROM node:${NODE_VERSION}-stretch as node

# To get the files copied to the destination image in BitBucket, we need
# to set the files owner to root as BitBucket uses userns of 100000:65536.
RUN \
    chown root:root -R /usr/local/bin && \
    chown root:root -R /usr/local/lib/node_modules && \
    chown root:root -R /opt

FROM .... # my image has a load of other things in it.

# Add node - you could also add --chown=<user>:<group> to the COPY commands if you want
COPY --from=node /usr/local/bin /usr/local/bin
COPY --from=node /usr/local/lib/node_modules /usr/local/lib/node_modules
COPY --from=node /opt /opt