加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

docker从一个容器复制文件到另一个容器?

发布时间:2020-12-16 03:43:34 所属栏目:安全 来源:网络整理
导读:这是我想要做的: docker-compose build docker-compose $COMPOSE_ARGS运行rm task1 docker-compose $COMPOSE_ARGS运行rm task2 docker-compose $COMPOSE_ARGS运行rm combine-both-task2 docker-compose $COMPOSE_ARGS运行rm selenium-test 还有一个看起来像

这是我想要做的:

> docker-compose build
> docker-compose $COMPOSE_ARGS运行–rm task1
> docker-compose $COMPOSE_ARGS运行–rm task2
> docker-compose $COMPOSE_ARGS运行–rm combine-both-task2
> 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中,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参数将使该共享可用作每个容器内的文件夹/共享.

要在撰写文件中表达:

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.yml在目录中运行它们 – 然后访问$DOCKER_HOST:8080以查看稳定更新的文件列表.

此外,我正在使用docker版本1.12.0并编写版本1.8.0但这应该适用于几个版本.

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读