1

I am trying to figure out why I am getting an error about postgresql not running in my project. It is not connecting through Flask, nor when I try to access it through bash using the command docker-compose run postgres bash then psql returns the error:

bash-5.0# psql
psql: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

I tried running --force-recreate and to drop all abandoned orphan containers but this has not seemed to work. Similarly, I made sure it does not interfere with my local postgresql installation by uninstalling the local one and removing all files. I am pretty stumped on this.

Here is my docker-compose file:

version: "3"

services:

  webapp:
    build: .
    container_name: webapp
    ports:
      - "5000:5000"

  postgres:
    image: postgres:11-alpine
    container_name: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_DB=tmp
      - POSTGRES_USER=tmp
      - POSTGRES_PASSWORD=tmp_password
    volumes:  # Persist the db data
      - database-data:/var/lib/postgresql/data


volumes:
  database-data:

Any help is appreciated.

David Frick
  • 641
  • 1
  • 9
  • 25

2 Answers2

3

Please try the below docker-compose.yml in which depends_on, healthcheck and links are added as web service depends on db service.

version: "3"
services:
  webapp:
    build: .
    container_name: webapp
    ports:
      - "5000:5000"
    links:
      - postgres
    depends_on:
      - postgres

  postgres:
    image: postgres:11-alpine
    container_name: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_DB=tmp
      - POSTGRES_USER=tmp
      - POSTGRES_PASSWORD=tmp_password
    volumes:  # Persist the db data
      - database-data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

volumes:
  database-data:

nischay goyal
  • 3,206
  • 12
  • 23
  • Okay, I will give this a go. I had the depends on before but not the condition, healthcheck or links. – David Frick May 31 '20 at 16:42
  • Got `The Compose file './docker-compose.yml' is invalid because: services.webapp.depends_on contains an invalid type, it should be an array ` – David Frick May 31 '20 at 20:40
  • Edited my answer, there was a typo – nischay goyal May 31 '20 at 20:41
  • oddly, same issue. docker-compose run postgres bash then psql will give the same error. What is going on :( ? I also checked my local machine with `netstat -anp|grep "5432"` and nothing is using that port. – David Frick May 31 '20 at 20:56
  • I am editing this. I saw that `links_on` is depreciated so I switched to using `network` – David Frick Jun 18 '20 at 02:23
1

So I figured out the issue after trying to set up a mail server. Basically, I had installed postgresql on the local machine and then when I moved it to docker I forgot to uninstall it. When I uninstalled postgresql from the local machine the docker db now works.

David Frick
  • 641
  • 1
  • 9
  • 25