メインコンテンツまでスキップ

WSL2上のDockerで名前解決がうまく行かない場合の対処法

· 約4分
moritalous
お知らせ

過去にQiitaに投稿した内容のアーカイブです。

Docker Desktop問題をこちらの記事を参考に対応してました。 https://qiita.com/ohtsuka1317/items/617a865b8a9d4fb67989

私の環境だけかもしれませんが、Dockerコンテナ内で名前解決がうまくいきませんでしたので応急処置的な対処方法です。

Dockerデーモン起動

WSL2
sudo /etc/init.d/docker start

Ubuntuイメージを取得して起動、Bashに接続

WSL2
docker run -it ubuntu:latest bash

apt updateしてみる

Ubuntuコンテナ
root@6babda3e1bb7:/# apt update
Err:1 http://archive.ubuntu.com/ubuntu focal InRelease
Temporary failure resolving 'archive.ubuntu.com'
Err:2 http://security.ubuntu.com/ubuntu focal-security InRelease
Temporary failure resolving 'security.ubuntu.com'
Err:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease
Temporary failure resolving 'archive.ubuntu.com'
Err:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Temporary failure resolving 'archive.ubuntu.com'
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal-updates/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal-backports/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/focal-security/InRelease Temporary failure resolving 'security.ubuntu.com'
W: Some index files failed to download. They have been ignored, or old ones used instead.
root@6babda3e1bb7:/#

名前解決ができてない模様

Ubuntuコンテナ
root@6babda3e1bb7:/# cat /etc/resolv.conf 
## This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
## [network]
## generateResolvConf = false
nameserver 172.17.48.1
root@6babda3e1bb7:/#

172.17.48.1はWSLのホストであるWindowsのIPアドレスです。

解決方法

resolv.confを上書きしちゃう

Ubuntuコンテナ
echo 'nameserver 8.8.8.8' | tee /etc/resolv.conf
root@6babda3e1bb7:/# apt update
Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:2 http://archive.ubuntu.com/ubuntu focal InRelease [265 kB]
Get:3 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [796 kB]
Get:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:5 http://security.ubuntu.com/ubuntu focal-security/restricted amd64 Packages [581 kB]
Get:6 http://security.ubuntu.com/ubuntu focal-security/multiverse amd64 Packages [30.1 kB]
Get:7 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [1135 kB]
Get:8 http://archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB]
Get:9 http://archive.ubuntu.com/ubuntu focal/universe amd64 Packages [11.3 MB]
Get:10 http://archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages [177 kB]
Get:11 http://archive.ubuntu.com/ubuntu focal/restricted amd64 Packages [33.4 kB]
Get:12 http://archive.ubuntu.com/ubuntu focal/main amd64 Packages [1275 kB]
Get:13 http://archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 Packages [33.4 kB]
Get:14 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [1582 kB]
Get:15 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [1084 kB]
Get:16 http://archive.ubuntu.com/ubuntu focal-updates/restricted amd64 Packages [632 kB]
Get:17 http://archive.ubuntu.com/ubuntu focal-backports/universe amd64 Packages [6310 B]
Get:18 http://archive.ubuntu.com/ubuntu focal-backports/main amd64 Packages [2668 B]
Fetched 19.3 MB in 6s (3008 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
4 packages can be upgraded. Run 'apt list --upgradable' to see them.
root@6babda3e1bb7:/#

正しいやり方かどうかはわかりません。

おまけ

VSCodeのRemote - Containers拡張を使う場合は、devcontainer.jsonpostCreateCommandにでも書いておきましょう

devcontainer.json
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.202.1/containers/ubuntu
{
"name": "Ubuntu",
"runArgs": ["--init"],
"build": {
"dockerfile": "Dockerfile",
// Update 'VARIANT' to pick an Ubuntu version: hirsute, focal, bionic
// Use hirsute or bionic on local arm64/Apple Silicon.
"args": { "VARIANT": "focal" }
},

// Set *default* container specific settings.json values on container create.
"settings": {},


// Add the IDs of extensions you want installed when the container is created.
"extensions": [],

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
"postCreateCommand": "echo 'nameserver 8.8.8.8' | sudo tee /etc/resolv.conf",

// Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode",
"features": {
}
}