【问题标题】:Docker container to bring up DB fails with connection refused error启动数据库的 Docker 容器失败并出现连接被拒绝错误
【发布时间】:2018-05-16 09:12:24
【问题描述】:

Docker:初始化数据库不起作用

我有一个设置数据库的 dockerfile。此 dockerfile 用于 docker-compose 文件。

当我运行$docker-compose up 时出现以下错误:

恐慌:拨打 tcp 172.19.0.2:5432:连接:连接被拒绝

FROM postgres:9.6 
ENV POSTGRES_PORT=5432
ENV POSTGRES_USER=abc
ENV FLYWAY_VERSION 5.0.2
RUN apt-get update && apt-get install -y wget build-essential
RUN ["mkdir", "/src"]
RUN wget -q https://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/$FLYWAY_VERSION/flyway-commandline-$FLYWAY_VERSION-linux-x64.tar.gz -P /src/
RUN tar xzf /src/flyway-commandline-$FLYWAY_VERSION-linux-x64.tar.gz -C /src/ RUN chmod +x /src/flyway-$FLYWAY_VERSION/flyway
ENV PATH=${PATH}:/src/flyway-$FLYWAY_VERSION
COPY Makefile /src/ COPY migrations /docker-entrypoint-initdb.d/migrations/ COPY flyway.test.conf /docker-entrypoint-initdb.d/
COPY bin/initdb.sh /docker-entrypoint-initdb.d/
EXPOSE $POSTGRES_PORT

【问题讨论】:

    标签: postgresql docker


    【解决方案1】:

    扩展 postgres docker 映像后,您需要在新的 Dockerfile 末尾添加入口点,以便您可以再次启动 postgres 服务,或者您可以在运行新容器时手动设置它。

    通过检查原始Dockerfile 中的Postgres 9.6,您可以看到缺少以下几行:

    ENTRYPOINT ["docker-entrypoint.sh"]
    CMD ["postgres"]
    

    【讨论】:

    • 我试过了,但仍然出现同样的错误... psql: 无法连接到服务器:连接被拒绝 db_1 |服务器是否在主机“localhost”(127.0.0.1) 上运行并接受 db_1 |端口 5432 上的 TCP/IP 连接? db_1 |无法连接到服务器:无法分配请求的地址 db_1 |服务器是否在主机“localhost”(::1) 上运行并接受 db_1 |端口 5432 上的 TCP/IP 连接?
    • 您能否暂时禁用initdb.sh 以确保此脚本不会引发错误?还有docker logs 的结果是什么?
    • db_1 | psql --command='CREATE DATABASE test_db' 2>/dev/null || true db_1 | psql --command="CREATE USER someone WITH SUPERUSER CREATEROLE CREATEDB LOGIN PASSWORD 'something'" || true db_1 | psql: could not connect to server: No such file or directory db_1 | Is the server running locally and accepting db_1 | connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
    • 我试过 ENV PGHOST=localhost 我得到了:db_1 | psql: could not connect to server: Connection refused db_1 | Is the server running on host "localhost" (127.0.0.1) and accepting db_1 | TCP/IP connections on port 5432? db_1 | could not connect to server: Cannot assign requested address db_1 | Is the server running on host "localhost" (::1) and accepting db_1 | TCP/IP connections on port 5432?
    • 可以在启动postgres容器时添加docker日志的结果吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    相关资源
    最近更新 更多