【问题标题】:run psql on postgres dockerfile在 postgres dockerfile 上运行 psql
【发布时间】:2020-06-06 03:24:21
【问题描述】:

我从 postgres 标准图像安装 postgis,然后我应该运行 psql 命令来创建扩展 POSTGIS。我认为我的问题是在 psql 命令中设置正确的 -h 参数,但我不确定。有什么想法吗?

FROM postgres:12.1

MAINTAINER xxx

#ARG A_DB_USER='postgres'
#ARG A_DB_PASS='postgres'
ARG A_DB_NAME='postgres'
ARG A_TZ='Europe/Zurich'

#ENV DB_USER=${A_DB_USER}
#ENV DB_PASS=${A_DB_PASS}
ENV DB_NAME=${A_DB_NAME}
ENV TZ=${A_TZ}

# Adjusting Timezone in the system
RUN echo $TZ > /etc/timezone && \
    apt-get update && apt-get install -y tzdata && \
    rm /etc/localtime && \
    ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && \
    dpkg-reconfigure -f noninteractive tzdata && \
    apt-get clean

# install postgis
RUN apt-get update && \
    apt-get install -y postgis && \
    apt-get clean

USER postgres

#Add password "postgres" to user postgres, create db, add .sql
RUN /etc/init.d/postgresql start && psql -U postgres -d mydb -h localhost -c "CREATE EXTENSION postgis;"


EXPOSE 5432

错误是:psql: error: could not connect to server: could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? could not connect to server: Cannot assign requested address Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? ERROR: Service 'istsos-db' failed to build: The command '/bin/sh -c /etc/init.d/postgresql start && psql -U postgres -d istsos -h localhost -c "CREATE EXTENSION postgis;"' returned a non-zero code: 2

编辑: 我用 docker-compose 运行它

version: '3.7'

services:

  app-db:
    build:
      context: ./
      dockerfile: Dockerfile-pg
    image: app-pg:1.0.0
    restart: always
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: app
      POSTGRES_DB: app-db
    volumes:
       - ./docker-entrypoint-initdb.d/init-user-db.sh:/docker-entrypoint-initdb.d/init-user-db.sh
       - v-app-pgdata:/var/lib/postgresql
       - v-app-pglog:/data/var/log/postgresql
       - v-app-pgconf:/etc/postgresql


    
  app-main:
    build: 
      context: ./
      dockerfile: Dockerfile-tar-cp
    image: app-main:1.0.0
    restart: always
    ports:
      - 80:80
volumes:
  v-app-pgdata:
    name: v-app-pgdata
  v-app-pglog:
    name: v-app-pglog
  v-app-pgconf:
    name: v-app-pgconf

我尝试使用POSTGRES_DB: app-db 并指定psql -h app-db 但是 docker-build 返回: No PostgreSQL clusters exist; see "man pg_createcluster" ... (warning). psql: error: could not connect to server: could not translate host name "app-db" to address: Name or service not known ERROR: Service 'app-db' failed to build: The command '/bin/sh -c /etc/init.d/postgresql start && psql -U postgres -d app -h app-db -c "CREATE EXTENSION postgis;"' returned a non-zero code: 2

我也尝试使用 localhost 没有成功。 有什么想法吗?

【问题讨论】:

  • 看看这个,好像是同一个问题stackoverflow.com/questions/45637206/…
  • @cogitoergosum 我尝试在 docker-compose 中指定 POSTGRES_DB: app-db 并使用 psql -h app-db 没有成功。有什么想法吗?

标签: postgresql dockerfile psql


【解决方案1】:

为您的运行脚本尝试以下代码。

RUN    /etc/init.d/postgresql start &&\
    psql --command "CREATE EXTENSION postgis;"

编辑:

请添加以下内容以确保端口已打开并且 pg 也侦听外部 IP。

RUN echo "host all  all    0.0.0.0/0  md5" >> /folder/to/conf/pg_hba.conf

添加监听所有ip的

RUN echo "listen_addresses='*'" >> /folder/to/conf/postgresql.conf

【讨论】:

  • 它返回:psql:错误:无法连接到服务器:无法连接到服务器:没有这样的文件或目录服务器是否在本地运行并接受 Unix 域套接字上的连接“/var/run/ postgresql/.s.PGSQL.5432”?错误:服务 'app-db' 无法构建:命令 '/bin/sh -c /etc/init.d/postgresql start && psql --command "CREATE EXTENSION postgis;"' 返回非零代码:2
  • 能否添加监听所有地址和远程连接以检查: RUN echo "host all all 0.0.0.0/0 md5" >> /folder/to/conf/pg_hba.conf 添加监听所有 ip 的 RUN echo "listen_addresses='*'" >> /folder/to/conf/postgresql.conf
猜你喜欢
  • 1970-01-01
  • 2017-11-12
  • 2020-09-21
  • 2020-06-26
  • 1970-01-01
  • 2020-09-25
  • 1970-01-01
  • 2020-08-21
  • 2022-11-10
相关资源
最近更新 更多