Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database Dockerfile中的Postgres数据库_Database_Postgresql_Docker - Fatal编程技术网

Database Dockerfile中的Postgres数据库

Database Dockerfile中的Postgres数据库,database,postgresql,docker,Database,Postgresql,Docker,我试图在docker中构建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_

我试图在docker中构建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运行-it进程ID

然后将以下命令发送到instantite数据库

 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
我得到一些错误,我不知道确切的原因?任何帮助都将不胜感激

我确实切换了yo 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,我仍然无法使端点访问数据库


更新
listen\u addresses='*'
postgresql/data/postgresql.conf

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\u 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。

可能重复的我认为这不是一个
bind\u地址问题。相反,这是一个两个邮局主管在同一个端口上运行的问题。请检查
ps-ef
以查看postgres已在何处运行--您可能需要将
port
更改为
Database/postgresql.conf
文件postgresql/data/postgresql.conf中的另一个值(除了5432)。登录容器并查找它。如果您找到了它,那么您需要从dockerfile copy命令提供编辑过的文件并运行它。文件postgresql/data/postgresql.conf不存在您是否尝试过
find
搜索文件夹结构以查找它?
postgresql/data/postgresql.conf
的完整路径可能会根据您的安装情况有所不同,然后您可以使用postgres提供的示例配置:@Osama Sy如果任何答案解决了您的问题,请接受相应的答案,与社区共享解决方案。如果没有,请提供进一步的信息这些都没有解决我的问题,我仍然无法从Docker文件看出数据库不工作,所以我切换到Docker Compose