Can't run SQL File Against Docker for Setup

postgresql
2.0

#1

I have a docker database being setup. The database is reachable by ping in code. Can’t seem to get the volumes init scripts to run so the tables aren’t in the database when the tests run. What’s the right way to do this?

version: 2
jobs:
  build:
    services:
      - docker
    docker:
      - image: circleci/golang:1.8
      - image: circleci/postgres:9.6
        volumes:
          - ./scripts/sql/init:/docker-entrypoint-initdb.d:rw
        environment:
         # POSTGRES_INITDB_ARGS: scripts/sql/init/reset.sql
         POSTGRES_USER: root
         POSTGRES_DB: circle_test

    working_directory: /go/src/github.com/org/repo
    steps:
      - checkout
      # specify any bash command here prefixed with `run: `
      - run: make unit
      - run: make integration

#2

Hi,

So right off the bat, the first issue I see here is that you’re trying to use Docker Volumes, which doesn’t work well with the docker executor. I’d suggest using the machine executor and here’s why (read near the bottom): https://circleci.com/docs/2.0/docker-compose/

Info on using executor: https://circleci.com/docs/2.0/configuration-reference/#docker--machine-executor


#3

I’m just trying to run a SQL script. I use the volumes in docker-compose. If I can do it another way that’s fine, not tied to volumes.

Also tried doing run: psql < ./scripts/init/reset.sql


#4

So if you want to stick with the Docker executor and forget using volumes, and the reset.sql file is in your repo, you just need to install the psql binary and then tell it which host to use.

I haven’t ran this myself but it might be something like this:

version: 2
jobs:
  build:
    docker:
      - image: circleci/golang:1.8
      - image: circleci/postgres:9.6
        environment:
          POSTGRES_USER: root
          POSTGRES_DB: circle_test

    working_directory: /go/src/github.com/org/repo
    steps:
      - checkout
      - run: sudo apt-get update && sudo apt-get install postgresql
      - run: psql < scripts/init/reset.sql

You may need to pass psql the hostname of 127.0.0.1 with the port as well since Postgres is running in a separate container.


#5

Worked! Thanks so much.


#6

You’re welcome.


#7