brand new note

ジャズ屋が技術の話をするところ

dockerのバインドマウント時のエラー

dockerの勉強中に以下のエラーが出ました.

コンテナ上でnginxを起動してhtmlファイルをホストOSから覗けるようにするだけの基本的なものです.エラーログ読む根性がほしいけど難しいものやるより簡単なものから数こなしたほうがつくのでは?抱え込んで簡単に心が折れてても一生独学成功しなくないか?というわけでメモです.ぐぐったらここにたどり着いた的な何かに今後なってくれると嬉しいんですが,応急処置に応急処置を重ねてる姿勢だけはくれぐれも真似しないでください.

PS C:\Users\ikeda\docker_tutorial\html> docker run --name first-nginx -v C:\Users\ikeda\docker_tutorial\html\:usr\share\nginx\html:ro -d -p 8080:80 nginx
docker: Error response from daemon: invalid volume specification: '/run/desktop/mnt/host/c/Users/ikeda/docker_tutorial/html:usr\share\nginx\html:ro': invalid mount config for type "bind": invalid mount path: 'usr/share/nginx/html' mount path must be absolute.
See 'docker run --help'.

"mount path must be absolute."が鍵.バインドマウントを行う際には絶対パスを指定する必要があるとのこと.これを見ると確かに/usrがusrになっていて相対パス扱いされています.ダブルコロンとスラッシュが逆ですね.

修正しましたが次のエラーが出てきました.

docker: Error response from daemon: driver failed programming external connectivity on endpoint first-nginx (4311e420ab2e3b1a826c6dc1642e1ce581f0b6bc9586656c1838f2bdf603ebde): Bind for 0.0.0.0:8080 failed: port is already allocated.

8080番はもう既に使用済みで使えないとのこと.これは個人的に別のコンテナを立てている為です.一旦8081番にします.

修正しましたが次のエラーが出てきました.

docker: Error response from daemon: Conflict. The container name "/first-nginx" is already in use by container "a436be8a7f358bc6b0f47446787d9071d33b0327db983f44b42ebaee970fb888". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.

first-nginxというコンテナ名ですでに一つコンテナを作成しているので,コンテナを削除するか別名で生成しないとコンフリクトしますよと書いてあります.別名でコンテナを生成します.second-nginxにします.

PS C:\Users\ikeda\docker_tutorial\html> docker run --name second-nginx -v C:\Users\ikeda\docker_tutorial\html:\usr\share\nginx\html:ro -d -p 8081:80 nginx
6502055431b93e3a6110b9236349dc9a55643e34f24a58ac184473ebde54957b
PS C:\Users\ikeda\docker_tutorial\html>

新しいUUIDでコンテナが生成されました.

f:id:frazz:20210407225647p:plain

でもデフォルトのindex.htmlが表示されていますね.自分で書いたindex.htmlはどこに行ったのでしょうか.本来はこれが出力されるはずです.

PS C:\Users\ikeda\docker_tutorial\html> ls

    ディレクトリ: C:\Users\ikeda\docker_tutorial\html

----                -------------         ------ ----
-a----       2021/04/07     22:04            100 index.html

PS C:\Users\ikeda\docker_tutorial\html> cat .\index.html
<!DOCTYPE html>
<html>
<body>

<h1>MyFirst Heading</h1>
<p>My first paragraph.</p>

</body>
</html>

powershellからコマンドを書いていて,パスの記述をバックスラッシュで統一しているからバインドマウントに失敗してるんじゃないかと思い,後半のコンテナ内のパスの部分を通常のスラッシュに変更します.

PS C:\Users\ikeda\docker_tutorial\html> docker run --name second-nginx -v C:\Users\ikeda\docker_tutorial\html:/usr/share/nginx/html:ro -d -p 8081:80 nginx
docker: Error response from daemon: Conflict. The container name "/second-nginx" is already in use by container "6502055431b93e3a6110b9236349dc9a55643e34f24a58ac184473ebde54957b". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.

あー.今作ったコンテナの名前だから被ってるってまた言われてしまいました.消します.

PS C:\Users\ikeda\docker_tutorial\html> docker rm 6502055431b93e3a6110b9236349dc9a55643e34f24a58ac184473ebde54957b
Error response from daemon: You cannot remove a running container 6502055431b93e3a6110b9236349dc9a55643e34f24a58ac184473ebde54957b. Stop the container before attempting removal or force remove

起動中のコンテナは停止してから削除するか強制削除してくださいとのこと.停止して削除します.

PS C:\Users\ikeda\docker_tutorial\html> docker stop 6502055431b93e3a6110b9236349dc9a55643e34f24a58ac184473ebde54957b
6502055431b93e3a6110b9236349dc9a55643e34f24a58ac184473ebde54957b
PS C:\Users\ikeda\docker_tutorial\html> docker rm 6502055431b93e3a6110b9236349dc9a55643e34f24a58ac184473ebde54957b
6502055431b93e3a6110b9236349dc9a55643e34f24a58ac184473ebde54957b

改めてパスを修正してコンテナを作成します.

PS C:\Users\ikeda\docker_tutorial\html> docker run --name second-nginx -v C:\Users\ikeda\docker_tutorial\html:/usr/share/nginx/html:ro -d -p 8081:80 nginx
640061cfc85ca52b5bfc47bc783380a9c022f68759f23ad2cc993d1b12b39fc1

できた.想定通りです.

f:id:frazz:20210407231307p:plain

しょーもないことで悩んで時間が浪費されて心が折れるみたいな現象の打開策として書き物するってのはやはり一定の効果があるようです.わからないことを言語化して基本から押さえるっていうのが最近の目標です.いろんな診断やっても「あなたに難しいことを計画的に成し遂げる根性はないです」みたいなことばっかり書いてあって辟易してるので,しばらくこれでいってみようと思います.