Docker ADD命令执行成功但surya_models.tar.gz未出现在镜像中
问题
构建Docker镜像时,ADD surya_models.tar.gz /home/mambauser/命令执行成功,但最终镜像中找不到该压缩文件。
Dockerfile内容
FROM mambaorg/micromamba:2 USER root WORKDIR /home/mambauser/llmtools # success, this folder really exists ADD environment.yml . # success, this file really in the image ADD mypy.ini . # success, this file really in the image ADD pyproject.toml . # success, this file really in the image ADD pip.conf /home/mambauser/.config/pip/pip.conf # success, this file really in the image ADD .condarc /home/mambauser/.condarc # success, this file really in the image WORKDIR /home/mambauser ADD surya_models.tar.gz /home/mambauser/ # no error, but this file does not in the image
构建上下文文件确认
(llmtools) (base) YYY:PATH/TO/WORKSPACE$ ls -l total 1334156 -rwxrwxr-x 1 ps ps 269 7月 25 13:11 build.sh -rw-rw-r-- 1 ps ps 1075 7月 25 13:15 Dockerfile -rw-rw-r-- 1 ps ps 406 7月 25 10:57 environment.yml -rw-rw-r-- 1 ps ps 284 4月 10 19:55 mypy.ini -rw-rw-r-- 1 ps ps 61 7月 21 18:28 pip.conf -rw-rw-r-- 1 ps ps 280 4月 10 12:34 pyproject.toml -rwxrwxr-x 1 ps ps 201 7月 25 09:49 run.sh drwxrwxr-x 4 ps ps 4096 4月 10 19:40 src -rw-rw-r-- 1 ps ps 1366125260 7月 25 13:18 surya_models.tar.gz drwxrwxr-x 4 ps ps 4096 4月 11 17:29 test
构建过程日志
(llmtools) (base) YYY:PATH/TO/WORKSPACE$ ./build.sh [+] Building 9.3s (14/14) FINISHED docker:default => [internal] load .dockerignore 0.0s => => transferring context: 2B 0.0s => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 1.11kB 0.0s => [internal] load metadata for mambaorg/micromamba:2 0.0s => [internal] load build context 0.0s => => transferring context: 196B 0.0s => [1/9] FROM mambaorg/micromamba:2 0.0s => CACHED [2/9] WORKDIR /home/mambauser/llmtools 0.0s => [3/9] ADD environment.yml . 0.1s => [4/9] ADD mypy.ini . 0.1s => [5/9] ADD pyproject.toml . 0.1s => [6/9] ADD pip.conf /home/mambauser/.config/pip/pip.conf 0.1s => [7/9] ADD .condarc /home/mambauser/.condarc 0.1s => [8/9] WORKDIR /home/mambauser 0.1s => [9/9] ADD surya_models.tar.gz /home/mambauser/ 5.2s => exporting to image 3.5s => => exporting layers 3.4s => => writing image sha256:73b4a5a9a333c405f2b72d30d40ce072d7b38886eba10184c4724c886c0f5fb7 0.0s => => naming to docker.io/library/llmtools:latest
容器内文件检查结果
(llmtools) (base) YYY:PATH/TO/WORKSPACE$ docker run --rm -ti llmtools:latest sh # ls llmtools # pwd /home/mambauser # ls -la total 40 drwxrwxrwx 1 mambauser mambauser 4096 Jul 25 05:29 . drwxrwxrwx 1 root root 4096 Jul 1 16:04 .. -rwxrwxrwx 1 mambauser mambauser 220 Apr 18 22:47 .bash_logout -rwxrwxrwx 1 mambauser mambauser 3573 Jul 1 16:04 .bashrc drwxr-xr-x 3 root root 4096 Jul 25 05:29 .cache -rw-rw-r-- 1 root root 772 Jul 21 10:16 .condarc drwxr-xr-x 3 root root 4096 Jul 25 05:29 .config -rwxrwxrwx 1 mambauser mambauser 807 Apr 18 22:47 .profile drwxr-xr-x 1 root root 4096 Jul 25 05:29 llmtools #
原因
Docker的ADD命令有自动解压特性:如果源文件是.tar.gz、.tar等格式的压缩包,且目标路径是目录,会自动将压缩包内容解压到目标目录,不会保留原压缩文件。构建日志中该步骤耗时5.2秒,正是解压操作的体现,而非单纯复制文件。
解决办法
根据需求选择以下方案:
方案1:保留原压缩包
改用COPY命令,它不会自动解压压缩包,只会复制原文件到目标路径:
WORKDIR /home/mambauser COPY surya_models.tar.gz /home/mambauser/
方案2:确认解压后的内容
如果本来就需要解压后的文件,直接检查目标目录下的解压产物:
# 在容器内执行 ls /home/mambauser/ # 或者递归查找所有文件 find /home/mambauser -type f
额外方案(不推荐)
若要强制ADD保留压缩包,可指定目标路径为具体文件名(不符合ADD设计意图,仅作特殊场景备选):
ADD surya_models.tar.gz /home/mambauser/surya_models.tar.gz
内容的提问来源于stack exchange,提问作者vipcxj
相关产品推荐
相关产品推荐

