3

I have three docker containers (postgresql, adminer, and go/migrate) and I've exposed both adminer and postgres ports to the host. I can access adminer in my browser, and postico can also connect to the DB. When I try to connect to the db from within adminer, it throws this error:

SQLSTATE[08006] [7] could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP
connections on port 5432? could not connect to server: Address not available
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?

The migrate container throws this error too:

error: dial tcp: 127.0.0.1:5432: connect: connection refused

So, clearly there is an issue with how the containers are able to communicate with each other. Do I need to create a docker network?

version: '3.1'

services:
  db:
    image: postgres
    environment:
        POSTGRES_DB: mydbname
        POSTGRES_USER: mydbuser
        POSTGRES_PASSWORD: mydbpwd
    ports:
      - "5432:5432"

  migrate:
    image: migrate/migrate
    volumes:
        - .:/migrations
    command: ["-database", "postgres://mydbuser:mydbpwd@localhost:5432/mydbname?sslmode=disable", "-path", "/migrations", "up"]
    links: 
        - db

  adminer:
    image: adminer
    restart: always
    ports:
      - "8081:8080"
    depends_on:
      - db

Jon Leopard
  • 835
  • 1
  • 12
  • 24
  • 1
    The issue is that on migrate container there is no postgres service running. Localhost always refers to the container itself not docker host. Replace `localhost` with `db`. – leopal May 04 '20 at 13:50

1 Answers1

7

You do not need to create linking, docker-compose create default network. also service to service communication should use service name, localhost mean this container(migrate) not DB container.

change localhost to db

  migrate:
    image: migrate/migrate
    volumes:
        - .:/migrations
    command: ["-database", "postgres://mydbuser:mybpwd@db:5432/mydbname?sslmode=disable", "-path", "/migrations", "up"]

as you DB service name is db so you connect with db container using it name.

Adiii
  • 54,482
  • 7
  • 145
  • 148
  • 1
    Thanks! It looks like the container can communicate with it now. When I run `docker-compose up -d` the migrate container throws this error: `error: dial tcp 172.22.0.2:5432: connect: connection refused`. When I run `docker-compose up migrate` again, it seems like it gets past that connection error, but now says `error: first: file does not exist` Is there an issue with how I am mapping the volume? To clarify, the migration folder is located in the project root `./migrations/`, and it seems that the `migrate` container isn't able to find it. – Jon Leopard May 04 '20 at 14:47
  • `error: first: file does not exist`, this error seems from migrate container, you need to check local directory structure, it binds the whole path `.:/migrations`, – Adiii May 04 '20 at 15:31