为何Docker镜像未同步仓库文件修改?已试--no-cache仍无效
修改仓库文件后重新构建Docker镜像,镜像却仍使用旧版本文件。Dockerfile中包含COPY --chown=root:root <repository directory> <repository directory>指令,理论上应该复制当前版本的仓库文件,但基于该镜像创建容器时仍出现旧文件相关错误。已尝试docker build --no-cache命令,问题仍未解决。
Dockerfile内容如下:
# "tensorflow" or "tensorflow-gpu" ARG TENSORFLOW_TARGET="tensorflow" # tensorflow version (deepcell 0.12.4 requires ~=2.8.0) ARG TENSORFLOW_VERSION="2.8.4" # "steinbock" or "steinbock-cellpose" ARG STEINBOCK_TARGET="steinbock" # steinbock version (for overriding setuptools-scm) ARG STEINBOCK_VERSION # third-party software versions ARG FIXUID_VERSION="0.5.1" ARG ILASTIK_BINARY="ilastik-1.3.3post3-Linux.tar.bz2" ARG CELLPROFILER_VERSION="4.2.5" ARG CELLPROFILER_PLUGINS_VERSION="4.2.1" ARG CELLPOSE_VERSION="2.2" ########## TENSORFLOW ########## FROM --platform=linux/amd64 tensorflow/tensorflow:${TENSORFLOW_VERSION} AS tensorflow ARG TENSORFLOW_VERSION ENV DEBIAN_FRONTEND="noninteractive" PYTHONDONTWRITEBYTECODE="1" PYTHONUNBUFFERED="1" RUN apt-get update && \ apt-get install -yqq python3 python3-pip && \ apt-get clean && \ python3 -m pip install --upgrade pip && \ python3 -m pip install "tensorflow==${TENSORFLOW_VERSION}" ########## TENSORFLOW-GPU ########## FROM --platform=linux/amd64 tensorflow/tensorflow:${TENSORFLOW_VERSION}-gpu as tensorflow-gpu ARG TENSORFLOW_VERSION ENV DEBIAN_FRONTEND="noninteractive" PYTHONDONTWRITEBYTECODE="1" PYTHONUNBUFFERED="1" RUN apt-get update && \ apt-get install -yqq python3 python3-pip && \ apt-get clean && \ python3 -m pip install --upgrade pip && \ python3 -m pip install "tensorflow==${TENSORFLOW_VERSION}" ########## TENSORFLOW-MACOS ########## # As of Jan 2023, the following hypothetical options exist for running # tensorflow-enabled Linux images using Docker on arm64-based Macs: # # 1. Emulation of tensorflow/tensorflow (linux/amd64) on the client # - Very slow because of emulation and no vectorization/GPU support # - Requires a custom no-AVX wheel of tensorflow (no official wheels available) # - This approach has been confirmed to work with a custom no-AVX tensorflow wheel # from https://github.com/yaroslavvb/tensorflow-community-wheels/issues/209 # # 2. Custom tensorflow-enabled linux image for the arm64 platform: # - Unclear whether this works (tensorflow for arm64-based linux is experimental) # - Slow multi-stage build (bazel & tensorflow require cross-compiled from source) # # Note that the instructions on https://developer.apple.com/metal/tensorflow-plugin/ # are for arm64-based Mac OS only (no Linux wheels available). ########## STEINBOCK ########## FROM ${TENSORFLOW_TARGET} AS steinbock ARG STEINBOCK_VERSION ARG FIXUID_VERSION ARG ILASTIK_BINARY ARG CELLPROFILER_VERSION ARG CELLPROFILER_PLUGINS_VERSION ARG TZ="Europe/Zurich" ENV DEBIAN_FRONTEND="noninteractive" PYTHONDONTWRITEBYTECODE="1" PYTHONUNBUFFERED="1" XDG_RUNTIME_DIR="/tmp" # install system packages RUN apt-get update && \ apt-get install -yqq build-essential curl git locales python3 python3-pip python3-dev python3-venv && \ apt-get clean && \ python3 -m pip install --upgrade pip setuptools wheel # set locale and timezone RUN sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen && locale-gen ENV LANG="en_US.UTF-8" LANGUAGE="en_US:en" LC_ALL="en_US.UTF-8" RUN ln -snf "/usr/share/zoneinfo/${TZ}" /etc/localtime && echo "${TZ}" > /etc/timezone # create steinbock user RUN addgroup --gid 1000 steinbock && \ adduser --uid 1000 --ingroup steinbock --disabled-password --gecos "" steinbock # create data directory RUN mkdir /data && \ chown steinbock:steinbock /data # install fixuid ENV RUN_FIXUID=1 RUN USER=steinbock && \ GROUP=steinbock && \ curl -SsL "https://github.com/boxboat/fixuid/releases/download/v${FIXUID_VERSION}/fixuid-${FIXUID_VERSION}-linux-amd64.tar.gz" | tar -C /usr/local/bin -xzf - && \ chmod 4755 /usr/local/bin/fixuid && \ mkdir -p /etc/fixuid COPY --chown=root:root fixuid.yml /etc/fixuid/config.yml # install ilastik RUN mkdir /opt/ilastik && \ curl -SsL "https://files.ilastik.org/${ILASTIK_BINARY}" | tar -C /opt/ilastik -xjf - --strip-components=1 # install cellprofiler in cellprofiler-venv RUN python3 -m venv --system-site-packages /opt/cellprofiler-venv ENV ROOT_VENV_PATH="${PATH}" CELLPROFILER_VENV_PATH="/opt/cellprofiler-venv/bin:${PATH}" ENV PATH="${CELLPROFILER_VENV_PATH}" RUN python -m pip install --upgrade pip setuptools wheel RUN apt-get update && \ apt-get install -yqq libgtk-3-dev openjdk-11-jdk-headless libmysqlclient-dev libnotify-dev libsdl2-dev libwebkit2gtk-4.0-dev && \ apt-get clean ENV JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64" RUN curl -SsO https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-20.04/wxPython-4.1.0-cp38-cp38-linux_x86_64.whl && \ python -m pip install wxPython-4.1.0-cp38-cp38-linux_x86_64.whl && \ rm wxPython-4.1.0-cp38-cp38-linux_x86_64.whl RUN python -m pip install git+https://github.com/CellProfiler/CellProfiler.git@4.2.x RUN mkdir /opt/cellprofiler_plugins && \ curl -SsL "https://github.com/BodenmillerGroup/ImcPluginsCP/archive/refs/tags/v${CELLPROFILER_PLUGINS_VERSION}.tar.gz" | tar -C /opt/cellprofiler_plugins -xzf - "ImcPluginsCP-${CELLPROFILER_PLUGINS_VERSION}/plugins/" --strip-components=2 ENV PATH="${ROOT_VENV_PATH}" # install steinbock in steinbock-venv (including napari system dependencies, Jupyter Notebook/Jupyter Lab, pre-trained models) RUN python3 -m venv --system-site-packages /opt/steinbock-venv ENV ROOT_VENV_PATH="${PATH}" STEINBOCK_VENV_PATH="/opt/steinbock-venv/bin:${PATH}" ENV PATH="${STEINBOCK_VENV_PATH}" RUN python -m pip install --upgrade pip setuptools wheel RUN apt-get update && \ apt-get install -yqq mesa-utils libgl1-mesa-glx libglib2.0-0 libfontconfig1 libxrender1 libdbus-1-3 libxkbcommon-x11-0 libxi6 libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-randr0 libxcb-render-util0 libxcb-xinerama0 libxcb-xinput0 libxcb-xfixes0 libxcb-shape0 && \ apt-get clean COPY --chown=root:root steinbock /app/steinbock/steinbock/ COPY --chown=root:root requirements.txt requirements_test.txt conftest.py MANIFEST.in pyproject.toml setup.cfg /app/steinbock/ RUN python -m pip install -r /app/steinbock/requirements.txt && \ python -m pip install -r /app/steinbock/requirements_test.txt && \ python -m pip install jupyter jupyterlab ENV TF_CPP_MIN_LOG_LEVEL="2" NO_AT_BRIDGE="1" RUN --mount=source=.git,target=/app/steinbock/.git SETUPTOOLS_SCM_PRETEND_VERSION="${STEINBOCK_VERSION#v}" python -m pip install -e "/app/steinbock[imc,deepcell,napari]" RUN mkdir -p /opt/keras/models && \ curl -SsL https://deepcell-data.s3-us-west-1.amazonaws.com/saved-models/MultiplexSegmentation-9.tar.gz | tar -C /opt/keras/models -xzf - # configure container WORKDIR /data USER steinbock:steinbock COPY --chown=root:root entrypoint.sh /app/entrypoint.sh ENTRYPOINT ["/app/entrypoint.sh"] EXPOSE 8888
构建命令:
docker build --no-cache -t 'steinbock_xti' .
运行命令:
docker run -v <path to data>:/data --platform linux/amd64 -u $(id -u):$(id -g) -p 8888:8888 -v /tmp/.X11-unix:/tmp/.X11-unix -v ~/.Xauthority:/home/steinbock/.Xauthority:ro -e DISPLAY=host.docker.internal:0 steinbock_xti
验证COPY指令的路径与文件更新
确认本地steinbock目录下的所有文件已正确修改,且Dockerfile中的COPY --chown=root:root steinbock /app/steinbock/steinbock/路径完全匹配。若本地目录结构和容器目标路径不一致,会导致部分文件未被覆盖。可在COPY前添加RUN rm -rf /app/steinbock/steinbock/强制清空旧目录,确保新文件完全替换。排查开发模式安装的缓存问题
Dockerfile中使用pip install -e进行开发模式安装,该模式会在Python的site-packages中创建指向源码的软链接。即使COPY了新文件,若之前的软链接关联旧内容(或存在缓存),可能导致代码加载异常。解决方法:在安装前删除/app/steinbock目录,修改为:RUN rm -rf /app/steinbock COPY --chown=root:root steinbock /app/steinbock/steinbock/ COPY --chown=root:root requirements.txt requirements_test.txt conftest.py MANIFEST.in pyproject.toml setup.cfg /app/steinbock/检查
.git挂载的版本影响
构建时通过--mount=source=.git,target=/app/steinbock/.git挂载本地git目录,若本地git仓库的提交记录、标签未更新,setuptools-scm可能识别旧版本号,导致逻辑沿用旧代码。可尝试更新本地git状态,或移除该挂载指令(若不需要动态生成版本号)。确认运行时未挂载旧目录
检查运行命令中的所有-v参数,确保没有将本地旧仓库目录挂载到容器的/app/steinbock路径下——若存在此类挂载,容器会直接使用本地旧文件而非镜像内的新文件。彻底清理Docker缓存与旧镜像
除了--no-cache,执行以下命令彻底清理后重新构建:docker rmi steinbock_xti docker builder prune -af docker build --no-cache -t steinbock_xti .
内容的提问来源于stack exchange,提问作者CodeRookie

