为什么我的代理 docker 网络请求在本地有效但在生产环境中无效?

Why does my proxied docker network request work locally but not in production?

我正在从事一个为 private/secure docker 注册表构建前端的项目。我这样做的方法是使用 docker-compose 在前端和注册表之间创建一个网络。我的想法是使用 express 为我的站点提供服务,并通过 docker 网络将客户端的请求转发到注册中心。

在本地,一切正常....

但是,在生产环境中,客户端不会从注册表中获得响应。我可以登录注册表并通过 https://myregistry.net:5000/v2/_catalog 的邮递员(对于前目录)访问它的 api。但是...客户端只是出错了。

当我进入快速服务器容器并尝试将我创建的端点 curl 到代理请求时,我得到了这个

curl -vvv http://localhost:3000/api/images
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 3000 (#0)
> GET /api/images HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.61.1
> Accept: */*
>
* Empty reply from server
* Connection #0 to host localhost left intact
curl: (52) Empty reply from server

并且返回的错误包括 _currentUrl of https://username:password@registry:5000/v2/_catalog

我的 docker-compose 文件看起来像这样...

version: '3'
services:
  registry:
    image: registry:2
    container_name: registry
    ports:
      # forward requests to registry.ucdev.net:5000 to 127.0.0.1:443 on the container
      - "5000:443"
    environment:
      REGISTRY_AUTH: htpasswd
      REGISTRY_AUTH_HTPASSWD_PATH: /auth/htpasswd
      REGISTRY_AUTH_HTPASSWD_REALM: Registry Realm
      REGISTRY_HTTP_ADDR: 0.0.0.0:443
      REGISTRY_HTTP_TLS_CERTIFICATE: /certs/fullchain.pem
      REGISTRY_HTTP_TLS_KEY: /certs/privkey.pem
    volumes:
      - /etc/letsencrypt/live/registry.ucdev.net/fullchain.pem:/certs/fullchain.pem
      - /etc/letsencrypt/live/registry.ucdev.net/privkey.pem:/certs/privkey.pem
      - ./auth:/auth
    restart: always
  server:
    image: uc/express
    container_name: registry-server
    ports:
      - "3000:3000"
    volumes:
      - ./:/project
    environment:
      NODE_ENV: production
    restart: always
    entrypoint: ["npm", "run", "production"]

我的前端请求示例如下所示...

axios.get('http://localhost:3000/api/images')
      .then((response) => {
        const { data: { registry, repositories } } = response;
        this.setState((state, props) => {
          return { registry, repositories }
        })
      })
      .catch((err) => {
        console.log(`Axios error -> ${err}`)
        console.error(err)
      })

并且该请求被发送到快速服务器,然后像这样发送到注册表...

app.get('/api/images', async (req, res) => {
  // scheme is either http or https depending on NODE_ENV
  // registry is the name of the container on the docker network
  await axios.get(`${scheme}://registry:5000/v2/_catalog`)
  .then((response) => {
    const { data } = response;
    data.registry = registry;
    res.json(data);
  })
  .catch((err) => {
    console.log('Axios error -> images ', err);
    return err;
  })
})

如果您能提供任何帮助,那就太好了!谢谢!

在这种特殊情况下,问题与服务器背后的防火墙有关。来自 docker 个容器的请求被阻止。为了解决这个问题,我们必须明确地将 network_mode 设置为桥接。这允许来自容器内的请求正确运行。最终的 docker-compose 文件看起来像这样

version: '3'
services:
  registry:
    image: registry:2
    container_name: registry
    # setting network_mode here and on the server helps the express api calls work correctly on the myregistry.net server.
    # otherwise, the calls fail with 'network unreachable' due to the firewall.
    network_mode: bridge
    ports:
      # forward requests to myregistry.net:5000 to 127.0.0.1:443 on the container
      - "5000:443"
    environment:
      REGISTRY_AUTH: htpasswd
      REGISTRY_AUTH_HTPASSWD_PATH: /auth/htpasswd
      REGISTRY_AUTH_HTPASSWD_REALM: Registry Realm
      REGISTRY_HTTP_ADDR: 0.0.0.0:443
      REGISTRY_HTTP_TLS_CERTIFICATE: /certs/fullchain.pem
      REGISTRY_HTTP_TLS_KEY: /certs/privkey.pem
    volumes:
      - /etc/letsencrypt/live/myregistry.net/fullchain.pem:/certs/fullchain.pem
      - /etc/letsencrypt/live/myregistry.net/privkey.pem:/certs/privkey.pem
      - ./auth:/auth
    restart: always
  server:
    image: uc/express
    container_name: registry-server
    network_mode: bridge
    ports:
      - "3000:3000"
    volumes:
      - ./:/project
    environment:
      NODE_ENV: production
    restart: always
    entrypoint: ["npm", "run", "production"]