Docker - 将http请求从一个容器发送到另一个容器

Docker  - 将http请求从一个容器发送到另一个容器

问题描述:

当我在AWS服务器生产上运行应用程序时,我无法向后端容器发送HTTP请求。但是,当我在本地运行应用程序时,我可以请求后端很好。对于请求,我使用fetch:

I cannot send an HTTP request to backend container when I'm running app on AWS server production. However, when I'm running app locally I can make requests to backend just fine. For making requests I use fetch:

fetch('http://localhost:8000/something')

这是项目结构如下所示:

Here is how project structure looks like:

.
├── docker-compose.yml
|
├── backend
│   ├── Dockerfile
│   └── server.js
|
└── frontend
    ├── Dockerfile
    ├── package.json
    ├── public
    │   └── index.html
    └── src
       ├── components
       ├── data
       ├── index.js
       ├── routes.js
       ├── static
       ├── tests
       └── views

docker-compose.yml: p>

docker-compose.yml:

version: '3'

services:
  frontend:
    build:
      context: .
      dockerfile: frontend/Dockerfile
    volumes:
      - ./frontend:/frontend
    ports:
      - "80:5000"
    links:
      - backend
  backend:
    build:
      context: .
      dockerfile: backend/Dockerfile
    volumes:
      - ./backend:/backend
    ports:
      - "8000:8000"

前端Dockerfile:

Dockerfile in frontend:

FROM node:latest

RUN mkdir -p /frontend

WORKDIR /frontend

ADD . /frontend

VOLUME ["/frontend"]

EXPOSE 5000

CMD yarn && yarn build && yarn global add serve && serve -s build

后端的Docker文件:

Dockerfile in backend:

FROM node:latest

RUN mkdir -p /backend

WORKDIR /backend

ADD . /backend

VOLUME ["/backend"]

EXPOSE 8000

CMD yarn && yarn start

有人可以解释我的配置有什么问题吗?我很困惑,因为它在本地没有任何问题。

Can someone explain me what is wrong with my config? I'm very confused, because it works without any issues locally.

TLDR:需要更改前端代码调用当前的主机而不是'localhost'

问题是你的应用程序是说嘿localhost,而不是嘿VPS ip,当访问从你的浏览器。您需要编辑您的前端代码才能访问您正在访问的当前主机。这就是为什么你在你的本地服务器上收到请求。

The problem is your app is saying 'hey localhost' instead of 'hey VPS ip', when visiting from YOUR browser. You need to edit your frontend code to visit the current host you're visiting. That's why you're receiving a request on YOUR localhost server.

而不是 fetch(http:/// localhost:8000 / something )将其更改为 fetch(http://+ location.host +:8000)(有更好的方法,这得到它完成)。

Instead of fetch("http:///localhost:8000/something") change it to fetch("http://"+location.host+":8000") (There are better ways, this gets it done).

还要注意,docker容器在网络方面也有所不同。 docker容器并没有真正具有'localhost'的概念,就像非docker容器应用程序一样。在从服务器到服务器进行呼叫时,必须使用VPS的IP /本地IP。我使用的一个技巧是使用docker的默认docker0桥172.17.0.1。

Also note docker containers are a little different in terms of networking as well. A docker container doesn't really have a concept of 'localhost' the same way non docker container apps do. You have to use the VPS's IP/Local IP when making the call from server to server. A trick I use is to use docker's default docker0 bridge 172.17.0.1.

我倾向于使用网络超过'链接',实际上不能完全评论它,但是当容器在同一个码头网络上,您可以使用容器的名称访问另一个容器。这仅适用于服务器端代码,即:node.js server - > node.js server / mongo db。示例mongodb连接将是 mongodb:// mongo_server:27017 / mydatabase ,mongo_server将解析为容器的IP。

I tend to use networks over 'links' and actually cant comment fully on it, but when containers were on the same docker network, you could access the other container by using the container's name. This only works for server side code however, ie: node.js server -> node.js server/mongo db. Example mongodb connection would be mongodb://mongo_server:27017/mydatabase and mongo_server would resolve to the container's IP.

尝试使用IP时可能遇到的另一件事是您的防火墙,您也必须通过防火墙允许特定的ip /端口。

Another thing you'll possibly encounter when attempting to use the IP is your firewall, you would have to allow that particular ip/port in through your firewall as well.