【问题标题】:Accessing a docker container's IP address from a static client从静态客户端访问 docker 容器的 IP 地址
【发布时间】:2020-09-25 08:25:07
【问题描述】:

我有一个静态 Gatsby 应用程序,它需要来自另一个容器的 uri 来进行 Hasura GraphQL 连接。

问题

Gatsby 容器在 Hasura 之前完成了 docker 构建,因此 Gatsby 中的 URI 设置为 undefined

如何使 uri 成为动态的,并在构建完成后更改为 Hasura 的实际容器 IP 地址?

我尝试了什么

  • docker-compose.yml 中添加depends_on 以强制Gatsby 等到Hasura 容器准备好,这样在Gatsby 容器开始构建时它就会拥有IP。但根据 [0],它并不能保证 Gatsby 会等到 Hasura 完成后自行启动。
  • 它建议添加一个自定义 bash 脚本来强制 Gatsby 容器等待。如果我使用wait-for-it.sh,子命令应该是什么(等待结束后的命令)?

[0]https://docs.docker.com/compose/startup-order/

docker-compose.yml

version: '3.6'

services:
  database:
    image: postgres:12.2
    container_name: 'postgres-db'
    env_file:
      - database.env
    volumes:
      - ./schema.sql:/docker-entrypoint-initdb.d/1-schema.sql
      - ./seed.sql:/docker-entrypoint-initdb.d/2-seed.sql

  hasura:
    image: hasura/graphql-engine:v1.2.1
    restart: on-failure
    container_name: 'hasura'
    depends_on:
      - database
    ports:
      - '8180:8080'
    env_file:
      - hasura.env

  web:
    build: '.'
    image: 'webserver'
    container_name: 'nginx-webserver'
    restart: on-failure
    depends_on:
      - hasura
    ports:
      - '8080:80'
    volumes:
      - /app/node_modules
      - .:/app
    env_file:
      - webserver.env

webserver.env 文件

NODE_ENV=production
GATSBY_WEBPACK_PUBLICPATH=/
HASURA_ENDPOINT=http://hasura:8080/v1/graphql

需要 Hasura URI 的 GraphQL Apollo 客户端:

export const client = new ApolloClient({
  uri: process.env.HASURA_ENDPOINT,
  fetch,
});

【问题讨论】:

  • 我想不通的是为什么你需要一个IP地址,如果你可以使用container_name,它会自动解析到容器内/etc/hosts的IP地址,因为所有的容器自动在同一个网络上。
  • 对不起,这就是我说 IP 时的意思 :)。我将容器名称 hasura 放在 webserver.env 文件 (http://hasura:8080/v1/graphql) 中,但它仍然在应用程序中返回 undefined。我认为这是因为应用容器先于另一个容器完成。
  • 不,文件/etc/hosts 应该会自动更新,一旦另一个容器启动,它应该会正常解析。我怀疑问题出在其他地方,也许hasura 会立即上下波动,使其进入不断重启的状态?

标签: docker docker-compose dockerfile apollo-client hasura


【解决方案1】:

找到了解决办法。

我想错了容器网络关系。

客户端在连接时查看主机的ip地址,而不是容器的。

说明

  • Hasura 容器通过localhost:8180 向所有人公开。如果您查看 docker-compose 文件,端口 8180:8080 的意思是“让 Hasura 的端口 8080 可访问 localhost 的端口 8180”。
  • gatsby 应用程序(nginx-webserver)应该指向localhost:8180,而不是hasura:8080

我最后的 docker-compose.yml:

version: '3.6'

    services:
      database:
        image: postgres:12.2
        container_name: 'postgres-db'
        env_file:
          - database.env
        volumes:
          - ./schema.sql:/docker-entrypoint-initdb.d/1-schema.sql
          - ./seed.sql:/docker-entrypoint-initdb.d/2-seed.sql
    
      hasura:
        image: hasura/graphql-engine:v1.2.1
        restart: on-failure
        container_name: 'hasura'
        depends_on:
          - database
        ports:
          - '8180:8080'
        env_file:
          - hasura.env
    
      web:
        build: '.'
        image: 'nginx-webserver'
        container_name: 'web'
        restart: on-failure
        ports:
          - '8080:80'
        volumes:
          - .:/app
          - app/node_modules
        env_file:
          - webserver.env

ApolloClient 设置:

import ApolloClient from 'apollo-boost';
import fetch from 'isomorphic-fetch';

export const HASURA_ENDPOINT_URI =
  process.env.NODE_ENV === 'development'
    ? 'http://localhost:8090/v1/graphql'
    : 'http://localhost:8180/v1/graphql';

export const client = new ApolloClient({
  uri: HASURA_ENDPOINT_URI,
  fetch
});

【讨论】:

    猜你喜欢
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 1970-01-01
    • 2014-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多