LoginSignup
3
1

More than 1 year has passed since last update.

MacのDocker Desktopを利用せずにLaravel 8.xのsailコマンドを実行できるようにする

Last updated at Posted at 2021-09-06

Docker Desktopが有料化されることでDocker Desktopを利用しない選択肢を迫られるケースが出てくると思います。

Docker Desktopが有料化へ 従業員数250人以下・年間売り上げ1000万ドル以下の組織などは引き続き無料 - ITmedia NEWS
https://www.itmedia.co.jp/news/articles/2109/02/news112.html

Docker is Updating and Extending Our Product Subscriptions - Docker Blog
https://www.docker.com/blog/updating-product-subscriptions/

Docker Desktopを利用しない方法にはいくつか選択肢がありますがLaravel 8.xのsailコマンドを利用する場合、選択肢が狭まってしまうのがわかったのでメモ。

結論

sailコマンドはDockerデーモンの起動確認をしてて、Lima + nerdctlはだめだったので、docker + docker-machine + docker-composeで!(2021/09/06時点)

インストール手順

# Docker Desktopのアンインストール
# GUIからアンインストールする
# ※brew caskでインストールしているなら
# brew uninsutall docker

# brewでvirtualbox、docker cli、docker-machine、docker-composeのインストール
$ brew install virtualbox docker docker-machine docker-compose

# インストール確認
# アプリが立ち上がったらOK
$ virtualbox

$ docker -v

$ docker-machine -v

$ docker-compose -v

インストールできたらVirtualBoxでVMインスタンス作成します。

# VirtualBoxでVMインスタンス作成
$ docker-machine create --driver virtualbox default

# 環境変数を.bash_profileに設定
# zsh、fishの場合は任意のファイルに
$ echo "$(docker-machine env default)" >> .bash_profile
. .bash_profile

'docker.credentials.errors.InitializationError: docker-credential-desktop not installed or not available in PATH' エラーが出る場合以下のコマンドで設定ファイルを削除します。

$ rm ~/.docker/config.json

動作確認

Laravelのプロジェクトを新規作成してSailコマンドが実行できるか確認します。

$ cd 任意のディレクトリ
# hogeは任意のプロジェクト名
$ curl -s https://laravel.build/hoge | bash
$ cd hoge && ./vendor/bin/sail up

プロジェクト作成できたらdocker-compose.ymlを編集してextra_hosts: 定義を無効化します。
これはERROR: for hoge_laravel.test_1 Cannot create container for service laravel.test: invalid IP address in add-host: "host-gateway" を回避するためです。

yaml
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'
$ cd hoge
$ ./vendor/bin/sail up -d

Creating network "hoge_sail" with driver "bridge"
Creating hoge_selenium_1    ... done
Creating hoge_mailhog_1     ... done
Creating hoge_meilisearch_1 ... done
Creating hoge_redis_1       ... done
Creating hoge_mysql_1       ... done
Creating hoge_laravel.test_1 ... done

いえぃ!
、、、かと思いきやこのままホストからhttp://localhost/ にアクセスできません。Docker Machineを利用しているのでVMのIPアドレスをdocker-machine ip で取得する必要があります。

$ docker-machine ip

<取得できたIPアドレス>

で、http://<取得できたIPアドレス> にブラウザでアクセスします。

スクリーンショット 2021-09-06 18.21.53.png

oh...
権限エラー

必要な権限を付与します。

$ ./vendor/bin/sail exec laravel.test bash

# chmod -R guo+w storage

で、http://<取得できたIPアドレス> にブラウザでアクセスします。

スクリーンショット 2021-09-06 18.30.24.png

やってやったぜー
ただ、動作確認は不十分なので、このあといろいろと不具合は出てくると思います。。。辛い

あとがき

Docker Desktopを利用しなくても起動できるにはできますが、いろいろと、とてもとても、、、とても手間がかかってしまうので、素直に所属する企業に状況を説明してお金を払ってもらうようにお願いするのが一番ストレスがないなぁって思いましたマル

参考

containerd & Lima: Open source alternative to Docker for Mac | by Akihiro Suda | nttlabs | Sep, 2021 | Medium
https://medium.com/nttlabs/containerd-and-lima-39e0b64d2a59

lima-vm/lima: Linux virtual machines, on macOS (aka "Linux-on-Mac", "macOS subsystem for Linux", "containerd for Mac", unofficially)
https://github.com/lima-vm/lima

Docker Desktopの代替方法 - Windows and Mac編
https://zenn.dev/koduki/articles/ba54daaba28f93

macOSでDocker Desktopをアンインストールしてdocker-cli + docker-machineで動かすようにする - その手の平は尻もつかめるさ
https://moznion.hatenadiary.com/entry/2021/09/01/112601

Get started with Docker Machine and a local VM | Docker Documentation
https://docs.docker.com/machine/get-started/#run-containers-and-experiment-with-machine-commands

docker-machine env | Docker ドキュメント
https://matsuand.github.io/docs.docker.jp.onthefly/machine/reference/env/

Error with docker-compose: docker-credential-desktop not installed or not available in PATH · Issue #3785 · docker/for-mac
https://github.com/docker/for-mac/issues/3785

docker-credential-desktop.exe not installed or not available in PATH
https://sunday-morning.app/posts/2020-07-28-docker-credential-desktop-exe-not-installed-or-not-available-in-path

What is linux equivalent of "host.docker.internal" - Stack Overflow
https://stackoverflow.com/questions/48546124/what-is-linux-equivalent-of-host-docker-internal/61001152

DockerでLaravel+Apache+MySQLの開発環境を構築する - Qiita
https://qiita.com/neneta0921/items/22f9864b6f6ff6d36004

Docker Machine をローカル VM で始めるには — Docker-docs-ja 1.11.0 ドキュメント
http://docs.docker.jp/v1.11/machine/get-started.html

docker-machine で localhost でポートを公開する - Qiita
https://qiita.com/superbrothers/items/9505d6ebab6b10a585dd

3
1
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
3
1