勉強日記

チラ裏

【解決】docker on WSL2 で、docker image build時にネットワークエラー


環境

docker --version
Docker version 18.09.7, build 2d0083d

現象

  • docker image build時にネットワークがおかしくなることがある

Dockerfile

FROM php:7.4-cli-alpine

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
RUN composer global require hirak/prestissimo
  • この短いdockerfileのビルドで、ネットワーク周りで3箇所くらい躓きました

結論: dockerdの設定で解決

  • /etc/init.d/docker
...
  # This is the pid file created/managed by start-stop-daemon
  DOCKER_SSD_PIDFILE=/var/run/$BASE-ssd.pid
  DOCKER_LOGFILE=/var/log/$BASE.log
- DOCKER_OPTS=
+ DOCKER_OPTS="--dns 8.8.8.8"
  DOCKER_DESC="Docker"
...

詳細

  • 問題おきまくり

composerのinstallerが降ってこない

  • 再掲
FROM php:7.4-cli-alpine

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
RUN composer global require hirak/prestissimo
docker image build .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM php:7.4-cli-alpine
 ---> 3d0ba8511fe4
Step 2/3 : RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
 ---> Running in 36b888336848
  • curlをverboseにしてみる
  FROM php:7.4-cli-alpine
   
- RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
+ RUN curl -vsS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
  RUN composer global require hirak/prestissimo
docker image build .
  • IPv6で死んでそう
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM php:7.4-cli-alpine
 ---> 3d0ba8511fe4
Step 2/3 : RUN curl -vsS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
 ---> Running in a535056fcb6d
*   Trying 205.251.192.250:443...
* TCP_NODELAY set
*   Trying 2600:9000:5300:fa00::1:443...
* TCP_NODELAY set
* Immediate connect fail for 2600:9000:5300:fa00::1: Address not available
*   Trying 2600:9000:5303:dd00::1:443...
* TCP_NODELAY set
* Immediate connect fail for 2600:9000:5303:dd00::1: Address not available
...

composer.pharが降ってこない

  • v4に固定したら一応installerは降ってきたっぽい
  FROM php:7.4-cli-alpine
   
- RUN curl -vsS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
+ RUN curl -sS4 https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
  RUN composer global require hirak/prestissimo
  • が、今度はcomposer.pharが降ってこない
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM php:7.4-cli-alpine
 ---> 3d0ba8511fe4
Step 2/3 : RUN curl -sS4 https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
 ---> Running in acae50461534
All settings correct for using Composer
Downloading...
  • Downloading... はcomposer installerの出力
  • 永久にこのまま

composerライブラリが降ってこない

  • composerをinstallerで入れることを諦める
  • composerは単一の実行ファイルなんだから、multistage-buildにして、composerコンテナイメージから引っこ抜いてくればいいじゃない
  FROM php:7.4-cli-alpine
   
- RUN curl -sS4 https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
+ COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
  RUN composer global require hirak/prestissimo
docker image .
  • 結局ライブラリが降ってこない
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM php:7.4-cli-alpine
 ---> 3d0ba8511fe4
Step 2/3 : COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
 ---> 031f6f2a43f1
Step 3/3 : RUN composer global require hirak/prestissimo
 ---> Running in ffa5cf38498a
Changed current directory to /root/.composer
Do not run Composer as root/super user! See https://getcomposer.org/root for details

The "http://repo.packagist.org/p/provider-2013%24d43bc94601a2eb4a005d86fcc58809e63661238903da0f3b7c5155a294dd9b7f.json" file could not be downloaded: failed to open stream: Operation timed out
Retrying with degraded mode, check https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode for more info

                                                                                                                                                                                                                             
  [Composer\Downloader\TransportException]                                                                                                                                                                                   
  The "http://repo.packagist.org/p/provider-2013%24d43bc94601a2eb4a005d86fcc58809e63661238903da0f3b7c5155a294dd9b7f.json" file could not be downloaded: php_network_getaddresses: getaddrinfo failed: Name does not resolve  
  failed to open stream: php_network_getaddresses: getaddrinfo failed: Name does not resolve                                                                                                                                 
                                                                                                                                                                                                                             

require [--dev] [--prefer-source] [--prefer-dist] [--no-progress] [--no-suggest] [--no-update] [--no-scripts] [--update-no-dev] [--update-with-dependencies] [--update-with-all-dependencies] [--ignore-platform-reqs] [--prefer-stable] [--prefer-lowest] [--sort-packages] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--] [<packages>]...

The command '/bin/sh -c composer global require hirak/prestissimo' returned a non-zero code: 1

解決

/etc/init.d/docker でdockerdの起動オプションを設定できる

...
  # This is the pid file created/managed by start-stop-daemon
  DOCKER_SSD_PIDFILE=/var/run/$BASE-ssd.pid
  DOCKER_LOGFILE=/var/log/$BASE.log
- DOCKER_OPTS=
+ DOCKER_OPTS="--dns 8.8.8.8"
  DOCKER_DESC="Docker"
...
  • dockerd再起動
sudo service docker restart
service docker status
 * Stopping Docker: docker
   ...done.
 * Starting Docker: docker
   ...done.
 * Docker is running
  • 再度、当初のdockerfileをビルドしてみる
FROM php:7.4-cli-alpine

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
RUN composer global require hirak/prestissimo
docker image build .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM php:7.4-cli-alpine
 ---> 3d0ba8511fe4
Step 2/3 : RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
 ---> Running in 6819a66cbf5b
All settings correct for using Composer
Downloading...

Composer (version 1.9.1) successfully installed to: /usr/bin/composer
Use it: php /usr/bin/composer

Removing intermediate container 6819a66cbf5b
 ---> ce23bdb76548
Step 3/3 : RUN composer global require hirak/prestissimo
 ---> Running in 90201e9c39ea
Changed current directory to /root/.composer
Using version ^0.3.9 for hirak/prestissimo
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
  - Installing hirak/prestissimo (0.3.9): DownlDownloadiDownlDownloading (100%)
Writing lock file
Generating autoload files
Removing intermediate container 90201e9c39ea
 ---> 2db7289c8521
Successfully built 2db7289c8521
  • やったー!

所感

  • ネットワークは必修科目
  • 雰囲気で使っていると足元を掬われる