Gogs + Drone getsockopt: connection refused

2019-07-27 07:45发布

问题:

In the Gogs/webhooks interface when i click the test delivery button i got this error;

Delivery: Post http://localhost:8000/hook?access_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0ZXh0IjoidG9tL2Ryb255IiwidHlwZSI6Imhvb2sifQ.UZdOVW2IdiDcLQzKcnlmlAxkuA8GTZBH634G0K7rggI: dial tcp [::1]:8000: getsockopt: connection refused

This is my docker-compose.yml file

version: '2'
services:
  gogs:
    image: gogs/gogs
    ports:
      - 3000:3000
      - 22:22
    links:
      - mysql

  mysql:
    image: mysql
    expose:
      - 3306
    environment:
      - MYSQL_ROOT_PASSWORD=1234
      - MYSQL_DATABASE=gogs

  drone:
    image: drone/drone
    links:
     - gogs
    ports:
      - 8000:8000
    volumes:
      - ./drone:/var/lib/drone 
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - REMOTE_DRIVER=gogs
      - REMOTE_CONFIG=http://gogs:3000?open=true
      # - PUBLIC_MODE=true

回答1:

The root cause is that Drone assumes its external address is localhost:8000 because that is how it is being accessed from your browser. Drone therefore configures all Gogs webhooks to use localhost:8000/hook as the callback URL.

The problem here is that Gogs and Drone are running in separate containers on separate networks. This means when Gogs tries to send the hook to drone it sends it to localhost:8000 and fails because Drone is on a separate bridge.

Recommended Fix

The recommended fix is to use DNS or an IP address with Drone and Gogs. If you are running a production installation of either system it is unlikely you will be using localhost, so this seems like reasonable soluation.

For local testing you can also use the local IP address assigned by Docker. You can find your Drone and Gogs IP addresses using Docker inspect:

"Networks": {
  "default": {
    "IPAddress": "172.18.0.3"
  }
}

Why not custom hostnames?

Using custom hostnames, such as http://gogs, is problematic because drone creates ephemeral Docker containers for every build using the default Docker network settings. This means your build environment will use its own isolated network and will not be able to resolve http://gogs

So even if we configured Drone and Gogs to communicate using custom hostnames, the build environment would be unable to resolve the Gogs hostname to clone your repository.