Php artisan migrate在docker容器上不起作用

时间:2018-02-17 09:33:19

标签: postgresql laravel-5

解决方案:docker inspect | grep IP获取postgres运行容器的ip并分别用容器的ip和端口更新你的env DB_HOST和DB_PORT。

更新:将postgres写入DB_HOST是最佳做法,因为IP地址可能会发生变化。

我一直在寻找一天将数据库从docker容器连接到我的Laravel应用程序,但无论我尝试过什么,即使我可以通过Postico连接它,我也无法迁移我的数据库。我收到以下错误:

  

SQLSTATE [08006] [7]无法连接到服务器:连接被拒绝     服务器是否在主机“localhost”(127.0.0.1)上运行并且正在接受     端口3306上的TCP / IP连接?

我的Dockerfile:

FROM php:7.1.3-fpm-alpine

RUN apk update && apk add build-base

RUN apk add postgresql postgresql-dev \
  && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
  && docker-php-ext-install pdo pdo_pgsql pgsql

RUN apk add zlib-dev git zip \
  && docker-php-ext-install zip

RUN curl -sS https://getcomposer.org/installer | php \
        && mv composer.phar /usr/local/bin/ \
        && ln -s /usr/local/bin/composer.phar /usr/local/bin/composer

COPY . /app
WORKDIR /app

RUN composer install --prefer-source --no-interaction

ENV PATH="~/.composer/vendor/bin:./vendor/bin:${PATH}"

我的docker-compose文件:

version: '2'
services:
  nginx:
    image: nginx:1.11.10-alpine
    ports:
      - 3000:80
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - api
  api:
    build: .
    ports:
      - 9000:9000
    volumes:
      - .:/app
      - /app/vendor
    depends_on:
      - postgres
    environment:
      DATABASE_URL: postgres://foodorder@postgres/foodorder
  postgres:
    image: postgres:latest
    environment:
      POSTGRES_USER: foodorder
      POSTGRES_DB: foodorder
      POSTGRES_PASSWORD: 123123
    ports:
      - 3306:5432

我的环境文件:

DB_CONNECTION = "pgsql"
DATABASE_CONNECTIONS_MASTER_PGSQL_HOST="localhost"
DATABASE_CONNECTIONS_MASTER_PGSQL_SCHEMA="foodorder"
DATABASE_CONNECTIONS_MASTER_PGSQL_DATABASE="foodorder"
DATABASE_CONNECTIONS_MASTER_PGSQL_PORT="3306"
DATABASE_CONNECTIONS_MASTER_PGSQL_USERNAME="foodorder"
DATABASE_CONNECTIONS_MASTER_PGSQL_PASSWORD="123123"

最后,在我的 database.php 中,

'default' => env('DB_CONNECTION', 'pgsql'),

'pgsql' => [
            'driver' => 'pgsql',
            'host' => env('DATABASE_CONNECTIONS_MASTER_PGSQL_HOST'),
            'port' => env('DATABASE_CONNECTIONS_MASTER_PGSQL_PORT'),
            'database' => env('DATABASE_CONNECTIONS_MASTER_PGSQL_DATABASE'),
            'username' => env('DATABASE_CONNECTIONS_MASTER_PGSQL_USERNAME'),
            'password' => env('DATABASE_CONNECTIONS_MASTER_PGSQL_PASSWORD'),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => env('DATABASE_CONNECTIONS_MASTER_PGSQL_SCHEMA'),
            'sslmode' => 'prefer',
        ],

1 个答案:

答案 0 :(得分:0)

检查数据库用户是否对localhost127.0.0.1都有访问权限。听起来您正在尝试连接到localhost,但只能为127.0.0.1授予访问权限,反之亦然。