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



我正在使用以下命令创建一个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

我应该怎么做才能让我的 PostgreSQL 容器从任何 IP 访问?

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

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

当我执行show listen_addresses以获取listen_addresses运行时属性时,我得到了*,这意味着参数已正确设置为*,但仍然不起作用。

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

你必须创建 postgresql.conf 参数,并设置 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

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

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

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

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

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

最新更新