David Frick
David Frick

Reputation: 776

Postgresql Container is not running in docker-compose file - Why is this?

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.

Upvotes: 1

Views: 7599

Answers (2)

David Frick
David Frick

Reputation: 776

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.

Upvotes: 1

nischay goyal
nischay goyal

Reputation: 3480

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:

Upvotes: 5

Related Questions