使用Docker容器连接到Laravel中的Postgres

哈里斯·哈达雷维奇

我从laravel连接到postgres容器时遇到问题。

它们与分离的Docker容器位于同一主机上。

这是 YML

version: '3'
services:

  # The Application
  app:
    env_file: .env
    container_name: "laravel"
    build:
      context: ./
      dockerfile: app.dockerfile
    working_dir: /var/www
    volumes:
      - ./:/var/www
    environment:
      - "DB_PORT=${DB_PORT}"
      - "DB_HOST=${DB_HOST}"
      - "DB_USER=${DB_USER}"
      - "DB_PASS=${DB_PASS}"
      - "DB_DATABASE=${DB_DATABASE}"
      - "DB_SCHEMA=${DB_SCHEMA}"
    restart: 'always'
  # The Web Server
  web:
    container_name: "nginx"
    build:
      context: ./
      dockerfile: web.dockerfile
    working_dir: /var/www
    volumes:
      - ./:/app
    ports:
      - 8080:80
    restart: 'always'
  # The Database
  pgsql:
    image: postgres:10.1
    container_name: "baza"
    environment:
      - 'DB_USER=${DB_USER}'
      - 'DB_PASS=${DB_PASS}'
    volumes:
      - '${DB_VOLUME_LOCATION}:/var/lib/postgresql/data'
    ports:
      - "${DB_PORT}:${PGSQL_CONTAINER_PORT}"
    restart: 'always'

这里.env是Laravel 5的变量:

# postgres
DB_HOST=localhost
DB_PORT=6666
DB_DATABASE=postgres
DB_USER=postgres
DB_PASS=postgres
PGSQL_CONTAINER_PORT=5432

码头工人ps

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                                 NAMES
2b8e667bcb08        postgres:10.1           "docker-entrypoint.s…"   4 seconds ago       Up 1 second         0.0.0.0:6666->5432/tcp                baza
58f1573bf016        laraveldockerized_app   "php-fpm"                4 seconds ago       Up 2 seconds        9000/tcp                              laravel
8a24bd5073a8        laraveldockerized_web   "nginx -g 'daemon of…"   4 seconds ago       Up 3 seconds        443/tcp, 0.0.0.0:8080->80/tcp         nginx

我可以使用DataGrip和以下命令从外部连接到postgres:

  • 主机:localhost
  • 端口:6666
  • 用户:postgres
  • 通过:postgres

但是,当我尝试与Laravel连接时,出现以下错误:

SQLSTATE [08006] [7]无法连接到服务器:连接被拒绝服务器是否在主机“ localhost”(127.0.0.1)上运行并在端口6666上接受TCP / IP连接?无法连接到服务器:无法分配请求的地址服务器是否在主机“ localhost”(:: 1)上运行并接受端口6666上的TCP / IP连接?(SQL:从“ dokuments”中选择*)

我试图将.env中的DB_HOST更改为:

  • 基地
  • pgsql
  • 127.0.0.1
  • 0.0.0.0

但以上方法均无效。

任何帮助将不胜感激。

谢谢

迈克·福克斯泰克

添加数据库的更改名称

environment:
      - 'DB_USER=${DB_USER}'
      - 'DB_PASS=${DB_PASS}'
      # - 'DB_NAME=${DB_NAME}'
-------------------------------
DB_HOST=pgsql
DB_PORT=5432
DB_DATABASE=postgres
DB_USER=postgres
DB_PASS=postgres

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法连接到Python Shell外部的Docker容器中的postgres

无法使用Sequelize的Docker Compose连接到Postgres容器

无法从 Flask 连接到 Postgres Docker 容器

如何从主机连接到Docker Postgres容器

无法连接到远程 Postgres docker 容器

ufw禁止docker容器连接到postgres

使用 Docker 连接到 Postgres

使用Python连接到Docker容器中的MySQL

使用 GUI 连接到 docker 容器中的 Postgresql

无法从 Golang 容器连接到 Postgres docker 容器

无法连接到Docker容器中的mongodb

Docker - 连接到容器中的开放端口

无法连接到 docker 容器中的 mongoDB

无法使用mongo连接到docker容器

如何使用域名连接到Docker容器

无法从我的本地机器连接到 docker 容器中的 postgres 数据库

无法将Go Application连接到docker-compose中的postgres容器

flyway无法在docker-entrypoint-initdb.d脚本中连接到Postgres容器

如何设置database.yml以连接到Postgres Docker容器?

无法从 golang Docker 容器连接到 Postgres 数据库

无法通过pgAdmin连接到Docker容器上的Postgres

无法连接到Docker中的Postgres

可以从 docker 容器外部连接到数据库,但不能从内部连接到数据库(在 laravel 中)

无法将MySQL容器连接到Docker中的Tomcat容器

Docker-如何使用karaf容器连接到mysql容器?

连接到使用 Docker 安装的 Laravel 9 中的 mySQL

通过Jira容器连接到Postgres容器

无法使用psycopg2连接到Postgres容器?

从Docker容器连接到主机?