Connecting to Remote or Local Database - Connection Issue

Hey Guys, I have setup drone on a kubernetes cluster with the google cloud console and everything is working fine using the local sqllite database. I want to connect drone to a database that persists between resets of the kubernetes pod, so of course I want to connect drone to either a mysql or postgres database.

I thought this would be a straightforward change, I followed the guide, set the appropriate environment variables, but I consistently get the following error message from the pod:

time=“2017-12-30T07:53:26Z” level=fatal msg=“database ping attempts failed”

To eliminate kubernetes as the culprit I created a docker compose (see below) and tested the issue locally. I received the same error message as I did on the cluster:

time=“2017-12-30T07:53:26Z” level=error msg="dial tcp 0.0.0.0:1000: getsockopt: connection refused"
time=“2017-12-30T07:53:26Z” level=fatal msg=“database ping attempts failed”

I have tried to no avail:

  • Using the environment variables that are specified in the Dockerfile
  • Building the Docker image from source and specifying postgres from there
  • Building a separate alpine image and connecting to the database from there to test my docker network
  • Pinging the database from inside the drone container

drone:
image: drone/drone:0.8.2
environment:
DRONE_HOST: ****
DRONE_OPEN: '1’
DRONE_ORGS: ***
DRONE_ADMIN: t***
DRONE_GITHUB: '1’
DRONE_GITHUB_CLIENT: ***
DRONE_GITHUB_SECRET: ****
DRONE_SECRET: ***
GIN_MODE: release
DATABASE_DRIVER: postgres
DATABASE_CONFIG: postgresql://postgres:[email protected]:1000/drone
# DRONE_DATABASE_DRIVER: postgres
# DRONE_DATABASE_DATASOURCE: postgresql://postgres:[email protected]:1000/drone
ports:
- 80:8000
- 9000:9000
volumes:
- ./drone:/var/lib/drone/

drone-agent:
image: drone/agent:0.8.2
command: agent
restart: always
depends_on:
- drone
volumes:
- ./drone/docker.sock:/var/run/docker.sock
environment:
- DRONE_SERVER=drone-server:9000
- DRONE_SECRET=***

postgres:
image: postgres:9.6.6-alpine
restart: always
environment:
POSTGRES_PASSWORD: password
POSTGRES_USER: postgres
ports:
- 1000:5432

Any help is appreciated :slight_smile:

I do see some configuration issues that would explain the error messages. I definitely recommend the docker-compose networking documentation as a primer.

Pinging the database only fails when there is an issue with the connection string or network configuration. In this case, it looks like the network is configured correctly, but the address used in the connection string is the root cause.

You won’t be able to connect to the postgres container using 0.0.0.0 from inside the drone container, because containers have isolated networks. You need to connect to the postgres container using its actual IP address, which is discoverable by hostname.

Below is a sample connection string that we use in our unit tests, which I can confirm works. Note that we use the container hostname, postgres, to resolve the postgres container IP address.

DATABASE_DRIVER=postgres
DATABASE_CONFIG=host=postgres user=postgres dbname=postgres sslmode=disable

You also won’t be able to connect to the postgres container using port 1000 from inside the drone container. Postgres is going to be available at postgres:5432 on the default bridge network that is created by docker-compose. Port 1000 is only available from the host machine network.

I also meant to attach a sample configuration. I just tested this locally to confirm it works, so you can use it as a reference. This should in turn help to eliminate drone as the culprit :slight_smile:

version: '2'

services:
  postgres:
    image: postgres
    environment:
      - POSTGRES_USER=postgres

  drone-server:
    image: drone/drone:0.8
    ports:
      - 80:8000
      - 9000
    environment:
      - DATABASE_DRIVER=postgres
      - DATABASE_CONFIG=host=postgres user=postgres dbname=postgres sslmode=disable
      - DRONE_HOST=...
      - DRONE_SECRET=...
      - DRONE_GITHUB=true
      - DRONE_GITHUB_CLIENT=...
      - DRONE_GITHUB_SECRET=...

Ok, thanks man it really helped :slight_smile:

Kind of new to the docker scene and the networking document was really informative.

It’s all up and running now on production thank you :slight_smile:

great, glad to hear you got it working!