如何使Postgres(Docker)可以远程访问任何IP?

时间:2018-01-19 12:29:32

标签: postgresql docker containers dockerfile

我使用以下命令创建PostgreSQL容器:

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

将下载所需的基本映像,并创建一个容器。

当我使用telnet检查服务连接时,我得到:

$ telnet 127.0.0.1 5432

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

使用127.0.0.1作为IP地址可以正常工作,但是如果我使用计算机的IP地址,控制台就会卡住,很长一段时间后它会给我一个超时错误。

$ telnet 191.115.52.110 5432

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

如何从任何IP访问PostgreSQL容器,我该怎么办?

我试过传递这样的配置属性。但是,当我这样做时,容器会立即退出(可能会崩溃)。

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

当我执行show listen_addresses以获取listen_addresses运行时属性时,我得到*,这意味着参数已正确设置为*,但它仍无法正常工作。< / p>

postgres=# show listen_addresses;
 listen_addresses 
------------------
 *
(1 row)

3 个答案:

答案 0 :(得分:5)

您必须创建 postgresql.conf whith参数,并设置 listen_addresses ='*'

启动容器时附加。

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

下一个解决方案。 创建Dockerfile并添加以下内容:

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"]

从Dockerfile构建映像,为其指定一个名称。

$ docker build -t my_postgresql .

运行PostgreSQL服务器容器(在前台):

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

从主机系统连接 $ 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

答案 1 :(得分:2)

我做到了,我不得不在路由器中打开端口。愚蠢的错误,考虑到我以前做过这个,但不知怎的,我今天忘记了。所有其他答案都是正确的,因为listen_addresses需要*

为了检查是否是,您可以在show listen_addresses;控制台中执行postgres=#

答案 2 :(得分:1)

我记得,基本的官方postgresql docker镜像只允许本地连接,这意味着配置listen_addresses='127.0.0.1'

要修复它,请进入容器内部,更新文件postgresql.conf配置listen_addresses='*',然后重新启动容器。