Docker-在服务之间共享本地存储

时间:2019-06-19 20:21:32

标签: python docker docker-compose dockerfile

我正在构建一个多容器应用程序。这是工作目录的整体视图:

MABSED/
|_ docker-compose.yml
|_ ...
|_ streamer/
|  |_ Dockerfile
|  |_ startStreaming.py
|  |_ credentials.py
|_ orchestrator/
   |_ Dockerfile
   |_ requirements.txt
   |_ tasks.py
   |_ my_sched.py
   |_ data/
   |  |_ streaming/
   |  |_ preprocessed/
   |  |_ results/
   |_ detector/
   |_ filter/
   |_ lemmatizer/

我的应用程序提供4种不同的服务:ElastisSearch容器,仪表板,从Twitter捕获推文的Streamer和执行任务并将结果保存在ElasticSearch中的Orchestrator。

此问题仅涉及两项服务,即Streamer和Orchestrator。正如我所说的,我希望这两个组件共享数据,应用于我的应用程序意味着我希望Orchestrator能够访问Streamer捕获的推文。此外,我希望将此数据存储在我的计算机本地目录MABSED/orchestrator/data/中,而不是仅存储在容器中,以防一旦停止进程后我需要访问该信息。

换句话说,我需要在执行docker-compose up时,这两个容器获取存储在MABSED/orchestrator/data/中的数据并添加相应的文件,以便在Streamer向{{1}添加新文件时} Orchestrator可以注意到这一变化,并将新文件添加到MABSED/orchestrator/data/streaming/

另外,流媒体服务运行的脚本MABSED/orchestrator/data/results/将数据保存到此相对路径startStreaming.py,该路径在本地运行正常,但我不知道它是否会在Docker容器上运行。 / p>

此刻,我的output_directory = '../orchestrator/data/streaming'看起来像这样:

docker-compose.yml

我想我需要创建一个卷来实现这一目标,但是我对Docker来说还比较陌生,我不知道如何管理它。

这是我的 Streamer Dockerfile

version: '2'

services:
  dashboard:
    build: demo-dashboard/
    ports:
     - "8080:8080"
    environment:
      - ES_ENDPOINT_EXTERNAL=http://localhost:9200
      - http.cors.enabled=true
      - http.cors.allow-origin=ES_ENDPOINT_EXTERNAL
      - http.cors.allow-headers=Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With
      - http.cors.allow-credentials=true
    volumes:
     - ./demo-dashboard:/usr/src/app
    networks:
      - dashboard-network

  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.7.0
    environment:
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - http.cors.enabled=true
      - http.cors.allow-origin=http://localhost:8080
      - http.cors.allow-headers=Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With
      - http.cors.allow-credentials=true
    ulimits:
      memlock:
        soft: -1
        hard: -1
      nofile:
        soft: 65536
        hard: 65536
    mem_limit: 1g
    cap_add:
      - IPC_LOCK
    volumes:
      - esdata1:/usr/share/elasticsearch/data
    networks:
      - dashboard-network
    ports:
      - 9200:9200

  orchestrator:
    image: orchestrator-mabsed
    build: orchestrator/
    environment:
      ES_HOST: 'elasticsearch'
    tty: true
    volumes:
      - ./orchestrator/data/:/usr/src/app/orchestrator/data

  streamer:
    image: streamer-mabsed
    build: streamer/
    tty: true
    volumes:
      - ./orchestrator/data/:/usr/src/orchestrator/data

volumes:
  esdata1:
    driver: local

networks:
  dashboard-network:
    driver: bridge

和我的 Orchestrator Dockerfile

FROM python:3.6

RUN pip3 install --user tweepy

WORKDIR /usr/src/app/
COPY startStreaming.py /usr/src/app/
COPY credentials.py /usr/src/app/

CMD python startStreaming.py

1 个答案:

答案 0 :(得分:1)

您可以与服务共享同一本地目录。
只要确保您的代码相应地引用了目录(共享路径)即可。
在这种情况下, / usr / src / app / orchestrator / data

样品:-

orchestrator:
image: orchestrator-mabsed
build: orchestrator/
environment:
  ES_HOST: 'elasticsearch'
tty: true
volumes:
  - MABSED/orchestrator/data/:/usr/src/app/orchestrator/data

streamer:
 image: streamer-mabsed
 build: streamer/
 tty: true
 volumes:
  - MABSED/orchestrator/data/:/usr/src/app/orchestrator/data