在Docker容器上运行的访问服务器

时间:2020-10-07 06:45:44

标签: python docker server

我正在通过Docker容器运行StanfordCoreNLP服务器。现在,我想通过我的python脚本访问它。

我要运行的Github存储库:https://github.com/swisscom/ai-research-keyphrase-extraction

我运行了以下命令:

docker run -v /home/goorulabs/NarrativeArcse-extraction/sent2vec/torontobooks_unigrams.bin:/sent2vec/pretrained_model.bin -it keyphrase-extraction
/app # cd /stanford-corenlp/
/stanford-corenlp # java -mx4g -cp "*" edu.stanford.nlp.pipeline.StanfordCoreNLPServer -preload tokenize,ssplit,pos -status_port 9000 -port 9000 -time
out 15000 &
/stanford-corenlp # [main] INFO CoreNLP - --- StanfordCoreNLPServer#main() called ---
[main] INFO CoreNLP - setting default constituency parser
[main] INFO CoreNLP - warning: cannot find edu/stanford/nlp/models/srparser/englishSR.ser.gz
[main] INFO CoreNLP - using: edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz instead
[main] INFO CoreNLP - to use shift reduce parser download English models jar from:
[main] INFO CoreNLP - http://stanfordnlp.github.io/CoreNLP/download.html
[main] INFO CoreNLP -     Threads: 4
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator tokenize
[main] INFO edu.stanford.nlp.pipeline.TokenizerAnnotator - No tokenizer type provided. Defaulting to PTBTokenizer.
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator ssplit
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator pos
[main] INFO edu.stanford.nlp.tagger.maxent.MaxentTagger - Loading POS tagger from edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger ... done [1.2 sec].
[main] INFO CoreNLP - Starting server...
[main] INFO CoreNLP - StanfordCoreNLPServer listening at /0.0.0.0:9000

现在,我尝试通过在config.ini文件中指定主机和端口来访问它,但是出现以下错误。

Traceback (most recent call last):
  File "/home/goorulabs/NarrativeArc/narrativearc/lib/python3.6/site-packages/urllib3/connection.py", line 160, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "/home/goorulabs/NarrativeArc/narrativearc/lib/python3.6/site-packages/urllib3/util/connection.py", line 84, in create_connection
    raise err
  File "/home/goorulabs/NarrativeArc/narrativearc/lib/python3.6/site-packages/urllib3/util/connection.py", line 74, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/goorulabs/NarrativeArc/narrativearc/lib/python3.6/site-packages/urllib3/connectionpool.py", line 677, in urlopen
    chunked=chunked,
  File "/home/goorulabs/NarrativeArc/narrativearc/lib/python3.6/site-packages/urllib3/connectionpool.py", line 392, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/lib/python3.6/http/client.py", line 1264, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1310, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1259, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib/python3.6/http/client.py", line 1038, in _send_output
    self.send(msg)
  File "/usr/lib/python3.6/http/client.py", line 976, in send
    self.connect()
  File "/home/goorulabs/NarrativeArc/narrativearc/lib/python3.6/site-packages/urllib3/connection.py", line 187, in connect
    conn = self._new_conn()
  File "/home/goorulabs/NarrativeArc/narrativearc/lib/python3.6/site-packages/urllib3/connection.py", line 172, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7fb5222fab70>: Failed to establish a new connection: [Errno 111] Connection refused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/goorulabs/NarrativeArc/narrativearc/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/home/goorulabs/NarrativeArc/narrativearc/lib/python3.6/site-packages/urllib3/connectionpool.py", line 727, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "/home/goorulabs/NarrativeArc/narrativearc/lib/python3.6/site-packages/urllib3/util/retry.py", line 439, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=9000): Max retries exceeded with url: /?properties=%7B%22outputFormat%22%3A+%22json%22%2C+%22annotators%22%3A+%22tokenize%2Cssplit%2Cpos%22%7D (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb5222fab70>: Failed to establish a new connection: [Errno 111] Connection refused',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 7, in <module>
    kp1 = launch.extract_keyphrases(embedding_distributor, pos_tagger, raw_text, 5,'en')
  File "/home/goorulabs/NarrativeArc/ai-research-keyphrase-extraction/launch.py", line 27, in extract_keyphrases
    tagged = ptagger.pos_tag_raw_text(raw_text)
  File "/home/goorulabs/NarrativeArc/ai-research-keyphrase-extraction/swisscom_ai/research_keyphrase/preprocessing/postagging.py", line 215, in pos_tag_raw_text
    tagged_text = list(raw_tag_text())
  File "/home/goorulabs/NarrativeArc/ai-research-keyphrase-extraction/swisscom_ai/research_keyphrase/preprocessing/postagging.py", line 211, in raw_tag_text
    tagged_data = self.parser.api_call(text, properties=properties)
  File "/home/goorulabs/NarrativeArc/narrativearc/lib/python3.6/site-packages/nltk/parse/corenlp.py", line 247, in api_call
    timeout=timeout,
  File "/home/goorulabs/NarrativeArc/narrativearc/lib/python3.6/site-packages/requests/sessions.py", line 578, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/home/goorulabs/NarrativeArc/narrativearc/lib/python3.6/site-packages/requests/sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/goorulabs/NarrativeArc/narrativearc/lib/python3.6/site-packages/requests/sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "/home/goorulabs/NarrativeArc/narrativearc/lib/python3.6/site-packages/requests/adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=9000): Max retries exceeded with url: /?properties=%7B%22outputFormat%22%3A+%22json%22%2C+%22annotators%22%3A+%22tokenize%2Cssplit%2Cpos%22%7D (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fb5222fab70>: Failed to establish a new connection: [Errno 111] Connection refused',))

docker ps显示PORTS的空白列

1 个答案:

答案 0 :(得分:1)

从日志中可以看到,您的服务正在侦听容器内的端口9000。 但是,从外部您需要更多信息才能访问它。您需要两条信息:

  1. 容器的IP地址
  2. docker将此9000导出到外部的外部端口(默认情况下docker不会导出本地打开的端口)。

要获取IP地址,您需要使用docker inspect,例如通过

docker inspect keyphrase-extraction | grep IPAddress

这应该为您显示一行,显示可以从本地主机用于寻址docker的IP地址。

关于端口,它需要显式公开。快速查看仓库中的Dockerfile并没有显示任何EXPOSE码头工人命令,因此默认情况下似乎未暴露端口9000。

您可以通过在命令中添加-p9000:9000(格式为-p HOST_PORT:CONTAINER_PORT)来公开它,如下所示:

docker run -p9000:9000 -v /home/goorulabs/NarrativeArcse-extraction/sent2vec/torontobooks_unigrams.bin:/sent2vec/pretrained_model.bin -it keyphrase-extraction    

完成所有这些操作后,可以使用以下方法测试端口是否已成功公开:

telnet 172.17.0.1 9000

假设以上是您从第一步获得的IP。