0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

devcontainerのazCLIでログインできない!

Last updated at Posted at 2023-04-21

困った事

devcontainerから "az login" すると "Failed to establish a new connection"

  • devcontainer 環境で az login する
  • ブラウザでログイン、閉じてもいいよと表示される
  • Failed to establish a new connection のエラーが表示され、azure CLI が使えない・・・
  • devcontainerからは使えないが、WSL からは使える
  • VScode の Azure 系アドオンもログインできない

原因

どうやら devcontainerazure CLI からどこか外を見に行くときに、名前解決ができていない模様。

対応

/etc/resolv.confnameServer に追記します。

sudo vi /etc/resolv.conf

nameServer にパブリックなDNSを追記します。
8.8.8.8 は Google さん提供のものですね。
(私の環境の場合、127.0.0.11を書き換えようとしましたがDocker composeで起動している他のサーバを解決するのに残しておく必要がありました)

/etc/resolv.conf
nameserver 127.0.0.11
options ndots:0
+ nameserver 8.8.8.8

恒久的対策

devcontainer なので、他のメンバーに同様の問題が発生した場合、いちいちファイルをいじってもらうのも忍びない。
ということで devcontainer 作成後に実行される postCreateCommand を仕込みました。

postCreateCommand に仕込む

devcontainer イメージ作成時に実行されるように、シェルスクリプトを仕込みました。

.devcontainer/devcontainer.json
{
  "postCreateCommand": ".devcontainer/postCreateCommand.sh"
}

このスクリプトに、 /etc/resolv.conf に DNS を追記するコマンドを記載しておきます。

.devcontainer/postCreateCommand.sh
# /etc/resolv.confにnameserverを追記
sudo sh -c "echo 'nameserver 8.8.8.8' >> /etc/resolv.conf"

これで解決!

参考

MSのQ&Aを参考にしました。

az login fails with "Failed to establish a new connection: [Errno -2] Name or service not known'"

0
0
2

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?