为什么我的代理Docker网络请求在本地工作,但在生产环境中不工作?

时间:2018-11-01 18:52:40

标签: express docker docker-compose docker-registry

我正在研究一个为私有/安全的Docker注册表构建前端的项目。我这样做的方法是使用docker-compose在前端和注册表之间创建网络。我的想法是使用express服务我的网站,并通过docker网络将客户端的请求转发到注册表。

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

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

当我进入快速服务器容器并尝试卷曲我创建的用于代理请求的端点时,我明白了

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中的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;
  })
})

您能提供的任何帮助都将非常棒!谢谢!

1 个答案:

答案 0 :(得分:1)

在这种特殊情况下,这是与服务器背后的防火墙有关的问题。来自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"]
相关问题