postgresql - 无法连接到服务器: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?

标签 postgresql docker symfony docker-compose dockerfile

Docker 版本:12.03.19

Postgres 版本:最新

PHP:7.4

Symfony 中的项目

当我开始docker-compose build一切都好

但是当我想要up时该项目,我有一个错误: SQLSTATE[08006] [7] could not connect to server: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?

docker-compose.yml:

version: '3.4'
services:
  php:
    build:
      context: ./kapitain
      dockerfile: Dockerfile-php
    volumes:
      - ./kapitain:/var/www/html
    depends_on:
      - db
  db:
    image: postgres:latest
    restart: always
    environment:
      - POSTGRES_DB=kapitain
      - POSTGRES_USER=kapitain_user
      - POSTGRES_PASSWORD=kapitain_user_password
    volumes:
      - ./kapitain/docker/postgres/init.sql:/docker-entrypoint-initdb.d/init.sql
      - db-data:/var/lib/postgresql/data:rw
    ports:
      - "5432:5432"
  api:
    build:
      context: ./kapitain
      dockerfile: Dockerfile-nginx
    ports:
      - "80:8080"
    volumes:
      - ./kapitain/public:/var/www/html/public:ro
    depends_on:
      - php
volumes:
  db-data: {}

Dockerfile-php:

ARG PHP_VERSION=7.4

FROM php:${PHP_VERSION}-fpm-alpine

RUN wget https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/msodbcsql17_17.5.1.1-1_amd64.apk && \
    wget https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/mssql-tools_17.5.1.1-1_amd64.apk

# persistent / runtime deps
RUN apk add --no-cache \
        acl \
        fcgi \
        file \
        gettext \
        git \
        gnupg \
        --allow-untrusted msodbcsql17_17.5.1.1-1_amd64.apk \
        --allow-untrusted mssql-tools_17.5.1.1-1_amd64.apk \
    ;

ARG APCU_VERSION=5.1.18
RUN set -eux; \
    apk add --no-cache --virtual .build-deps \
        $PHPIZE_DEPS \
        icu-dev \
        libzip-dev \
        postgresql-dev \
        zip \
        zlib-dev \
        unixodbc-dev \
    ; \
    \
    docker-php-ext-configure zip; \
    docker-php-ext-install -j$(nproc) \
        intl \
        pdo_pgsql \
        pdo_mysql \
        zip \
    ; \
    pecl install \
        apcu-${APCU_VERSION} \
        xdebug \
        pdo_sqlsrv \
    ; \
    pecl clear-cache; \
    docker-php-ext-enable \
        apcu \
        opcache \
        xdebug \
        pdo_sqlsrv \
    ; \
    \
    runDeps="$( \
        scanelf --needed --nobanner --format '%n#p' --recursive /usr/local/lib/php/extensions \
            | tr ',' '\n' \
            | sort -u \
            | awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
    )"; \
    apk add --no-cache --virtual .api-phpexts-rundeps $runDeps; \
    \
    apk del .build-deps

RUN rm msodbcsql17_17.5.1.1-1_amd64.apk && \
    rm mssql-tools_17.5.1.1-1_amd64.apk

COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

RUN ln -s $PHP_INI_DIR/php.ini-production $PHP_INI_DIR/php.ini

# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser
ENV COMPOSER_ALLOW_SUPERUSER=1
# install Symfony Flex globally to speed up download of Composer packages (parallelized prefetching)
RUN set -eux; \
    composer global require "symfony/flex" --prefer-dist --no-progress --no-suggest --classmap-authoritative; \
    composer clear-cache
ENV PATH="${PATH}:/root/.composer/vendor/bin"

WORKDIR /var/www/html

# build for production
ARG APP_ENV=prod

# prevent the reinstallation of vendors at every changes in the source code
COPY composer.json composer.lock symfony.lock ./
RUN set -eux; \
    composer install --no-dev --prefer-dist --no-scripts --no-progress --no-suggest; \
    composer clear-cache

# do not use .env files in production
COPY .env ./
COPY .env.test ./
COPY behat.yml.dist ./behat.yml
RUN composer dump-env ${APP_ENV}; \
    rm .env; \
    rm .env.test

# copy only specifically what we need
COPY bin bin/
COPY config config/
COPY public public/
COPY src src/
COPY features features/
COPY fixtures fixtures/
COPY templates templates/
COPY translations translations/

RUN set -eux; \
    mkdir -p var/cache var/log; \
    composer dump-autoload --no-dev --classmap-authoritative; \
    composer run-script --no-dev post-install-cmd; \
    chmod +x bin/console; sync

COPY docker/php/docker-entrypoint.sh /usr/local/bin/docker-entrypoint
RUN chmod +x /usr/local/bin/docker-entrypoint

RUN chown -R www-data:www-data /var/www/html
USER www-data:www-data

ENTRYPOINT ["docker-entrypoint"]
CMD ["php-fpm"]

.env 中的 DATABASE_URL: DATABASE_URL=pgsql://kapitain_user:<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef848e9f869b8e8681b09a9c8a9db09f8e9c9c98809d8bafdeddd8c1dfc1dfc1de" rel="noreferrer noopener nofollow">[email protected]</a>:5432/kapitain

netstat -at: TCP 0.0.0.0:5432 PGLW0321:0 LISTENING InHost

有人可以帮助我吗?

致以诚挚的问候

最佳答案

当使用 Docker-Compose 运行时,您应该通过服务名称访问数据库。

DATABASE_URL=pgsql://kapitain_user:kapitain_user_password@db:5432/kapitain

也许您必须允许通过 pg_hba 文件进行访问。

https://www.postgresql.org/docs/9.1/auth-pg-hba-conf.html

关于postgresql - 无法连接到服务器: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63791583/

相关文章:

database - 如何更改安装 postgis 的位置?后置数据库

docker - 在同一图上绑定(bind)两个实例 Janusgraph 时出错

php - 如何在docker容器中连接我的主机mysql?

php - 我可以赋予 Symfony 2 服务读取和写入 cookie 的能力吗?

postgresql - 创建表时执行触发器

postgresql - PostgreSQL 选择查询中表名中的双引号

postgresql - Postgres 语法需要额外的 ' ' 和 ""围绕事物

postgresql - 将 Airflow 连接添加到本地主机数据库(在 docker 上运行的 postgres)

ajax - symfony2 - 如何创建没有值的 "entity"类型的表单字段

php - Symfony/api-平台 PATCH NotEncodableValueException : "Syntax error"