【问题标题】:Docker: can't RUN psql commands from DockerfileDocker:无法从 Dockerfile 运行 psql 命令
【发布时间】:2017-11-12 16:20:56
【问题描述】:

我有一个 Python/PostgreSQL 项目,我正在尝试将其放入 Docker 容器中。 当我尝试在容器内运行 psql 命令以在构建映像时创建用户和数据库时,出现此错误:

psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

这是一个 Dockerfile:

FROM python:2.7-slim

WORKDIR /app

ADD . /app

RUN apt-get update && apt-get install -y postgresql postgresql-contrib libgeos-c1

RUN service postgresql start

RUN su postgres bash -c "psql -c \"CREATE USER myuser WITH PASSWORD 'password';\"" \
   && su postgres bash -c "psql -c \"CREATE DATABASE db WITH OWNER = myuser;\""```

请注意,我什至尝试在 psql 命令之前启动 postgresql 服务。

我尝试了另一种方法,即使用基本映像 postgres:9.6.3 并在其上安装 Python pip。我仍然得到同样的错误。这是第二个 Dockerfile 供参考:

FROM postgres:9.6.3

WORKDIR /app

ADD . /app

RUN apt-get update && apt-get install -y python-pip libgeos-c1

RUN service postgresql start

RUN su postgres bash -c "psql -c \"CREATE USER myuser WITH PASSWORD 'password';\"" \
   && su postgres bash -c "psql -c \"CREATE DATABASE db WITH OWNER = myuser;\""

自白:我是个码头菜鸟。

【问题讨论】:

  • @GregHNZ 嗯。我不这么认为。在那里,他试图从容器外部连接到 postgres。就我而言,我无法在容器内运行 psql 命令。我不想从外部连接。你能复制我的结果吗?我转储了我的 Dockerfile,以便您可以确认/反驳我的情况。

标签: postgresql docker


【解决方案1】:

最好使用docker-entrypoint-initdb.d 中的脚本。 在Dockerfile

COPY ./init-user-db.sh /docker-entrypoint-initdb.d/init-user-db.sh

还有脚本本身:

#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE USER docker;
CREATE DATABASE docker;
GRANT ALL PRIVILEGES ON DATABASE docker TO docker;

EOSQL

更多详情:https://docs.docker.com/samples/library/postgres/#how-to-extend-this-image

【讨论】:

    【解决方案2】:

    这是相关的Multiple RUN vs. single chained RUN in Dockerfile, what is better?

    我也遇到过这个问题。每个 RUN 可能不在同一个“上下文”中。请参阅此输出,注意容器对于 start 命令和 psql 命令是不同的。似乎它在构建映像时正在构建容器的快照。

    Step 25/30 : RUN service postgresql start
     ---> Running in 5c22e985359c
    Starting PostgreSQL 9.6 database server: main.
    Removing intermediate container 5c22e985359c
     ---> db1f2c2cdb33
    Step 26/30 : USER postgres
     ---> Running in 6d56501e7286
    Removing intermediate container 6d56501e7286
     ---> b35d3912eacf
    Step 27/30 : RUN psql -c "CREATE USER user with password 'password'"
     ---> Running in 8c9130d2daa5
    psql: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain 
    

    我使用以下命令将这些命令链接在一起

    RUN service postgresql start &&\
      sudo -u postgres psql -c "CREATE USER user with password 'password'"
    

    导致 CREATE USER 命令成功,如果需要,应该能够继续执行进一步的 &&\

    Step 23/23 : RUN service postgresql start &&  sudo -u postgres psql -c "CREATE USER user with password 'password'"
     ---> Running in f234f6074f56
    Starting PostgreSQL 9.6 database server: main.
    CREATE ROLE
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-28
      • 2020-06-26
      • 1970-01-01
      • 2019-11-08
      • 2019-03-14
      • 2022-01-18
      • 2020-04-25
      • 2019-07-17
      相关资源
      最近更新 更多