LoginSignup
5
4

More than 3 years have passed since last update.

Docker(PHP+Laravel+PostgreSQL)で環境構築後にphp artisan migrateで失敗した時の対処

Last updated at Posted at 2020-01-29

Docker上で下記の環境を整えた後にコンテナ起動後、php artisan migrateをした際に以下に記載する2パターンのエラーに遭遇した際の対処をここに記す。

  • 「could not find driver」
  • 「configure: error: Cannot find libpq-fe.h.」

環境

  • php:7.3-fpm-alpine
  • nginx 1.17.8
  • PostgreSQL 12.1
  • Laravel 6.12.0

エラーその1 「could not find driver」

$ docker-compose up -d --build
$ docker-compose exec app ash
$ php artisan migrate

Illuminate\Database\QueryException  : could not find driver (SQL: select * from information_schema.tables where table_schema = public and table_name = migrations and table_type = 'BASE TABLE')

  at /work/vendor/laravel/framework/src/Illuminate/Database/Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673| 

原因と対策

could not find driverとあるようにPostgreSQL用のドライバーが無かった為に発生していた。
Dockerfileでdocker-php-ext-installする際にpdo_pgsqlを追記することで対処。
具体的には以下の行をDockerfile内のRUN set箇所に追記して無事にいけた。

docker-php-ext-install pgsql pdo_pgsql && \

追記後のDockerfile (参考程度に御覧ください)

Dockerfile
FROM php:7.3-fpm-alpine
LABEL maintainer "XXXXX"

ARG TZ
ENV COMPOSER_ALLOW_SUPERUSER 1
ENV COMPOSER_HOME /composer

RUN rm -rf /var/cache/apk/* && \
  rm -rf /tmp/*

RUN apk update
RUN set -eux && \
  apk add --update-cache --no-cache --virtual=.build-dependencies tzdata && \
  cp /usr/share/zoneinfo/${TZ} /etc/localtime && \
  apk del .build-dependencies && \
  docker-php-ext-install bcmath pgsql pdo_pgsql && \
  curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer && \
  composer config -g repos.packagist composer https://packagist.jp && \
  composer global require hirak/prestissimo

エラーその2 「configure: error: Cannot find libpq-fe.h.」

$ php artisan migrate
~中略~
configure: error: Cannot find libpq-fe.h. Please specify correct PostgreSQL installation path

原因と対策

調べてみると「libpq-devを入れる」という記事がいくつか出てくるが、
自分の環境だと同じエラーで突破できず。

そこで以下のURLにて解決した。
https://github.com/docker-library/php/issues/221#issuecomment-209920424
どうやらAlpine Linux環境だとpostgresql-devを入れる必要があるようですね。

以下の行をRUN set内に追記することで解決。

apk add --update-cache --no-cache postgresql-dev && \

以下が追記後のDockerfile (参考程度に御覧ください)

Dockerfile
FROM php:7.3-fpm-alpine
LABEL maintainer "XXXXXX"

ARG TZ
ENV COMPOSER_ALLOW_SUPERUSER 1
ENV COMPOSER_HOME /composer

RUN rm -rf /var/cache/apk/* && \
  rm -rf /tmp/*

RUN apk update

RUN set -eux && \
  apk add --update-cache --no-cache --virtual=.build-dependencies tzdata && \
  cp /usr/share/zoneinfo/${TZ} /etc/localtime && \
  apk del .build-dependencies && \
  apk add --update-cache --no-cache postgresql-dev && \
  docker-php-ext-install bcmath pgsql pdo_pgsql && \
  curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer && \
  composer config -g repos.packagist composer https://packagist.jp && \
  composer global require hirak/prestissimo

※ Ubuntuではaptを使用できますが、Alpine Linuxでは使用できませんのでapkと呼ばれるパッケージマネージャを使用する。

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4