【问题标题】:Postgres Database in DockerfileDockerfile 中的 Postgres 数据库
【发布时间】:2019-11-18 08:08:14
【问题描述】:

我正在尝试在 DockerManually 中构建 PostgreSQL 映像,因为我需要为 docker 提供的 postgres 映像中不退出的数据库添加一些特定配置。 我写了以下dockerfile

    FROM alpine:3.7

    #update the OS
    RUN apk update

    #installing alpine software kit
    RUN apk add alpine-sdk

RUN apk update \
  postgresql-$PG_MAJOR-postgis-$POSTGISV \
  postgresql-$PG_MAJOR-postgis-$POSTGISV-scripts \
  postgresql-$PG_MAJOR-pgrouting \
  postgresql-$PG_MAJOR-pgrouting-scripts \
  postgresql-server-dev-$PG_MAJOR

#installing postgresql
RUN apk add postgresql
RUN apk add openrc --no-cache

RUN apk update && apk upgrade
RUN apk add --no-cache --virtual .base_deps build-base openssl-dev zlib-dev libxml2-dev wget gnupg ca-certificates
RUN apk add --no-cache readline-dev glib-lang libssl1.0 postgresql postgresql-client
RUN apk add --update binutils
RUN apk add --no-cache sudo
RUN apk --purge del .base_deps
RUN echo "postgres ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/postgres
RUN chmod 600 /etc/sudoers.d/postgres
RUN sync
#RUN /etc/init.d/postgresql start
#RUN postgres -D /usr/local/pgsql/data


EXPOSE 5432

然后我正在尝试使用交互式 shell 与 dockerfile 共进午餐

docker run -it 进程ID

然后使用以下命令来实例化数据库

 initdb Database
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 locales
  COLLATE:  C
  CTYPE:    C.UTF-8
  MESSAGES: C
  MONETARY: C
  NUMERIC:  C
  TIME:     C
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.

creating directory Database ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... UTC
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... sh: locale: not found
2019-11-18 08:04:06.670 UTC [89] WARNING:  no usable system locales were found
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.

然后我尝试使用以下命令对数据库进行午餐

postgres -D Database
2019-11-18 08:05:27.358 UTC [91] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2019-11-18 08:05:27.358 UTC [91] LOG:  could not bind IPv6 address "::1": Address not available
2019-11-18 08:05:27.358 UTC [91] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
2019-11-18 08:05:27.360 UTC [91] FATAL:  could not create lock file "/run/postgresql/.s.PGSQL.5432.lock": No such file or directory
2019-11-18 08:05:27.360 UTC [91] LOG:  database system is shut down

我遇到了一些我不知道为什么的错误?任何帮助将不胜感激。

我确实切换了 docker-compose,这是文件的示例

version: "3"
services:
  #  Create a service named db.
  db:
    #   Use the Docker Image postgres. This will pull the newest release.
    image: "postgres:12.1-alpine"
    #   Give the container the name my_postgres. You can changes to something else.
    container_name: "theNameOfDockerCompose"
    #   Setup the username, password, and database name. You can changes these values.
    environment:
      - POSTGRES_USER=DatabaseName
      - POSTGRES_PASSWORD=Password
      - POSTGRES_DB=development
    #   Maps port 54320 (localhost) to port 5432 on the container. You can change the ports to fix your needs.
    ports:
      - "5432:5432"
    #   Set a volume some that database is not lost after shutting down the container.
    #   I used the name postgres-data but you can changed it to something else.
    volumes:
      - ./postgres-data:/var/lib/postgresql/data

这是我现在使用的文件。 但是使用 Dockerfile,我仍然无法启用端点访问数据库。

【问题讨论】:

  • 我认为这不是bind_address 问题。相反,这是两个 postmaster 在同一个端口上运行的问题。请检查 ps -ef 以查看 postgres 已经在哪里运行——您可能需要将 port 更改为 Database/postgresql.conf 中的另一个值(除了 5432)

标签: database postgresql docker


【解决方案1】:

postgres-docker-bind-address-ipv6 可能重复


listen_addresses = '*' 更新为postgresql/data/postgresql.conf

【讨论】:

  • 文件 postgresql/data/postgresql.conf 不存在
  • 签入 /etc/postgresql 文件夹。登录到容器并查找它。如果你找到了,那么你需要从 dockerfile copy 命令提供编辑后的文件并运行它
【解决方案2】:
2019-11-18 08:05:27.360 UTC [91] FATAL:  could not create lock file "/run/postgresql/.s.PGSQL.5432.lock": No such file or directory

指向丢失的文件夹。添加

RUN mkdir -p /run/postgresql

到你的 Dockerfile

另外,您需要更改postgresql/data/postgresql.conf 中的listen_addresses = "*"。否则,您的数据库将绑定到容器中的 localhost,并且您将无法从除该特定容器内的任何地方访问它。您可以在此处的日志中看到这一点

2019-11-18 08:05:27.358 UTC [91] LOG:  listening on IPv4 address "127.0.0.1", port 5432

用于本地主机绑定。此外,这将修复您日志中的以下错误消息

2019-11-18 08:05:27.358 UTC [91] LOG:  could not bind IPv6 address "::1": Address not available

postgres 绑定 ipv6 失败。

【讨论】:

  • 文件 postgresql/data/postgresql.conf 不存在
  • 您是否尝试过find 来搜索它的文件夹结构? postgresql/data/postgresql.conf 的完整路径可能因您的安装而异
  • 那么你可以使用postgres提供的示例配置:github.com/postgres/postgres/blob/master/src/backend/utils/misc/…
  • @Osama Sy 如果任何答案解决了您的问题,请接受适当的答案以与社区分享解决方案。如果没有,请提供更多信息
  • 这些都没有解决我的问题,我仍然无法从 Docker 文件中找出数据库不工作,所以我切换到 Docker Compose
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-09
  • 2013-08-17
  • 1970-01-01
  • 2019-06-13
  • 2015-03-31
  • 2021-05-16
  • 1970-01-01
相关资源
最近更新 更多