# WSL2 Ubuntu で zsh の Docker 補完エラー解決方法

Windows10 の WSL2 の Ubuntu 環境で zsh(oh-my-zsh)を使用していますが、ある日突然warning errorが出るようになりました。

# エラー内容

no such file or directory: /usr/share/zsh/vendor-completions/_docker

# 環境

項目 内容
OS Windows 10
環境 WSL2 Ubuntu
シェル zsh(oh-my-zsh)
問題 Docker 補完ファイルが見つからない

現状、.zshrcの内容は oh-my-zsh のデフォルトで、特に変更していません。

# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH

# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"

# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
ZSH_THEME="jonathan"

# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in $ZSH/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Uncomment the following line to use case-sensitive completion.
# CASE_SENSITIVE="true"

# Uncomment the following line to use hyphen-insensitive completion.
# Case-sensitive completion must be off. _ and - will be interchangeable.
# HYPHEN_INSENSITIVE="true"

# Uncomment one of the following lines to change the auto-update behavior
# zstyle ':omz:update' mode disabled  # disable automatic updates
# zstyle ':omz:update' mode auto      # update automatically without asking
# zstyle ':omz:update' mode reminder  # just remind me to update when it's time

# Uncomment the following line to change how often to auto-update (in days).
# zstyle ':omz:update' frequency 13

# Uncomment the following line if pasting URLs and other text is messed up.
# DISABLE_MAGIC_FUNCTIONS="true"

# Uncomment the following line to disable colors in ls.
# DISABLE_LS_COLORS="true"

# Uncomment the following line to disable auto-setting terminal title.
# DISABLE_AUTO_TITLE="true"

# Uncomment the following line to enable command auto-correction.
# ENABLE_CORRECTION="true"

# Uncomment the following line to display red dots whilst waiting for completion.
# You can also set it to another string to have that shown instead of the default red dots.
# e.g. COMPLETION_WAITING_DOTS="%F{yellow}waiting...%f"
# Caution: this setting can cause issues with multiline prompts in zsh < 5.7.1 (see #5765)
# COMPLETION_WAITING_DOTS="true"

# Uncomment the following line if you want to disable marking untracked files
# under VCS as dirty. This makes repository status check for large repositories
# much, much faster.
# DISABLE_UNTRACKED_FILES_DIRTY="true"

# Uncomment the following line if you want to change the command execution time
# stamp shown in the history command output.
# You can set one of the optional three formats:
# "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
# or set a custom format using the strftime function format specifications,
# see 'man strftime' for details.
# HIST_STAMPS="mm/dd/yyyy"

# Would you like to use another custom folder than $ZSH/custom?
# ZSH_CUSTOM=/path/to/new-custom-folder

# Which plugins would you like to load?
# Standard plugins can be found in $ZSH/plugins/
# Custom plugins may be added to $ZSH_CUSTOM/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git)

source $ZSH/oh-my-zsh.sh

# エラー解決手順

このエラーは、Docker Desktop for Windows の補完ファイルへのシンボリックリンクが壊れているか、ファイルが存在しないことが原因です。

# ステップ 1:シンボリックリンクの削除

既存のシンボリックリンクまたはファイルを削除します。

sudo rm -rf /usr/share/zsh/vendor-completions/_docker

# ステップ 2:Docker 補完ファイルのコピー

WSL2 の Docker Desktop の補完ファイルをコピーします。

sudo cp /mnt/wsl/docker-desktop/cli-tools/usr/share/zsh/vendor-completions/_docker /usr/share/zsh/vendor-completions/

パスの説明

  • /mnt/wsl/docker-desktop/cli-tools/: WSL2 から Docker Desktop の CLI ツールにアクセスするパス
  • /usr/share/zsh/vendor-completions/: zsh のベンダー補完ファイルの標準的な配置場所

# ステップ 3:ファイルの保護(書き込み不可に設定)

ファイルが上書きされないように、書き込み不可属性を設定します。

sudo chattr +i /usr/share/zsh/vendor-completions/_docker

chattr +i について

chattr +iは、ファイルに「不変(immutable)」属性を設定します。これにより、ファイルの削除や変更ができなくなります。解除する場合はchattr -iを使用します。

# ステップ 4:zsh 設定の再読み込み

.zshrcを再読み込みして、設定を反映させます。

source ~/.zshrc
# または
. ~/.zshrc

# ステップ 5:WSL の再起動(必要な場合)

エラーが続く場合は、WSL を再起動します。

PowerShell またはコマンドプロンプトから実行:

wsl --shutdown

その後、WSL を再度起動します。

# まとめ

WSL2 の Ubuntu で zsh の Docker 補完エラーを解決する方法を解説しました。

# エラーの原因

  • Docker Desktop for Windows の補完ファイルへのシンボリックリンクが壊れている
  • 補完ファイルが存在しない

# 解決手順

  1. シンボリックリンクの削除
  2. Docker 補完ファイルのコピー(WSL2 の Docker Desktop から)
  3. ファイルの保護chattr +iで書き込み不可に設定)
  4. zsh 設定の再読み込み
  5. WSL の再起動(必要な場合)

# ポイント

  • WSL2 では、Docker Desktop の補完ファイルは/mnt/wsl/docker-desktop/cli-tools/からアクセスできます
  • chattr +iでファイルを保護することで、再発を防げます
  • エラーが続く場合は、WSL を再起動してみてください

関連情報: WSL2 Docker Desktop 統合 (opens new window)

同じタグを持つ記事をピックアップしました。