如何解决docker间通信-从一个docker到另一个docker的HTTP请求失败
我有三个docker容器,它们都在同一个docker网络中运行。它们都部署在Linux Ubuntu服务器上。
- Nginx是我的Web服务器/反向代理服务器
- 由gunicorn提供服务的MyDjangoApp
- tomcat提供的微服务
在Windows PC上,我可以成功访问所有Django端点。但是,其中之一失败了,并且尝试向微服务(从我的django应用程序)发出http get或http post的尝试失败了。我的http get请求(从docker 2到docker 3)看起来像这样:
if
在我的Windows PC上,我浏览到http://my_ip:9000/output_annotations/
,但失败并出现以下错误:
http:my_ip:1337/microservice/output_annotations/
当我使用邮递员从requests.exceptions.ConnectionError: HTTPConnectionPool(host='standard_core_nlp',port=9000): Max retries exceeded with url: /output_annotations (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f5ae38e3940>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution',))
获取http时,它成功了!
当我所有的docker都本地部署后,一切正常。
这是我与docker相关的文件:
docker-compose.yml
http://my_ip:9000/output_annotations
Dockerfile :
version: "3"
services:
web:
build:
context: ../..
dockerfile: docker/pb_django/Dockerfile
args:
HTTP_PROXY: http://my_proxy.com:9480
HTTPS_PROXY: http://my_proxy:9480
image: pb_django
container_name: pb_django
expose:
- "8000"
networks:
- ciee_network
nginx:
build: ./nginx
ports:
- "1337:80"
depends_on:
- web
networks:
- ciee_network
networks:
ciee_network:
external: true
nginx Dockerfile
FROM python:3.6
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ARG HTTP_PROXY
ARG HTTPS_PROXY
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
RUN mkdir /code
WORKDIR /code
ADD . /code/
RUN chmod -R 755 /code
RUN useradd -m user
RUN chmod 777 /home/user
USER user
CMD unset http_proxy
CMD unset https_proxy
CMD unset HTTP_PROXY
CMD unset HTTPS_PROXY
ENV HTTP_PROXY=
ENV HTTPS_PROXY=
CMD ["gunicorn","PB_Django.wsgi:application","--config","docker/pb_django/gunicorn.conf","--keep-alive","600"]
nginx.conf
FROM nginx:alpine
RUN rm -f /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d/default.conf
最后是我的微服务的docker-compose.yml
upstream pb_django {
server web:8000 fail_timeout=0;
}
server {
listen 80;
location / {
proxy_pass http://pb_django/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_read_timeout 10m;
proxy_connect_timeout 10m;
client_max_body_size 700m;
}
}
用于微服务的Dockerfile.ca
version: "3"
services:
web:
build:
context: ..
dockerfile: ./docker/Dockerfile.ca
image: microservice
container_name: microservice
ports:
- "9000:9000"
任何想法或建议将不胜感激!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。