简体   繁体   English

在 docker-compose 内使用 nginx 托管 web 页面:无法解析主机名

[英]Hosting web page with nginx inside docker-compose: Can't resolve hostname

My setup is the following:我的设置如下:

  1. Backend: Flask server running on port 5000;后端:Flask 服务器运行在 5000 端口; implemented in python在 python 中实现
  2. Frontend: React app sending http-requests to backend前端:React 应用程序向后端发送 http 请求
  3. Both are dockerized and running on the same node using docker-compose两者都使用 docker-compose 进行了 dockerized 并在同一节点上运行

My docker-compose file:我的 docker-compose 文件:

version: "3"
services:
  backend:
    ports:
      - "5000:5000"
    image: <backend-image>

  frontend:
    depends_on:
      - backend
    ports:
      - "80:80"
    links:
      - "backend:backend-python"
    image: <frontend-image>

The nginx.conf nginx.conf

server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri /index.html;                 
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

From the frontend I send requests to http://backend-python:5000/<my-endpoint> .我从前端向http://backend-python:5000/<my-endpoint>发送请求。 backend-python is defined inside the frontend specification in docker-compose as hostname of the backend. backend-python在 docker-compose 的前端规范中定义为后端的主机名。 When I run docker-compose up locally and open localhost in a browser the page gets displayed as expected.当我在本地运行docker-compose up并在浏览器中打开localhost时,页面会按预期显示。 When I run docker-compose up on a Raspberry Pi 3 the frontend can't resolve http://backend-python:5000/<my-endpoint> when I open the page locally on my laptop.当我在 Raspberry Pi 3 上运行docker-compose up ,当我在笔记本电脑上本地打开页面时,前端无法解析http://backend-python:5000/<my-endpoint>

I also tried to replace nginx with serving the build using serve .我还尝试将nginx替换为使用serve服务构建。 This leads to the same behaviour as described above.这导致与上述相同的行为。

From googling it seems to me that using docker-compose builds its own network in which each container can be found by its name or names defined under links .从谷歌搜索来看,在我看来,使用 docker-compose 构建了自己的网络,其中每个容器都可以通过其名称或在links下定义的名称找到。

Has anybody an idea why the name can't be resolved when accessing the page from an other machine?有人知道为什么从其他机器访问页面时无法解析名称吗?

A frontend app is running inside a browser and when the frontend app try to reach an API, it's the browser that try to communicate with it.前端应用程序在浏览器中运行,当前端应用程序尝试访问 API 时,是浏览器尝试与之通信。

When you host a frontend app inside docker with nginx, all the container do is to server the static file needed by the browser, but it's not nginx that try to communicate with your API. When you host a frontend app inside docker with nginx, all the container do is to server the static file needed by the browser, but it's not nginx that try to communicate with your API.

It's why your browser can't accès to your backend using the container name, you need to expose your API and change the API/backend URL in your frontend app to use http://localhost:5000 .这就是为什么您的浏览器无法使用容器名称访问您的后端的原因,您需要公开您的 API 并在您的前端应用程序中更改 API/后端 URL 以使用http://localhost:5000

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

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