简体   繁体   English

通过docker共享数据在两个python服务器之间撰写

[英]sharing data over docker compose between two python servers

sharing a buffer of doubles between two python webservers(collector and calculator) over docker-compose 通过docker-compose在两个Python网络服务器(收集器和计算器)之间共享双打缓冲区

I am trying to simply send a buffer or an array of integers from a python server called collector to another one called calculator. 我试图简单地从称为收集器的python服务器向另一个称为计算器的python服务器发送缓冲区或整数数组。 calculator server should perfom simple mathimatical algorithim. 计算器服务器应执行简单的数学算法。 This is all a trial. 这都是试验。 collector and calculator python scripts are runned in docker-compose in two containers and designed to be connected to the same network. 收集器和计算器python脚本在docker-compose中运行在两个容器中,并设计为连接到同一网络。

collector python script 收集器python脚本


app=Flask(__name__)

@app.route('/')
def index():
    d={"my_number": list(range(10))}
    return jsonify(d)

calculator python script 计算器python脚本

import requests

r=requests.get('https://collector:5000')

app = Flask(__name__)
@app.route('/')
def index():
    numbers_array = r.json()["my_numbers"]
    x=numbers_array[1] + numbers_array[2]
    return '{}'.format(x)

docker-compose.yml 泊坞窗,compose.yml


services:
  collector:
    build: .
    env_file:
      - collector.env
    ports:
      - '5000:5000'
    volumes:
      - '.:/app'
    networks:
      - my_network



  calculator:
    build: ./calculator
    depends_on:
      - collector
    env_file:
      - calculator.env
    ports:
      - '5001:5000'
    volumes:
      - './calculator:/app'
    networks:
      - my_network

networks:
  my_network:
    driver: bridge

Dockerfile for both images is the same 两个映像的Dockerfile相同

FROM python:2.7-slim

RUN mkdir /app
WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt

COPY . .

LABEL maintainer="Mahmoud KD"

VOLUME ["/app/public"]

CMD flask run --host=0.0.0.0 --port=5000

when I run the docker-compose up --build, the first server,collector is achievable on my computer host and is working fine. 当我运行docker-compose up --build时,在我的计算机主机上可以实现第一个服务器,收集器并且工作正常。 The second server, calculator, fails to connect to collector via request.get. 第二台服务器计算器无法通过request.get连接到收集器。 I tried to ping collector from calculator container while the docker-compose is running the two containers and the ping didn't function, it says " executable file not found in PATH: unknown". 我尝试在docker-compose运行两个容器且ping无法正常运行时从计算器容器ping收集器,它说“在PATH中找不到可执行文件:未知”。 it seems that the connection of the two containers is not established although while doing inspection of my_network it shows the two containers. 似乎未建立两个容器的连接,尽管在检查my_network时会显示两个容器。 Can any body tell me what I am doing wrong. 谁能告诉我我在做什么错。 I am very grateful... 我很感激...

Use expose instead 改用expose

one app on port 5000 other on port 5001 端口5000上一个应用程序,端口5001上另一个应用程序

docker-compose: 泊坞窗-撰写:

app1: APP1:

expose:
  - 5000

app2: APP 2:

expose:
  - 5001  

make sure you run apps with ip=0.0.0.0 确保您使用ip = 0.0.0.0运行应用

If you want to access app 2 from hostmachine, forward ports 如果要从主机访问应用程序2,请转发端口

app2: APP 2:

expose:
  - 5001
ports:
  - 80:5001

Explanation: 说明:

Expose only reveales ports inside docker world. 公开仅显示docker world中的端口。 So if you expose container's A port 8888, all other containers will be able to access that container at that port. 因此,如果暴露容器的A端口8888,则所有其他容器将能够在该端口访问该容器。 But you will never reach it from host machine. 但是您永远无法从主机上访问它。

Standard procedure is that you forward only one port, that is 80 from security reasons and the rest of traffic is unreachable from outside world 标准过程是仅转发一个端口,出于安全原因,即80个端口,其余流量无法从外部访问


Also change dockerfile. 同时更改dockerfile。 You dont want hardcoded ports 您不需要硬编码的端口


Edit: 编辑:

Also get rid of this 也摆脱这个

volumes:
  - '.:/app'

It may actually cause extra troubles 实际上可能会造成额外的麻烦


Working Example: - it works, but the provided app contains errors 工作示例: -可行,但提供的应用包含错误

docker-compose.yml 泊坞窗,compose.yml

version: '3.5'

services:
  collector:
    container_name: collector
    build:
      context: collector/.
    ports:
      - '80:5555'
    expose:
      - '5555'


  calculator:
    container_name: calculator
    build:
      context: calculator/.
    depends_on:
      - collector
    expose:
      - 6666
    ports:
      - '81:6666'
    volumes:
      - './calculator:/app'

You can access both endpoints on ports 80 and 81. Communication between both endpoints are hidden from us and its on 5555 and 6666. If you close 81(or 80), you can access the other endpoint only as 'proxy' 您可以访问端口80和81上的两个终结点。两个终结点之间的通信对我们和5555和6666均不可见。如果关闭81(或80),则只能作为“代理”访问另一个终结点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM