代码之家  ›  专栏  ›  技术社区  ›  Wai Yan Hein

无法将postgres与docker compose和Laravel一起使用

  •  0
  • Wai Yan Hein  · 技术社区  · 6 年前

    这是我的docker-compose.yml:

    version: '3'
    services:
        apache:
            container_name: easy_eat_apache
            image: webdevops/apache:ubuntu-16.04
            environment:
                WEB_DOCUMENT_ROOT: /var/www/public
                WEB_ALIAS_DOMAIN: easy-eat.localhost
                WEB_PHP_SOCKET: php-fpm:9000
            volumes: # Only shared dirs to apache (to be served)
            - ./public:/var/www/public:cached
            - ./storage:/var/www/storage:cached
            networks:
            - easy-eat-network
            ports:
            - "80:80"
            - "443:443"
        php-fpm:
            container_name: easy_eat_php
            image: jguyomard/laravel-php:7.2
            volumes:
            - ./:/var/www/
            - ./ci:/var/www/ci:cached
            - ./vendor:/var/www/vendor:delegated
            - ./storage:/var/www/storage:delegated
            - ./node_modules:/var/www/node_modules:cached
            - ~/.ssh:/root/.ssh:cached
            - ~/.composer/cache:/root/.composer/cache:delegated
            networks:
            - easy-eat-network
        db:
            image: postgres
            restart: always
            ports:
            - 5432
            environment:
                POSTGRES_PASSWORD: secret
            volumes:
            - easy-eat-data:/var/lib/postgresql
    networks:
        easy-eat-network:
            driver: "bridge"
    volumes:
        easy-eat-data:
            driver: "local"
    

    DB_CONNECTION=pgsql
    DB_HOST=db
    DB_PORT=5432
    DB_DATABASE=postgres
    DB_USERNAME=postgres
    DB_PASSWORD=secret
    

    然后运行migration命令,查看我的应用程序是否能够连接到postgres。我犯了这个错误。

       Illuminate\Database\QueryException  : SQLSTATE[08006] [7] timeout expired (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations)
    
      at /var/www/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
        660|         // If an exception occurs when attempting to run a query, we'll format the error
        661|         // message to include the bindings with SQL, which will make this exception a
        662|         // lot more helpful to the developer instead of just the database's errors.
        663|         catch (Exception $e) {
      > 664|             throw new QueryException(
        665|                 $query, $this->prepareBindings($bindings), $e
        666|             );
        667|         }
        668| 
    
      Exception trace:
    
      1   PDOException::("SQLSTATE[08006] [7] timeout expired")
          /var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
    
      2   PDO::__construct("pgsql:host=db;dbname=postgres;port=5432;sslmode=prefer", "postgres", "secret", [])
          /var/www/vendor/laravel/framework/src/Illuminate/Database/Connectors/Connector.php:70
    
      Please use the argument -v to see more details.
    

    我怎样才能修好它?

    1 回复  |  直到 3 年前
        1
  •  1
  •   David Maze    6 年前

    你的 php-fpm 将容器连接到 easy-eat-network 网络,但你的 db 容器不是,因此它将连接到默认的网络Docker Compose创建。这将导致在解决和路由容器之间的问题。

    这里最简单的解决方案可能就是依赖Docker Compose为您创建的默认网络。删除所有 networks: 区块,包括顶层和单个服务。

    如果您确实希望自己显式声明网络,那么您需要添加 networks: [easy-eat-network] 第二节 db: 服务区。