docker-compose up:OSError:[Errno 99]无法分配请求的地址

时间:2019-04-03 22:50:00

标签: python docker ubuntu

我正在使用docker-compose up运行python文件,但是,错误信息如下:

matching_server_1  | Traceback (most recent call last):
matching_server_1  |   File "server.py", line 65, in <module>
matching_server_1  |     server = TCPServer((HOST, PORT), RequestHandler)
matching_server_1  |   File "/usr/local/lib/python3.7/socketserver.py", line 452, in __init__
matching_server_1  |     self.server_bind()
matching_server_1  |   File "/usr/local/lib/python3.7/socketserver.py", line 466, in server_bind
matching_server_1  |     self.socket.bind(self.server_address)
matching_server_1  | OSError: [Errno 99] Cannot assign requested address

但是该程序可以在没有docker的情况下在python3 server.py上正常运行。

项目的结构为:

root
   |—docker-compose.yml
   |—matching_engine
         |—Dockerfile
         |-server.py (main)
         |-.......

我的Dockerfile是:

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /src
WORKDIR /src
ADD requirements.txt /src/
RUN pip install -r requirements.txt
ADD . /src/

和我的docker-compose.yml是:

version: '2'

services:
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: passw0rd
      POSTGRES_USER: postgres
      POSTGRES_DB: EXCHANGEMACHINE
    volumes:
      - /var/lib/docker/volumes/data-volume:/var/lib/postgresql/data
    restart: always
  matching_server:
    build: ./matching_engine
    user: nobody
    command: bash -c "python3 server.py"
    volumes:
      - ./matching_engine:/src
    ports:
      - "12345:12345"
    expose:
      - "12345"
    depends_on:
      - db

,并且报告server.py中错误的代码是:

if __name__ == '__main__':
    HOST = '0.0.0.0'
    PORT = 12345
    server = TCPServer((HOST, PORT), RequestHandler)
    server.serve_forever()

在这种情况下怎么了?谢谢!

1 个答案:

答案 0 :(得分:1)

尝试添加您的matching_server服务

links: 
  - db

并删除暴露:“ 12345”