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 (参考程度に御覧ください)
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 (参考程度に御覧ください)
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と呼ばれるパッケージマネージャを使用する。