DockerfileのCOPYの動きを確認してみる
DockerfileのCOPY命令がファイルの内容に変化がなければキャッシュを使うことは分るが・・・ファイルが消えた時等はどうなるのだろう・・・ということで試してみた。
1.サンプルファイル
Dockerfile src\ a.txt b.txt c.txt
Dockerfileの内容:
FROM alpine:latest COPY src dst
2.初回ビルド
$ docker build -t copytest . Sending build context to Docker daemon 4.096kB Step 1/2 : FROM alpine:latest latest: Pulling from library/alpine 4fe2ade4980c: Already exists Digest: sha256:621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528 Status: Downloaded newer image for alpine:latest ---> 196d12cf6ab1 Step 2/2 : COPY src dst ---> 7a3d51886012 Successfully built 7a3d51886012 Successfully tagged copytest:latest
COPY結果を確認してみる。
$ docker run --rm copytest ls -l /dst total 0 -rwxr-xr-x 1 root root 0 Oct 27 14:25 a.txt -rwxr-xr-x 1 root root 0 Oct 27 14:25 b.txt -rwxr-xr-x 1 root root 0 Oct 27 14:25 c.txt
3.2回目ビルド
ホスト環境を何も変えず、2回目を実行してみる。
$ docker build -t copytest . Sending build context to Docker daemon 4.096kB Step 1/2 : FROM alpine:latest ---> 196d12cf6ab1 Step 2/2 : COPY src dst ---> Using cache ---> 7a3d51886012 Successfully built 7a3d51886012 Successfully tagged copytest:latest
COPYは実行されず、キャッシュが使われている。よって、COPY結果も変わらず。
4.ファイルを追加する
まずはファイルを追加してみる。
Dockerfile src\ a.txt b.txt c.txt d.txt <- ファイルを追加する
ビルドを実行してみると・・・
$ docker build -t copytest . Sending build context to Docker daemon 4.608kB Step 1/2 : FROM alpine:latest ---> 196d12cf6ab1 Step 2/2 : COPY src dst ---> 4584e2974830 Successfully built 4584e2974830 Successfully tagged copytest:latest
キャッシュは使われずに、COPYが実行されている。結果を確認してみる。
$ docker run --rm copytest ls -l /dst total 0 -rwxr-xr-x 1 root root 0 Oct 27 14:25 a.txt -rwxr-xr-x 1 root root 0 Oct 27 14:25 b.txt -rwxr-xr-x 1 root root 0 Oct 27 14:25 c.txt -rwxr-xr-x 1 root root 0 Oct 27 14:25 d.txt
5.追加したファイルを削除する
先ほど追加した d.txt を削除して、ビルドを実行してみると、
$ docker build -t copytest . Sending build context to Docker daemon 4.096kB Step 1/2 : FROM alpine:latest ---> 196d12cf6ab1 Step 2/2 : COPY src dst ---> Using cache ---> 7a3d51886012 Successfully built 7a3d51886012 Successfully tagged copytest:latest
予想に反して(?)、(d.txtを追加する前に作成した)キャッシュが使われている。賢い^^;
6.さらにファイルを削除する
さらに c.txt を削除して、ビルドを実行してみると、
$ docker build -t copytest . Sending build context to Docker daemon 3.584kB Step 1/2 : FROM alpine:latest ---> 196d12cf6ab1 Step 2/2 : COPY src dst ---> c8f23fe46a11 Successfully built c8f23fe46a11 Successfully tagged copytest:latest
ファイルが消えたことを検知して、COPYが実行された。dstの内容は・・・
$ docker run --rm copytest ls -l /dst total 0 -rwxr-xr-x 1 root root 0 Oct 27 14:25 a.txt -rwxr-xr-x 1 root root 0 Oct 27 14:25 b.txt
期待通りの動きだ^^