Версия докера: 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?

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"]

DATABASE_URL в .env: DATABASE_URL=pgsql://kapitain_user:kapitain_user_password@127.0.0.1:5432/kapitain

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

Кто-нибудь может мне помочь?

С уважением

1
Corentin S 8 Сен 2020 в 13:02

1 ответ

Лучший ответ

При работе с 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

2
opHASnoNAME 8 Сен 2020 в 11:01