【问题标题】:docker copy file from one container to another?docker将文件从一个容器复制到另一个容器?
【发布时间】:2016-08-20 10:15:37
【问题描述】:

这是我想做的:

  1. docker-compose 构建
  2. docker-compose $COMPOSE_ARGS 运行 --rm task1
  3. docker-compose $COMPOSE_ARGS 运行 --rm task2
  4. docker-compose $COMPOSE_ARGS 运行 --rm combine-both-task2
  5. docker-compose $COMPOSE_ARGS 运行 --rm selenium-test

还有一个看起来像这样的 docker-compose.yml:

task1:
  build: ./task1
  volumes_from:
    - task1_output
  command: ./task1.sh

task1_output:
  image: alpine:3.3
  volumes:
    - /root/app/dist
  command: /bin/sh

# essentially I want to copy task1 output into task2 because they each use different images and use different tech stacks... 
task2:
  build: ../task2
  volumes_from:
    - task2_output
    - task1_output:ro
  command: /bin/bash -cx "mkdir -p task1 && cp -R /root/app/dist/* ."

所以现在所有必需的文件都在 task2 容器中...我将如何启动 Web 服务器并使用 task2 中的内容公开端口?

我被困在这里...如何从我的 combine-tasks/Dockerfile 中的 task2_output 访问内容:

combine-both-task2:
  build: ../combine-tasks
  volumes_from:
     - task2_output

【问题讨论】:

    标签: docker docker-compose dockerfile


    【解决方案1】:

    在最新版本的 docker 中,named volumes replace data containers 是在容器之间共享数据的简便方法。

    docker volume create --name myshare
    docker run -v myshare:/shared task1
    docker run -v myshare:/shared -p 8080:8080 task2
    ...
    

    这些命令将设置一个本地卷,-v myshare:/shared 参数将使该共享作为每个容器内的文件夹 /shared 可用。

    在撰写文件中表达:

    version: '2'
    services:
      task1:
        build: ./task1
      volumes:
        - 'myshare:/shared'
    
      task2:
        build: ./task2
      ports:
        - '8080:8080'
      volumes:
        - 'myshare:/shared'
    
    volumes:
      myshare:
        driver: local 
    

    为了测试这一点,我做了一个小项目:

    - docker-compose.yml (above)
    - task1/Dockerfile
    - task1/app.py
    - task2/Dockerfile
    

    我使用 node 的 http-server 作为 task2/Dockerfile:

    FROM node
    RUN npm install -g http-server
    WORKDIR /shared
    CMD http-server
    

    task1/Dockerfile 使用python:alpine,显示两个不同的堆栈写入和读取。

    FROM python:alpine
    WORKDIR /app
    COPY . .
    CMD python app.py
    

    这里是task1/app.py

    import time
    
    count = 0
    while True:
      fname = '/shared/{}.txt'.format(count)
      with open(fname, 'w') as f:
        f.write('content {}'.format(count))
        count = count + 1
      time.sleep(10)
    

    获取这四个文件,并通过 docker compose up 在具有 docker-compose.yml 的目录中运行它们 - 然后访问 $DOCKER_HOST:8080 以查看稳定更新的文件列表。

    另外,我正在使用 docker 版本 1.12.0 和 compose 版本 1.8.0 但这应该适用于几个版本。

    请务必查看 docker 文档了解我可能在这里错过的详细信息:
    https://docs.docker.com/engine/tutorials/dockervolumes/

    【讨论】:

    • 我尝试了您的示例,让 task1 创建 hello.txt,然后尝试在 task2 中找到该文件,输出如下: 步骤 1:FROM ubuntu ---> f8d79ba03c00 步骤 2:运行 echo " hello" > hello.txt ---> 使用缓存 ---> 3f2e4e8df3ec 成功构建 3f2e4e8df3ec 构建 task2 步骤 1:FROM ubuntu ---> f8d79ba03c00 步骤 2:WORKDIR /shared ---> 使用缓存 ---> bc2f077838ce 步骤3:运行 ls hello.txt ---> 在 6b6e1eb63f7f 中运行 ls:无法访问 'hello.txt':没有这样的文件或目录错误:服务 'task2' 构建失败:命令 '/bin/sh -c ls hello .txt'
    • 混淆可能在于构建时间与运行时间。卷共享仅在docker run 期间存在,docker build 期间不存在。所以RUN ls hello.txt 没有连接到共享读取,RUN echo "hello" > hello.txt 只在本地写入第一个图像。我将添加完整的 python dockerfile,以便您可以复制所有示例。
    • 谢谢杰特。我认为您是绝对正确的,这是构建时间与运行时间。本质上,我希望 task1 生成一些人工制品,例如 npm run dist (生成 js、图像等文件),然后 task2 将这些文件复制到它的图像中,然后启动 tomcat。然后 task3 对 task2 运行 selenium。感谢您迄今为止的帮助
    • 很高兴有帮助。听起来下一步是制作一个对卷进行任何读取或写入的脚本,并使这些脚本成为容器的CMD 指令。 app.pyhttp-server 在我的示例中以这种方式使用音量。
    【解决方案2】:

    对我来说,从容器复制文件或将文件复制到容器的最佳方法是使用docker cp,例如: 如果您想将 apacheNutch 容器中的 schema.xml 复制到 solr 容器,则:

    1. docker cp apacheNutch:/root/nutch/conf/schema.xml /tmp/schema.xml 服务器/solr/configsets/nutch/

    2. docker cp /tmp/schema.xml solr:/opt/solr-8.1.1/server/solr/configsets/nutch/conf

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      • 1970-01-01
      • 2021-03-11
      • 2017-09-09
      • 1970-01-01
      • 2020-03-01
      相关资源
      最近更新 更多