首页 > 解决方案 > 无法连接到服务器:连接被拒绝 服务器是否在主机“127.0.0.1”上运行并接受端口 5432 上的 TCP/IP 连接?

问题描述

码头工人版本:19.03.12

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?

码头工人-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:kapitain_user_password@127.0.0.1:5432/kapitain

网络统计-at: TCP 0.0.0.0:5432 PGLW0321:0 LISTENING InHost

有人可以帮助我吗?

此致

标签: postgresqldockersymfonydocker-composedockerfile

解决方案


使用 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


推荐阅读