Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Postgres (Docker) accessible for any IP remotely?

I'm creating a PostgreSQL container using the following command:

docker run -p 5432:5432 -e POSTGRES_PASSWORD=123456789 -d postgres:9.3.6

which will download the required base image, and create a container.

When I check the service connectivity using telnet I get:

$ telnet 127.0.0.1 5432

Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.

It works properly using 127.0.0.1 as the IP address, but if I use my computer's IP address, the console gets stuck and after a long while it gives me a timeout error.

$ telnet 191.115.52.110 5432

Trying 191.115.52.110...
telnet: Unable to connect to remote host: Connection timed out

What should I do in order to have my PostgreSQL container accessible from any IP?

I have tried passing a config attribute like this. However when I do, the container exits immediately (perhaps it crashes).

docker run -p 5432:5432 -e POSTGRES_PASSWORD=123456789 -d postgres:9.3.6 -c "listen_addresses = '*'"

When I execute show listen_addresses to get the listen_addresses runtime attribute, I get * which means the parameter was set to * correctly, but still it doesn't work.

postgres=# show listen_addresses;
 listen_addresses 
------------------
 *
(1 row)
like image 365
Chris Vilches Avatar asked Jan 19 '18 12:01

Chris Vilches


People also ask

How do I make my docker container accessible from network?

To make a port available to services outside of Docker, or to Docker containers which are not connected to the container's network, use the --publish or -p flag. This creates a firewall rule which maps a container port to a port on the Docker host to the outside world.

How do I access docker in PostgreSQL?

Connecting to the PSQL server via CLI : Run the below command to enter into the container (with the ID from step-1). docker exec -it <PSQL-Container-ID> bash. Authenticate to start using as postgres user. psql -h localhost -p 5432 -U postgres -W.


2 Answers

You have to create postgresql.conf whith parameters, and set listen_addresses = '*'

attach when starting your container.

docker run -p 5432:5432 -e POSTGRES_PASSWORD=123456789 \
 -d postgres:9.3.6 \
 -c config_file=/path/to/postgresql.conf

next solution. Create Dockerfile and add follows:

FROM ubuntu

# Add the PostgreSQL PGP key to verify their Debian packages.
# It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.asc
RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8

# Add PostgreSQL's repository. It contains the most recent stable release
#     of PostgreSQL, ``9.3``.
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list

# Install ``python-software-properties``, ``software-properties-common`` and PostgreSQL 9.3
#  There are some warnings (in red) that show up during the build. You can hide
#  them by prefixing each apt-get statement with DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3

# Note: The official Debian and Ubuntu images automatically ``apt-get clean``
# after each ``apt-get``

# Run the rest of the commands as the ``postgres`` user created by the ``postgres-9.3`` package when it was ``apt-get installed``
USER postgres

# Create a PostgreSQL role named ``docker`` with ``docker`` as the password and
# then create a database `docker` owned by the ``docker`` role.
# Note: here we use ``&&\`` to run commands one after the other - the ``\``
#       allows the RUN command to span multiple lines.
RUN    /etc/init.d/postgresql start &&\
    psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
    createdb -O docker docker

# Adjust PostgreSQL configuration so that remote connections to the
# database are possible.
RUN echo "host all  all    0.0.0.0/0  md5" >> /etc/postgresql/9.3/main/pg_hba.conf

# And add ``listen_addresses`` to ``/etc/postgresql/9.3/main/postgresql.conf``
RUN echo "listen_addresses='*'" >> /etc/postgresql/9.3/main/postgresql.conf

# Expose the PostgreSQL port
EXPOSE 5432

# Add VOLUMEs to allow backup of config, logs and databases
VOLUME  ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]

# Set the default command to run when starting the container
CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"]

Build an image from the Dockerfile assign it a name.

$ docker build -t my_postgresql .

Run the PostgreSQL server container (in the foreground):

$ docker run --rm -P --name pg_test my_postgresql

Connecting from your host system $ docker ps

CONTAINER ID        IMAGE                  COMMAND                CREATED             STATUS              PORTS                                      NAMES
5e24362f27f6        my_postgresql:latest   /usr/lib/postgresql/   About an hour ago   Up About an hour    0.0.0.0:49153->5432/tcp                    pg_test

$ psql -h localhost -p 49153 -d docker -U docker --password
like image 119
Senior Pomidor Avatar answered Sep 22 '22 15:09

Senior Pomidor


As I remember, the basic official postgresql docker image only allow to connect locally, that means the config listen_addresses='127.0.0.1'.

To fix it, please go inside your container, update file postgresql.conf the configuration listen_addresses='*', and then restart your container.

like image 20
Mabu Kloesen Avatar answered Sep 22 '22 15:09

Mabu Kloesen