docker 中的 Apollo 客户端 uri 配置

Apollo client uri configuration inside docker

我 运行 我的三个服务在 docker 个容器中

container 1 => nginx (for routing)
container 2 => nextjs app (client)
container 3 => apollo server (on top of express)

是否可以从客户端(容器 2)直接访问服务器(容器 3)?

const client = new ApolloClient({
  uri: "http://api", // <=== this gives following error
  cache: new InMemoryCache(),
});

Error: request to http://api/ failed, reason: connect ECONNREFUSED 172.20.0.3:80

这是我的 docker-compose 文件

version: "3"

services:
  nginx:
    restart: always
    depends_on:
      - client
      - api
    build:
      dockerfile: Dockerfile.dev
      context: ./nginx
    ports:
      - "80:80"
  client:
    build:
      dockerfile: Dockerfile.dev
      context: ./client
    ports:
      - "3000:3000"
    links:
      - "api:server"
    volumes:
      - /client/node_modules
      - ./client:/client
  api:
    build:
      dockerfile: Dockerfile.dev
      context: ./server
    ports:
      - "4000:4000"
    volumes:
      - /server/node_modules
      - ./server/src:/server/src

只要api暴露一个端口就可以

错误表明 api 而不是 暴露了 http://api/

使用的默认端口 (80)

DockerCompose为api定义容器端口4000X:YY的值),并在宿主机上暴露该端口(也为4000X:YX的值)。

如果这是准确的,那么您需要将端口引用为 http://api:4000/

NOTE The ports array only defines port mappings between the host and the container but you must ensure that the container port referenced is indeed exposed by the container.