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

标签 python docker docker-compose dockerfile

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

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 将新文件添加到 MABSED/orchestrator/data/streaming/ 时,Orchestrator 可以注意到此更改并将新文件添加到 MABSED/orchestrator/data/results/.

还有 startStreaming.py,这是 Streamer 服务运行的脚本,将数据保存到此相对路径 output_directory = '../orchestrator/data/streaming',这在本地运行良好,但我不知道它是否适用于 Docker 容器。

此刻,我的 docker-compose.yml 看起来像这样:

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

我想我需要创建一个卷才能实现这一点,但我对 Docker 比较陌生,我不知道如何管理它。

这是我的 Streamer 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

和我的Orchestrator Dockerfile:

FROM python:3.6

COPY . /usr/src/app/
WORKDIR /usr/src/app/
RUN pip3 install --user -r requirements.txt

CMD python my_sched.py

最佳答案

您可以与您的服务共享同一个本地目录。
只需确保您的代码相应地引用目录(共享路径)即可。
在这种情况下,/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

关于python - Docker - 在服务之间共享本地存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56675289/

相关文章:

python - 将 cookie 字符串转换为 Python 字典

python - 如何使用 UNION 语句在 python 中合并两个 SQLite 表(当前收到 "ValueError: parameters are of unsupported type"错误消息)

python - 如何永久添加 Python 导入路径?

Docker 容器无法 curl,SSL 版本号错误

azure - Docker 构建失败并出现 "Service failed to build: COPY failed: no such file or directory"错误

python - 如何在Redis列表中有效存储16位整数

linux - 允许容器监听 80 端口的副作用

bash - Docker容器看不到串口设备

node.js - ECONNREFUSED在Docker容器内部;访问远程API

ubuntu - 在 windows ubuntu 子系统上运行 docker compose