Docker:本地主机上的暴露端口拒绝连接

时间:2019-06-18 22:53:39

标签: docker

我有一个Docker映像,用于处理映像,并使用curl获得最终的映像。我在Dockerfile中暴露了端口

FROM debian:latest

RUN apt-get -y update && apt-get install -y git python3-pip python3-dev python3-tk vim procps curl

#Face classificarion dependencies & web application
RUN pip3 install numpy scipy scikit-learn pillow tensorflow pandas h5py opencv-python==3.2.0.8 keras statistics pyyaml pyparsing cycler matplotlib Flask

ADD . /ekholabs/face-classifier

WORKDIR ekholabs/face-classifier

ENV PYTHONPATH=$PYTHONPATH:src
ENV FACE_CLASSIFIER_PORT=8084
EXPOSE $FACE_CLASSIFIER_PORT

ENTRYPOINT ["python3"]
CMD ["src/web/faces.py"]

一旦我建立了图像:

sudo docker build . --tag fc

我使用来运行图像

sudo docker run -d -p 8084:8084 --name=face-classification fc

一旦这样做,我首先尝试卷曲输出:

sudo curl -v -F image=@smile.png http://localhost:8084/classifyImage > output.txt

并收到连接被拒绝的错误:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0*   Trying 127.0.0.1...
* connect to 127.0.0.1 port 8084 failed: Connection refused
* Failed to connect to localhost port 8084: Connection refused
* Closing connection 0
curl: (7) Failed to connect to localhost port 8084: Connection refused

这时我不确定连接是否不正确,所以我尝试对本地主机进行常规卷曲:

sudo curl http://localhost:8084/

并出现以下错误:

curl: (7) Failed to connect to localhost port 8084: Connection refused

这时,我检查是否暴露了docker端口。由于端口中未列出任何内容,因此从此输出看起来好像没有公开。

ubuntu@ip-172-31-6-79:~/face_classification$ sudo docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                     PORTS               NAMES
2e343778c8fc        fc                  "python3 src/web/fac…"   8 minutes ago       Exited (1) 8 minutes ago                       face-classification

如何正确建立连接?

1 个答案:

答案 0 :(得分:0)

您的容器状态为EXITED(您需要检查日志docker logs container_name),或者您可能以错误的方式使用ENTRYPOINT和CMD试试:

  

CMD [“ python3”,“ src / web / faces.py”]

希望有帮助。