Postgres: Cannot run on OSX --> could not bind IPv6 socket: Cannot assign requested address

Created on 11 Apr 2018  Â·  8Comments  Â·  Source: docker-library/postgres

(this is a copy of what I posted on https://github.com/docker-library/postgres/issues/343#issuecomment-380408122. I just realised afterwards that more likely nobody from docker is still monitoring that post so I'm opening a new issue as suggested by @tianon, hopefully adding some more informations on this.)

I'm facing the could not bind IPv6 socket: Cannot assign requested address as well.
----------------- config infos -------------------
Host: Darwin Asuna.local 17.5.0 Darwin Kernel Version 17.5.0: Mon Mar 5 22:24:32 PST 2018; root:xnu-4570.51.1~1/RELEASE_X86_64 x86_64 (macOS 10.13.4)

Docker: community edition - Version 18.04.0-ce-rc2-mac61 (23890)
(at this very moment I'm running the experimental version hoping it could help, but the same happens with the current stable one)

docker-compose:

db:
    network_mode: "bridge"
    image: postgres:9.6-alpine
    ports:
      - "5432:5432"
    environment:
      POSTGRES_USER: docker
      POSTGRES_PASSWORD: docker
      POSTGRES_DB: docker

So nothing fancy other than the basic config needed to use it.
(but the same happens if I comment out everything except image: postgres:9.6-alpine, or use image postgres:9.6)

Output for docker-compose run db
(yes fig is actually /Users/<user>/local/bin/fig -> /usr/local/bin/docker-compose 'cose life is too short to type docker-compose)

➜  fig run db
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... sh: locale: not found
No usable system locales were found.
Use the option "--debug" to see details.
ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /var/lib/postgresql/data -l logfile start

waiting for server to start....LOG:  could not bind IPv6 socket: Address not available
HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
LOG:  database system was shut down at 2018-04-11 09:53:50 UTC
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started
 done
server started
CREATE DATABASE

CREATE ROLE


/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

waiting for server to shut down...LOG:  received fast shutdown request
.LOG:  aborting any active transactions
LOG:  autovacuum launcher shutting down
LOG:  shutting down
LOG:  database system is shut down
 done
server stopped

PostgreSQL init process complete; ready for start up.

LOG:  database system was shut down at 2018-04-11 09:53:52 UTC
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

----------------- /config infos -------------------

My 1st reaction was obviusly to check whatever I had ipv6 support enabled, only to find this bit of doc:
https://docs.docker.com/config/daemon/ipv6/
where Note: IPv6 networking is only supported on Docker daemons running on Linux hosts. makes me wonder if anyone was ever able to actually run this image on an OSX machine (and eventually how) since I see this kind of issue been around since a long time on this repo.

Some interesting bits I noticed when running it:

waiting for server to start....LOG:  could not bind IPv6 socket: Address not available

here is the pesky error message but:

[...]
LOG:  autovacuum launcher started
 done
server started                           <---- here it seems to be started any way(?)
CREATE DATABASE

CREATE ROLE

Only to shutdown right the way

waiting for server to shut down...LOG:  received fast shutdown request
[...]
 done
server stopped

but then it starts back up (again?)

PostgreSQL init process complete; ready for start up.

and down again

LOG:  database system was shut down at 2018-04-11 09:53:52 UTC

ending up with :

LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections            <------ wut? O.o
LOG:  autovacuum launcher started

end docker-compose doesn't quit, letting me think despite the errors stuff is actually running in that container.

Listing the active containers:

➜  docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS              PORTS               NAMES
d502bb4fe7e5        postgres:9.6-alpine   "docker-entrypoint.s…"   13 minutes ago      Up 13 minutes       5432/tcp            b2baccountsapi_db_run_3

shows it running but the exported port doesn't seems to be connected, and checking inside the container:

➜  ~ docker exec -it  d502bb4fe7e5 bash
bash-4.3# netstat -plunt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN      -
tcp        0      0 :::5432                 :::*                    LISTEN      -
bash-4.3# ps aux
PID   USER     TIME   COMMAND
    1 postgres   0:00 postgres
   48 postgres   0:00 postgres: checkpointer process
   49 postgres   0:00 postgres: writer process
   50 postgres   0:00 postgres: wal writer process
   51 postgres   0:00 postgres: autovacuum launcher process
   52 postgres   0:00 postgres: stats collector process
   97 root       0:00 bash

shows postgres active and listening (amusingly on ipv6 too after throwing the known error?), yet clients cannot connect to it.
I went thought all the usual voodoo: delete and re-install docker (multiple versions), delete and re-pull the base image (multiple versions multiple times), reboot my computer, etc but nothing changed.
At this point I'm pretty confused and I'm out of walls where to bang my head on to find a solution.

I'm just a simple developer so both my knowledge of postgres-administration and troubleshooting are limited (I'm just trying to have a postgres instace for my dev env, so time to troubleshoot something like this is very limited as well).

Can anyone perhaps suggest a postgres image that just works?
thanks for help.

question

Most helpful comment

in my case i had a homebrew installation of [email protected] which I tried to dockerize.
I received this error only when i mounted a directory in the host machine to the docker. First I thought its a docker issue, but then I tried to mount an empty directory and the error disappeared. I realized that postgres cannot read the data directory of a different postgres even though both versions were 9.6.6....no reason why

hope this helps

All 8 comments

I'm not sure what isn't working. I get identical output on a Linux host and I am able to connect with the supplied user/pass from another container. You will want to define a name for the automatic data volume so that it will be reused on restarts:

volumes:
  - pgdata:/var/lib/postgresql/data

So yes the ouput you are seeing is correct, on first start (when the postgres has an empty database directory), it initializes it:

https://github.com/docker-library/postgres/blob/61e369ce3738e38386fa01ce4809c2304e76257c/9.6/alpine/docker-entrypoint.sh#L61

Then it starts a temporary server that only listens for connections within the container (localhost). This likely where the could not bind IPv6 socket comes from since there is no ipv6 loopback address available:

https://github.com/docker-library/postgres/blob/61e369ce3738e38386fa01ce4809c2304e76257c/9.6/alpine/docker-entrypoint.sh#L97-L99

Then it creates the specified user + db:

https://github.com/docker-library/postgres/blob/61e369ce3738e38386fa01ce4809c2304e76257c/9.6/alpine/docker-entrypoint.sh#L108-L119

Then runs scripts in /docker-entrypoint-initdb.d:

https://github.com/docker-library/postgres/blob/61e369ce3738e38386fa01ce4809c2304e76257c/9.6/alpine/docker-entrypoint.sh#L126-L134

And then it stops the temporary server:

https://github.com/docker-library/postgres/blob/61e369ce3738e38386fa01ce4809c2304e76257c/9.6/alpine/docker-entrypoint.sh#L137

And then starts up the real server in the foreground so that Docker tracks the process of the container correctly and where the listen ::::5432 is coming from. So, postgres is listening on all ipv6 interfaces (and the kernel allows it) but it won't get data since there are no ipv6 addresses on any interface:

https://github.com/docker-library/postgres/blob/61e369ce3738e38386fa01ce4809c2304e76257c/9.6/alpine/docker-entrypoint.sh#L145

Since the proposed resolution was not responded to in some time I'm assuming the issue is resolved

no, it's not resolved at all: I just did what happens 99% of the times in these situations: I changed image because by the time I opened this issue I already spent too much time trying to figure the problem out :)

After looking at it again I believe your issue is not ipv6 related, the run command you gave to docker-compose: docker-compose run db seems to affect the networking configuration, perhaps something to do with just calling the single service from the file.


docker-compose run db

rei@Ayanami-clone:~/issues/postgres-429$ docker-compose run db
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.utf8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/postgresql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... sh: locale: not found
No usable system locales were found.
Use the option "--debug" to see details.
ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /var/lib/postgresql/data -l logfile start

waiting for server to start....LOG:  database system was shut down at 2018-05-21 18:37:19 UTC
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started
 done
server started
CREATE DATABASE

CREATE ROLE


/usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*

waiting for server to shut down....LOG:  received fast shutdown request
LOG:  aborting any active transactions
LOG:  autovacuum launcher shutting down
LOG:  shutting down
LOG:  database system is shut down
 done
server stopped

PostgreSQL init process complete; ready for start up.

LOG:  database system was shut down at 2018-05-21 18:37:23 UTC
LOG:  MultiXact member wraparound protections are now enabled
LOG:  database system is ready to accept connections
LOG:  autovacuum launcher started

$ netstat -tulpn | grep 5432
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)

$ ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.097 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.235 ms
^C


docker-compose up

$ docker-compose up
Creating postgres-429_db_1 ... done
Attaching to postgres-429_db_1
db_1  | The files belonging to this database system will be owned by user "postgres".
db_1  | This user must also own the server process.
db_1  | 
db_1  | The database cluster will be initialized with locale "en_US.utf8".
db_1  | The default database encoding has accordingly been set to "UTF8".
db_1  | The default text search configuration will be set to "english".
db_1  | 
db_1  | Data page checksums are disabled.
db_1  | 
db_1  | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1  | creating subdirectories ... ok
db_1  | selecting default max_connections ... 100
db_1  | selecting default shared_buffers ... 128MB
db_1  | selecting dynamic shared memory implementation ... posix
db_1  | creating configuration files ... ok
db_1  | running bootstrap script ... ok
db_1  | performing post-bootstrap initialization ... sh: locale: not found
db_1  | No usable system locales were found.
db_1  | Use the option "--debug" to see details.
db_1  | ok
db_1  | syncing data to disk ... ok
db_1  | 
db_1  | Success. You can now start the database server using:
db_1  | 
db_1  |     pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1  | 
db_1  | 
db_1  | WARNING: enabling "trust" authentication for local connections
db_1  | You can change this by editing pg_hba.conf or using the option -A, or
db_1  | --auth-local and --auth-host, the next time you run initdb.
db_1  | waiting for server to start....LOG:  database system was shut down at 2018-05-21 18:50:34 UTC
db_1  | LOG:  MultiXact member wraparound protections are now enabled
db_1  | LOG:  database system is ready to accept connections
db_1  | LOG:  autovacuum launcher started
db_1  |  done
db_1  | server started
db_1  | CREATE DATABASE
db_1  | 
db_1  | CREATE ROLE
db_1  | 
db_1  | 
db_1  | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1  | 
db_1  | waiting for server to shut down....LOG:  received fast shutdown request
db_1  | LOG:  aborting any active transactions
db_1  | LOG:  autovacuum launcher shutting down
db_1  | LOG:  shutting down
db_1  | LOG:  database system is shut down
db_1  |  done
db_1  | server stopped
db_1  | 
db_1  | PostgreSQL init process complete; ready for start up.
db_1  | 
db_1  | LOG:  database system was shut down at 2018-05-21 18:50:37 UTC
db_1  | LOG:  MultiXact member wraparound protections are now enabled
db_1  | LOG:  database system is ready to accept connections
db_1  | LOG:  autovacuum launcher started

$ netstat -tulpn | grep 5432
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp6       0      0 :::5432                 :::*                    LISTEN      -

$ ping 172.17.0.2
PING 172.17.0.2 (172.17.0.2) 56(84) bytes of data.
64 bytes from 172.17.0.2: icmp_seq=1 ttl=64 time=0.187 ms
64 bytes from 172.17.0.2: icmp_seq=2 ttl=64 time=0.123 ms
^C

As Yosifkit asked previously: "I'm not sure what isn't working."
Could you elaborate on what specific commands you're running and what the output is, and if it should indicate an error with the image then I'll reopen as an image issue.

If you're trying to connect externally and using run instead of up, you probably want to include --service-ports: (from https://docs.docker.com/compose/reference/run/)

    --service-ports       Run command with the service's ports enabled and mapped
                          to the host.

(otherwise it won't do the port-mapping defined in the YAML)

in my case i had a homebrew installation of [email protected] which I tried to dockerize.
I received this error only when i mounted a directory in the host machine to the docker. First I thought its a docker issue, but then I tried to mount an empty directory and the error disappeared. I realized that postgres cannot read the data directory of a different postgres even though both versions were 9.6.6....no reason why

hope this helps

For Postgres inside the container to listen to connections from the host: certain edits have to be made to the postgres.conf and pg_hba.conf files. See the Gotchas section at the cityseer/postgis repo.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

orion110217 picture orion110217  Â·  3Comments

roks0n picture roks0n  Â·  4Comments

bsctl picture bsctl  Â·  4Comments

Enelar picture Enelar  Â·  4Comments

andrewvo148 picture andrewvo148  Â·  3Comments