Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-Compose Postgres 5432 Bind: Address already in use error

Alright so I've been searching for 3-4 days now and I can't seem to find a solution to my error. I have found similar errors, and some that seemed to be describing my exact issue but none of the solutions worked.

I'm trying to get a simply postgres, express, node app up and running. I want to be able to run docker-compose up -d and have it build all of the images, volumes, etc. Then I will run a bash script that will seed my postgres db with data. However, I keep getting an error with the ports I'm using. I've removed all of my images, containers, and even reinstalled docker but I can't figure it out. I removed everything from my docker-compose except for the postgres and it still doesn't work.

version: '3'
  services:
   postgres:
    image: postgres:10.0
    volumes:
      - /var/lib/postgresql/data
    ports:
     - '5432:5432'

Then on my host machine I simply plan on running the following bash script.

#!/bin/bash

host="postgres.dev"
adminUser="postgres"

psql -h $host -U $adminUser -c "select 1 from pg_database where datname = 'table_name';" | grep -q 1 || psql -h $host -U $adminUser -f ./"$(dirname $0)"/init-local-db.sql

I know that this approach should work since I'm patterning it after a work project...which works for them. But here's the error I get:

ERROR: for pg-db  Cannot start service postgres: driver failed programming external connectivity on endpoint pg-db (b3e5697cd563264250479682ec83d4a232d0d4bd679a787ad2089e944dda9e2f): Error starting userland proxy: listen tcp 0.0.0Creating test-api ... done

ERROR: for postgres  Cannot start service postgres: driver failed programming external connectivity on endpoint pg-db (b3e5697cd563264250479682ec83d4a232d0d4bd679a787ad2089e944dda9e2f): Error starting userland proxy: listen tcp 0.0.0.0:5432: bind: address already in use
ERROR: Encountered errors while bringing up the project.

I know some people say to simply change the port number so '5433:5432' but my problem with this is that when you install postgres by default its port is 5432, and I know it's not necessary to change it(because the work project doesn't). Any thoughts?

Update (next morning):

Okay I still have no idea why the error popped up in the first place, as I used lsof -i tcp:5432 (along with netstat) and nothing came up as using that port. I put my computer in suspend mode and went to bed. In the morning I woke up, changed my postgres version to 9.6 to see if that was it, and everything worked. I then switched it back to postgres 10.0 and again everything worked. Hopefully it won't pop back up again, but I have no idea why it popped up in the first place.

like image 396
BryceHayden Avatar asked Nov 02 '17 07:11

BryceHayden


People also ask

Why can't I start Docker PostgreSQL on port 5432?

A process (very likely PostgreSQL) is listening on port 5432, preventing another to listen on that port. Stop the process, e.g. with systemctl, then you will be able to start your docker PostgreSQL. Show activity on this post. There is a possibility that this might be happening due to multiple instances of postgres running on your machine.

How to use 5432 instead of 5432 in Docker?

You can also use 5432 instead of 5432:5432 in your docker command or docker-compose file and let docker choose the host port automatically. HI this is old but I tried ur suggestion by changing the 5432:5432 to just 5432 in docker-compose.yml file. But it still gives the same error. do you have any output for sudo lsof -i :5432?

How to kill a Postgres instance running in Docker?

Some instance of Postgres is running. You can execute kill <pid> to kill it if you want. You can also use 5432 instead of 5432:5432 in your docker command or docker-compose file and let docker choose the host port automatically. HI this is old but I tried ur suggestion by changing the 5432:5432 to just 5432 in docker-compose.yml file.

How to kill the host port 5432 in Docker?

You can execute kill <pid> to kill it if you want. You can also use 5432 instead of 5432:5432 in your docker command or docker-compose file and let docker choose the host port automatically. HI this is old but I tried ur suggestion by changing the 5432:5432 to just 5432 in docker-compose.yml file. But it still gives the same error.


1 Answers

There is only one reason you may be getting this error. You have PostgreSQL installed on your local machine and it's running, occupying the port 5432.

You have the following options:

  1. Disable (and remove from startup) PostgreSQL on your local machine. - Your Docker Compose will run.

    sudo service postgresql stop
    
    sudo update-rc.d postgresql disable
    
  2. Use a different port in docker-compose. There is nothing wrong with applying '5433:5432'. Other services of your docker-compose will connect to postgres by 5432 port. From your local machine you'll be able you address postgres by localhost:5433.

like image 131
Sergei Krivosheenko Avatar answered Oct 19 '22 14:10

Sergei Krivosheenko