简体   繁体   English

Docker - PostgreSQL 无法连接到服务器:连接被拒绝 127.0.0.1:5432

[英]Docker - PostgreSQL could not connect to server: Connection refused 127.0.0.1:5432

Trying to dockerise my Symfony 4 app, running with PostgreSQL尝试将我的 Symfony 4 应用程序 dockerise 与 PostgreSQL 一起运行

But when I'm running:但是当我跑步时:

$ sudo docker-compose build

I'm having this error:我有这个错误:

In AbstractPostgreSQLDriver.php line 73:AbstractPostgreSQLDriver.php 第 73 行中:

An exception occurred in driver: SQLSTATE[08006] [7] could not connect to server: Connection refused:驱动程序发生异常: SQLSTATE [08006] [7] 无法连接到服务器:连接被拒绝:
Is the server running on host "127.0.0.1" and accepting服务器是否在主机“127.0.0.1”上运行并接受
TCP/IP connections on port 5432 ?端口5432上的 TCP/IP 连接?

Here's my docker-compose.yml file:这是我的docker-compose.yml文件:

version:  '3.7'
services:
  db:
    image: ${POSTGRES_IMAGE}
    restart: always
    environment:
      POSTGRES_DB: ${DATABASE_NAME}
      POSTGRES_USER: ${DATABASE_USER}
      POSTGRES_PASSWORD: ${DATABASE_PASSWORD}

  php:
    build:
      context: .
      dockerfile: ./docker/php/Dockerfile
    restart: on-failure
    user: 1000:1000
    volumes:
      - ./docker/php/uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
      - .:/var/www/symfony
    working_dir: /var/www/symfony
    depends_on:
      - db

Content of my .env file我的.env文件的内容

DATABASE_NAME=db
DATABASE_HOST=127.0.0.1
DATABASE_PORT=5432
DATABASE_USER=postgres
DATABASE_PASSWORD=root

## Docker images (name and version)
PHP_IMAGE=php:7.3-fpm
POSTGRES_IMAGE=postgres

Also FYI :仅供参考

$ sudo docker-compose config
debian@debian:~/dev/symfony$ sudo docker-compose config
services:
  db:
    environment:
      POSTGRES_DB: mrd
      POSTGRES_PASSWORD: root
      POSTGRES_USER: postgres
    image: postgres
    restart: always
  php:
    build:
      context: /home/debian/dev/symfony
      dockerfile: ./docker/php/Dockerfile
    depends_on:
    - db
    restart: on-failure
    user: 1000:1000
    volumes:
    - /home/debian/dev/symfony:/var/www/symfony:rw
    working_dir: /var/www/symfony
version: '3.7'

My Dockerfile :我的Dockerfile

FROM php:7.3-fpm

WORKDIR /var/www/symfony

RUN apt-get update

# Install Postgres PDO
RUN apt-get install -y libpq-dev \
 && apt-get install -y zip \
 && docker-php-ext-install pgsql pdo_pgsql \
 && apt-get install -y git

RUN pecl install apcu

RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
    && php -r "if (hash_file('SHA384', 'composer-setup.php') === 'a5c698ffe4b8e849a443b120cd5ba38043260d5c4023dbf93e1558871f1f07f58274fc6f4c93bcfd858c6bd0775cd8d1') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
    && php composer-setup.php --filename=composer \
    && php -r "unlink('composer-setup.php');" \
    && mv composer /usr/local/bin/composer

RUN ls -larth
COPY . /var/www/symfony

RUN PATH=$PATH:/var/www/symfony/vendor/bin:bin

RUN pwd \
    && ls \
    && composer install --no-interaction --no-ansi --optimize-autoloader\
    && php bin/console doctrine:database:create \
    && php bin/console doctrine:schema:update --no-interaction \
    && php bin/console doctrine:fixtures:load --no-interaction

Has anybody a clue on why it does this?有人知道它为什么这样做吗? And how to solve it?以及如何解决?

Searched a lot, and couldn't find anything that worked.搜索了很多,找不到任何有用的东西。 Thought the depends_on: db would do the trick, but no.认为depends_on: db可以解决问题,但没有。

The host for symphony isn't going to be localhost. Symphony 的主机不会是 localhost。 It's going to be db (the service name).它将是 db(服务名称)。 db instead of localhost. db 而不是 localhost。

The pastes in your question indicate a number of things to be addressed with your docker-compose.yml file:您问题中的粘贴表明您的docker-compose.yml文件需要解决的一些问题:

  1. You are missing an env_file: entry for your .env file您缺少.env文件的env_file:条目
  2. You are missing an image for the php service您缺少php服务的image
  3. You specify .您指定. as your build context (which should be a directory that indicates where your Dockerfile is located)作为您的构建上下文(应该是指示Dockerfile所在位置的目录)
  4. You specify a path as your Dockerfile (which should be a filename, not a directory) -- I guess this might all work, but it's a bit confusing to read您将路径指定为 Dockerfile (应该是文件名,而不是目录)——我想这可能都有效,但读起来有点混乱
  5. Indentation for -db and - /home/debian/dev/symfony:/var/www/symfony:rw is off -- this might not be a problem, but it's still difficult to read -db- /home/debian/dev/symfony:/var/www/symfony:rw的缩进关闭——这可能不是问题,但仍然难以阅读

From your question about build failing with an error regarding inability to connect to a database -- could you update your question and share your Dockerfile for symfony?从您关于build失败并出现有关无法连接到数据库的错误的问题中,您能否更新您的问题并为 symfony 分享您的Dockerfile I suspect that you need to remove a reference to a database connection.我怀疑您需要删除对数据库连接的引用。

Try to run:尝试运行:

docker-compose ps

see Container name见容器名称

Set DATABASE_HOST=Container name设置 DATABASE_HOST=容器名称

DATABASE_NAME=db
DATABASE_HOST=## paste Container name
DATABASE_PORT=5432
DATABASE_USER=postgres
DATABASE_PASSWORD=root

## Docker images (name and version)
PHP_IMAGE=php:7.3-fpm
POSTGRES_IMAGE=postgres

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法连接到服务器:连接被拒绝 服务器是否在主机“127.0.0.1”上运行并接受端口 5432 上的 TCP/IP 连接? - 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? 无法连接到服务器:连接被拒绝。 服务器是否在主机“localhost”(127.0.0.1) 上运行并在端口 5432 上接受 TCP/IP 连接 - could not connect to server: Connection refused. Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432 AWS RDS 的 Terraform Postgresql 提供程序错误:“拨号 tcp 127.0.0.1:5432:连接:连接被拒绝” - Terraform Postgresql provider error for AWS RDS: "dial tcp 127.0.0.1:5432: connect: connection refused" docker + golang lib/pq“拨号 tcp 127.0.0.1:5432:连接:连接被拒绝” - docker + golang lib/pq "dial tcp 127.0.0.1:5432: connect: connection refused" Postgresql:连接被拒绝 服务器是否在主机“127.0.01”(127.0.0.1)上运行并接受端口 5432 上的 TCP/IP 连接? - Postgresql: Connection refused Is the server running on host “127.0.01” (127.0.0.1) and accepting TCP/IP connections on port 5432? PostgreSQL:无法连接到服务器 - 连接被拒绝错误 - PostgreSQL: could not connect to server - Connection refused error PostgreSQL:无法连接到服务器:连接被拒绝 - PostgreSQL: could not connect to server: Connection refused 如何在Docker上将Rails与Postgresql连接? 正在获取:无法连接到服务器:连接被拒绝 - How to connect rails with postgresql on docker? Getting: could not connect to server: Connection refused PG 错误无法连接到服务器:连接被拒绝服务器是否在端口 5432 上运行? - PG Error could not connect to server: Connection refused Is the server running on port 5432? 无法连接到服务器:连接被拒绝 服务器是否在主机上运行……并接受端口 5432 上的 TCP/IP 连接? - could not connect to server: Connection refused Is the server running on host … and accepting TCP/IP connections on port 5432?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM