Laravel8 を標準装備の Sail + Dockerで開発環境を作る際にMySQLを使わずPostgresに変更してみる

コマンド一発!

curl -s "https://laravel.build/hogehoge" | bash

これでインストールできるLaravelの開発環境ですが、デフォルトだとDockerにMySQLのコンテナがアサインされまます。

定番のインストールについては、この記事を参考に。

docker-compose.ymlにメスを入れる

version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        extra_hosts:
            - 'host.docker.internal:host-gateway'
        ports:
            - '${APP_PORT:-80}:80'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
            XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
            XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
#            - mysql
            - pgsql
            - redis
            - meilisearch
            - selenium
#    mysql:
#        image: 'mysql:8.0'
#        ports:
#            - '${FORWARD_DB_PORT:-3306}:3306'
#        environment:
#            MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
#            MYSQL_DATABASE: '${DB_DATABASE}'
#            MYSQL_USER: '${DB_USERNAME}'
#            MYSQL_PASSWORD: '${DB_PASSWORD}'
#            MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
#        volumes:
#            - 'sailmysql:/var/lib/mysql'
#        networks:
#            - sail
#        healthcheck:
#            test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
#            retries: 3
#            timeout: 5s
    pgsql:
        image: postgres:13
        ports:
            - '${FORWARD_DB_PORT:-5432}:5432'
        environment:
            PGPASSWORD: '${DB_PASSWORD:-secret}'
            POSTGRES_DB: '${DB_DATABASE}'
            POSTGRES_USER: '${DB_USERNAME}'
            POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}'
        volumes:
            - 'sailpostgresql:/var/lib/postgresql/data'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "pg_isready", "-q", "-d", "${DB_DATABASE}", "-U", "${DB_USERNAME}"]
    redis:
        image: 'redis:alpine'
        ports:
            - '${FORWARD_REDIS_PORT:-6379}:6379'
        volumes:
            - 'sailredis:/data'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "redis-cli", "ping"]
            retries: 3
            timeout: 5s
    meilisearch:
        image: 'getmeili/meilisearch:latest'
        ports:
            - '${FORWARD_MEILISEARCH_PORT:-7700}:7700'
        volumes:
            - 'sailmeilisearch:/data.ms'
        networks:
            - sail
        healthcheck:
            test: ["CMD", "wget", "--no-verbose", "--spider",  "http://localhost:7700/health"]
            retries: 3
            timeout: 5s
    mailhog:
        image: 'mailhog/mailhog:latest'
        ports:
            - '${FORWARD_MAILHOG_PORT:-1025}:1025'
            - '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
        networks:
            - sail
    selenium:
        image: 'selenium/standalone-chrome'
        volumes:
            - '/dev/shm:/dev/shm'
        networks:
            - sail
networks:
    sail:
        driver: bridge
volumes:
#    sailmysql:
#        driver: local
    sailpostgresql:
        driver: local
    sailredis:
        driver: local
    sailmeilisearch:
        driver: local

mysqlが退場し、ポスグレさんが参戦。

Sailから各コンテナを起動

./vendor/bin/sail up

ちなみに、docker-compose.yml内のインデントが少しでもずれていたら、

ERROR: yaml.parser.ParserError: while parsing a block mapping
  in "./docker-compose.yml", line 4, column 5
expected <block end>, but found '<block mapping start>'
  in "./docker-compose.yml", line 48, column 6
ERROR: yaml.parser.ParserError: while parsing a block mapping
  in "./docker-compose.yml", line 4, column 5
expected <block end>, but found '<block mapping start>'
  in "./docker-compose.yml", line 48, column 6
ERROR: yaml.parser.ParserError: while parsing a block mapping
  in "./docker-compose.yml", line 4, column 5
expected <block end>, but found '<block mapping start>'
  in "./docker-compose.yml", line 48, column 6

鬼のようにエラー吐くので要注意。

mailhogで何故かエラー

d4deb2e86480: Pull complete
d4132927c0d9: Pull complete
3d03efa70ed1: Pull complete
645312b7d892: Pull complete
3cc7074f2000: Pull complete
5d6e98ee16de: Pull complete
Digest: sha256:5d6e513ae9b936e5cd7bff5f1eb658d17512a49e2bb1902ac4c85b7ac8627308
Status: Downloaded newer image for postgres:13
Creating corporate_training_mailhog_1     ... error
Creating corporate_training_meilisearch_1 ... 
Creating corporate_training_pgsql_1       ... 
Creating corporate_training_redis_1       ... 
Creating corporate_training_meilisearch_1 ... done
Creating corporate_training_pgsql_1       ... done
Creating corporate_training_redis_1       ... done
Creating corporate_training_selenium_1    ... done
Creating corporate_training_laravel.test_1 ... done

ERROR: for mailhog  Cannot start service mailhog: Ports are not available: listen tcp 0.0.0.0:1025: bind: address already in use
ERROR: Encountered errors while bringing up the project.

コンテナ構築の最後でエラーを履きました。

PC内ですでにmailhogが稼働していて、ポートが競合しているエラーですね。

$brew services stop mailhog
==> Successfully stopped `mailhog` (label: homebrew.mxcl.mailhog)

そっとローカルのmailhogを止めました♪

で、結果。

問題なく動きました。

Postgressへのアクセスは

.envファイルを編集ですね。

#DB_CONNECTION=mysql
#DB_HOST=mysql
#DB_PORT=3306
DB_CONNECTION=pgsql
DB_HOST=pgsql
DB_PORT=5432
DB_DATABASE=hogehoge
DB_USERNAME=sail
DB_PASSWORD=password

これで大丈夫そう。

ちなみに各コンテナの起動中に、localhostの5432ポートへDBクライアントから直接アクセスも問題なく行えましたので、もう普通にpostgessを使っていけそうですねん♪