首页 > 解决方案 > 如何在配置自定义映像时将文件夹从 docker 复制到主机

问题描述

我尝试创建自己的 Docker 映像。结果,它运行后,在容器中创建了存档文件夹。我需要自动将该存档文件夹复制到我的主机。重要的是,在我的 Docker 文件中创建映像之前,我必须配置此过程。您可以在下面看到我的 Docker 文件中已有的内容:

FROM python:3.6.5-slim
RUN apt-get update && apt-get install -y gcc && apt-get autoclean -y
WORKDIR /my-tests
COPY jobfile.py testbed.yaml requirements.txt rabbit.py ./
RUN pip install --upgrade pip wheel setuptools && \
    pip install --no-cache-dir -r requirements.txt && \
    rm -v requirements.txt
VOLUME /my-tests/archive
ENV TINI_VERSION v0.18.0
ADD  https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /tini
RUN chmod +x /tini
ENTRYPOINT ["/tini","--"]
CMD ["easypy", "jobfile.py","-testbed_file","testbed.yaml"]

标签: dockerdockerfiledocker-image

解决方案


运行容器时,将主机上的任何文件夹映射到容器的存档文件夹,使用-v /usr/host_folder:/my-tests/archive. 在 /my-tests/archive 的容器内创建的任何东西现在都可以在主机上的 /usr/host_folder 中使用。

或者使用以下命令使用 scp 复制文件。您可以创建一个脚本,该脚本首先运行容器,然后运行 ​​docker exec 命令。

docker exec -it <container-name> scp /my-tests/archive <host-ip>:/host_path -r

推荐阅读