# Connecting to Remote or Local Database - Connection Issue

**URL:** <https://drone.discourse.group/t/connecting-to-remote-or-local-database-connection-issue/11355>\
**Category:** Drone Support\
**Created:** [December 30, 2017, 7:58am UTC](https://drone.discourse.group/t/connecting-to-remote-or-local-database-connection-issue/11355 "2017-12-30T07:58:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomnlittle](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/tomnlittle/32/4186_2.png) [@tomnlittle](https://drone.discourse.group/u/tomnlittle)\
**Post date:** [December 30, 2017, 7:58am UTC](https://drone.discourse.group/t/connecting-to-remote-or-local-database-connection-issue/11355/1 "2017-12-30T07:58:18Z")

</div>

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:password@0.0.0.0:1000/drone  
> # DRONE\_DATABASE\_DRIVER: postgres  
> # DRONE\_DATABASE\_DATASOURCE: postgresql://postgres:password@0.0.0.0: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 🙂

---

<div class="post-metadata">

**Author:** ![bradrydzewski](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/bradrydzewski/32/3513_2.png) [@bradrydzewski](https://drone.discourse.group/u/bradrydzewski)\
**Post date:** [December 30, 2017, 8:27am UTC](https://drone.discourse.group/t/connecting-to-remote-or-local-database-connection-issue/11355/3 "2017-12-30T08:27:41Z")

</div>

I do see some configuration issues that would explain the error messages. I definitely recommend the docker-compose [networking documentation](https://docs.docker.com/compose/networking/) as a primer.

> [@On another topic](https://drone.discourse.group/t/1476/1):
>
> 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”

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.

> [@On another topic](https://drone.discourse.group/t/1476/1):
>
> DATABASE\_CONFIG: postgresql://postgres:password@ **0.0.0.0** :1000/drone

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.

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

```

> [@On another topic](https://drone.discourse.group/t/1476/1):
>
> DATABASE\_CONFIG: postgresql://postgres:password@0.0.0.0: **1000** /drone

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.

---

<div class="post-metadata">

**Author:** ![bradrydzewski](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/bradrydzewski/32/3513_2.png) [@bradrydzewski](https://drone.discourse.group/u/bradrydzewski)\
**Post date:** [December 30, 2017, 9:13am UTC](https://drone.discourse.group/t/connecting-to-remote-or-local-database-connection-issue/11355/4 "2017-12-30T09:13:23Z")

</div>

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 🙂

```auto
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=...

```

---

<div class="post-metadata">

**Author:** ![tomnlittle](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/tomnlittle/32/4186_2.png) [@tomnlittle](https://drone.discourse.group/u/tomnlittle)\
**Post date:** [December 30, 2017, 10:28am UTC](https://drone.discourse.group/t/connecting-to-remote-or-local-database-connection-issue/11355/5 "2017-12-30T10:28:38Z")

</div>

Ok, thanks man it really helped 🙂

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 🙂

---

<div class="post-metadata">

**Author:** ![bradrydzewski](https://yyz1.discourse-cdn.com/flex003/user_avatar/drone.discourse.group/bradrydzewski/32/3513_2.png) [@bradrydzewski](https://drone.discourse.group/u/bradrydzewski)\
**Post date:** [December 30, 2017, 8:04pm UTC](https://drone.discourse.group/t/connecting-to-remote-or-local-database-connection-issue/11355/6 "2017-12-30T20:04:01Z")

</div>

great, glad to hear you got it working!
