Fel*_*hes 1 postgresql containers docker dockerfile
我正在使用以下命令创建PostgreSQL容器:
docker run -p 5432:5432 -e POSTGRES_PASSWORD=123456789 -d postgres:9.3.6
Run Code Online (Sandbox Code Playgroud)
这将下载所需的基本映像,并创建一个容器.
当我使用telnet检查服务连接时,我得到:
$ telnet 127.0.0.1 5432
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Run Code Online (Sandbox Code Playgroud)
它可以正常使用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
Run Code Online (Sandbox Code Playgroud)
我应该怎么做才能从任何IP访问我的PostgreSQL容器?
我试过传递这样的配置属性.但是,当我这样做时,容器会立即退出(也许它会崩溃).
docker run -p 5432:5432 -e POSTGRES_PASSWORD=123456789 -d postgres:9.3.6 -c "listen_addresses = '*'"
Run Code Online (Sandbox Code Playgroud)
当我执行show listen_addresses以获取listen_addresses运行时属性时,我得到的*意思是参数设置为*正确,但仍然不起作用.
postgres=# show listen_addresses;
listen_addresses
------------------
*
(1 row)
Run Code Online (Sandbox Code Playgroud)
你必须创建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
Run Code Online (Sandbox Code Playgroud)
下一步解决 创建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"]
Run Code Online (Sandbox Code Playgroud)
从Dockerfile构建映像为其分配一个名称.
$ docker build -t my_postgresql .
Run Code Online (Sandbox Code Playgroud)
运行PostgreSQL服务器容器(在前台):
$ docker run --rm -P --name pg_test my_postgresql
Run Code Online (Sandbox Code Playgroud)
从主机系统连接$ 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
Run Code Online (Sandbox Code Playgroud)
我记得,基本的官方 postgresql docker 镜像只允许本地连接,这意味着配置listen_addresses='127.0.0.1'。
要修复此问题,请进入容器内部,更新postgresql.conf配置文件listen_addresses='*',然后重新启动容器。
| 归档时间: |
|
| 查看次数: |
5251 次 |
| 最近记录: |