Docker版本: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"]
.env中的 DATABASE_URL:
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
有人可以帮我吗?
最诚挚的问候
答案 0 :(得分:2)
使用Docker-Compose运行时,应通过服务名称访问数据库。
DATABASE_URL=pgsql://kapitain_user:kapitain_user_password@db:5432/kapitain
也许您必须允许通过pg_hba文件访问。