1
1

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 5 years have passed since last update.

WSLでも通常のLinuxでもdockerにマウントできるカレントパスを取得する

Posted at

概要

WSL上でdockerを使用する場合、docker for windowsに接続して使用する事が一般的かと思います。
ここで、一番厄介なのがボリュームのパスです。
docker for windowsはwindowsの世界で動いているので、マウントするボリュームパスとしてはwindowsのパスを指定する必要が有ります。
WSL上のshellから「カレントディレクトリの内容をdockerに食わせたい」と考えた場合、パスの形式が違うので一度変換する必要が有ります。
スクリプト等でこれをやろうとする、WSLと普通のlinuxで書き方が変わってしまってややこしいので変換関数を作りました。

変換補助関数

以下が今回作成した補助関数です。
これを使うと、WSLの場合は、現在のパスがwindowsのパスとして取得されます。


function getPWD(){
    local t_PWD=`pwd`
    if [ -f /proc/sys/fs/binfmt_misc/WSLInterop ]; then
        echo `wslpath -w $t_PWD`;
    else
        echo $t_PWD
    fi
}

使用イメージ

カレントディレクトリをlocalにマウントする

pwd=`getPWD`
docker run --rm -v ${pwd}:/local ${DOCKER_IMAGE}
1
1
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?