Windows10(WSL2)でRails/Dockerの開発環境を構築したい


まず、Windows10でWSL2を使えるようにする。

公式ドキュメント通りに進める

私はUbuntuを入れました。(Ubuntu 20.04 LTSで大丈夫です。)

Docker EngineとDocker-ComposeをUbuntuにインストールする。

Docker Engineを先に入れてから、Docker-composeを入れる。

理由:On Linux systems, first install the Docker Engine for your OS as described on the Get Docker page, then come back here for instructions on installing Compose on Linux systems.

開発ディレクトリについて(重要)

WSL2 環境において開発ディレクトリをどこに置くかは重要です。

開発ディレクトリを Windowsファイルシステム側(Linuxパス: /mnt/c/...)に置いた場合、ファイル IO が異常に遅く、一部 Docker 環境ではネットワーク通信に不具合が発生するなどの問題が起こります。
そのため、基本的には \wsl$\Ubuntu-20.04\home<ユーザ名>(Linuxパス: /home/<ユーザ名>)など、Linuxファイルシステム側に開発ディレクトリを置く必要があります。
開発ディレクトリがLinuxファイルシステム側に置いてあれば、Dockerプロジェクトも安定・軽快に動かすことができます。(少なくとも今のところは)

Docker EngineをUbuntuに入れる

エラーが出た場合

~$ sudo docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

私はsudo service docker startを使うと動きました。(参考)

Docker-ComposeをUbuntuに入れる

Docker-composeを使用して、Railsをインストールする。

Docker社のクイックスタートを参考にして入れる

エラーが出た場合

W: Failed to fetch http://deb.debian.org/debian/dists/buster/InRelease  Temporary failure resolving 'deb.debian.org'
W: Failed to fetch http://security.debian.org/debian-security/dists/buster/updates/InRelease  Temporary failure resolving 'security.debian.org'
W: Failed to fetch http://deb.debian.org/debian/dists/buster-updates/InRelease  Temporary failure resolving 'deb.debian.org'
W: Some index files failed to download. They have been ignored, or old ones used instead.
Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package nodejs
E: Unable to locate package postgresql-client
ERROR: Service 'web' failed to build : The command '/bin/sh -c apt-get update -qq && apt-get install -y nodejs postgresql-client' returned a non-zero code: 100

どうやらコンテナのDNS設定によるものらしい。

私は、この通りに/etc/wsl.confと/etc/resolve.confを編集したら動きました!!