概要
上記書籍を参考にDockerfileを作る→ビルドするという流れで遊んでいたら
思いの外ハマったのでそのときの解決方法とかを共有します
実行環境
・PC
MacBook Pro (13-inch, 2020, Four Thunderbolt 3 ports)
M1じゃないやつです
・Docker
Version:20.10.6
#遭遇したエラー①
hogehoge ~ % docker build -t test/apache .
[+] Building 0.0s (1/2)
=> ERROR [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 40B 0.0s
------
> [internal] load build definition from Dockerfile:
------
failed to solve with frontend dockerfile.v0: failed to read dockerfile: error from sender: open .Trash: operation not permitted
原因
ホームディレクトリにDockerfileが存在している
解決方法
参考書籍では mkdir document
でディレクトリを作成しているので、 document
にファイルを移動した
遭遇したエラー②
hogehoge document % docker build -t test/apache .
[+] Building 2.2s (9/9) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 37B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:latest 2.0s
=> [auth] library/centos:pull token for registry-1.docker.io 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 2B 0.0s
=> CANCELED [1/4] FROM docker.io/library/centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1 0.0s
=> => resolve docker.io/library/centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1 0.0s
=> => sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1 762B / 762B 0.0s
=> => sha256:dbbacecc49b088458781c16f3775f2a2ec7521079034a7ba499c8b0bb7f86875 529B / 529B 0.0s
=> => sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55 2.14kB / 2.14kB 0.0s
=> CACHED [2/4] RUN yum -y update 0.0s
=> CACHED [3/4] RUN yum -y install httpd 0.0s
=> ERROR [4/4] ADD document/index.html /var/www/html/index.html 0.0s
------
> [4/4] ADD document/index.html /var/www/html/index.html:
------
failed to compute cache key: failed to walk /var/lib/docker/tmp/buildkit-mount776233207/document: lstat /var/lib/docker/tmp/buildkit-mount776233207/document: no such file or directory
Dockerfileの内容を調べてみる
hogehoge document % cat Dockerfile
FROM centos
RUN yum -y update
RUN yum -y install httpd
ADD document/index.html /var/www/html/index.html
EXPOSE 80
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
どうやらこの記事に書いてあることが怪しい
【docker】ビルドコンテキスト(build context)とは?
内容を私が理解した形で書くと、docker build時にコピーするファイル(=ビルドコンテキスト)
を指定するときは相対パス(カレントディレクトリからのパス)で指定する必要があるらしい
Dockerfileの5行目は ADD document/index.html /var/www/html/index.html
としているので、これは間違っているということになる
Dockerfileの内容を修正してみる
hogehoge document % vim Dockerfile
hogehoge document % cat Dockerfile
FROM centos
RUN yum -y update
RUN yum -y install httpd
ADD index.html /var/www/html/index.html
EXPOSE 80
CMD ["/usr/sbin/httpd", "-D", "FOREGROUND"]
hogehoge document % docker build -t test/apache .
[+] Building 32.0s (10/10) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 193B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/centos:latest 2.1s
=> [auth] library/centos:pull token for registry-1.docker.io 0.0s
=> [1/4] FROM docker.io/library/centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1 6.1s
=> => resolve docker.io/library/centos@sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1 0.0s
=> => sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1 762B / 762B 0.0s
=> => sha256:dbbacecc49b088458781c16f3775f2a2ec7521079034a7ba499c8b0bb7f86875 529B / 529B 0.0s
=> => sha256:300e315adb2f96afe5f0b2780b87f28ae95231fe3bdd1e16b9ba606307728f55 2.14kB / 2.14kB 0.0s
=> => sha256:7a0437f04f83f084b7ed68ad9c4a4947e12fc4e1b006b38129bac89114ec3621 75.18MB / 75.18MB 2.4s
=> => extracting sha256:7a0437f04f83f084b7ed68ad9c4a4947e12fc4e1b006b38129bac89114ec3621 3.4s
=> [internal] load build context 0.0s
=> => transferring context: 53B 0.0s
=> [2/4] RUN yum -y update 19.0s
=> [3/4] RUN yum -y install httpd 3.6s
=> [4/4] ADD index.html /var/www/html/index.html 0.0s
=> exporting to image 1.0s
=> => exporting layers 1.0s
=> => writing image sha256:5eb40198b5fc8aabb0ff6e794402522c6686c20827cc0373bb8c60ddc4281835 0.0s
=> => naming to docker.io/test/apache 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
ビルドできたっぽい
docker imagesを実行してみる
hogehoge document % docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
test/apache latest 5eb40198b5fc About a minute ago 358MB
おお、できてる
おわりに
人によっては結構ハマる気がするので、この問題でハマったの参考になれば嬉しいです。
私は2時間ほど格闘してました。