【问题标题】:Why Is This File Copied to the Root of a Docker Image Instead of the Working Directory为什么这个文件被复制到 Docker 镜像的根目录而不是工作目录
【发布时间】:2021-10-03 18:08:19
【问题描述】:

使用以下Dockerfile

FROM alpine:latest

WORKDIR /usr/src/app

COPY ["somefile", "/"]

还有build以下命令:

docker build \
--file=Dockerfile \
--no-cache=true \
--progress=plain \
--tag=someimage:sometag \
.

=>

#1 [internal] load build definition from Dockerfile
. . .
#8 naming to docker.io/someimage:sometag done
#8 DONE 0.0s

为什么在根 (/) 中找到 somefile

docker run someimage:sometag ls -altr ../../../.

#=>

total 68
. . .
-rw-r--r--    1 root     root           128 Jul 27 12:34 somefile
. . .
drwxr-xr-x    1 root     root          4096 Jul 27 12:34 ..
drwxr-xr-x    1 root     root          4096 Jul 27 12:34 .

而不是工作目录 (/usr/src/app):

docker run someimage:sometag ls -altr .

#=>

total 8
drwxr-xr-x    3 root     root          4096 Jul 27 12:34 ..
drwxr-xr-x    2 root     root          4096 Jul 27 12:34 .

【问题讨论】:

    标签: docker dockerfile docker-copy


    【解决方案1】:

    Dockerfile 中的 COPY 指令:

    . . .
    COPY ["somefile", "/"]
    . . .
    

    使用绝对路径而不是相对路径;更多关于 here.

    / 替换为./ 会将COPY somefile 替换为/usr/src/app,而不是根目录:

    FROM alpine:latest
    
    WORKDIR /usr/src/app
    
    COPY ["somefile", "./"]
    
    

    我们可以在build 使用以下图片后找到somefile

    docker run someimage:anothertag ls -altr .
    
    #=>
    
    total 16
    -rw-r--r--    1 root     root           128 Jul 27 23:45 somefile
    drwxr-xr-x    1 root     root          4096 Jul 27 23:45 ..
    drwxr-xr-x    1 root     root          4096 Jul 27 23:45 .
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-20
      • 1970-01-01
      相关资源
      最近更新 更多