LoginSignup
5
3

More than 5 years have passed since last update.

Dockerfile : COPY対象のファイルの存在が不確定の場合への対応

Last updated at Posted at 2019-01-17

同一コンテナでもデプロイ環境が異なると、confファイルが異なったり、存在しなかったりする。
confファイルがあってもなくても、同一のdockerfileでbuildしたい。
COPY対象ファイルが存在しない場合でも問題なくdocker buildする方法を調べた時のメモ。

COPY対象ファイルがない場合のdocker build

COPY対象ファイルsample.confが存在しない場合の例

dockerCOPYdirectory.png

Dockerfile
FROM amazonlinux
COPY sample.conf /etc/
CMD ["cat", "/etc/sample.conf"]

実行結果
dockerCOPY001.png

対応

COPY対象として、必ず存在するファイルを1つ指定。サンプルではDockerfile。
存在が不確定なファイルはワイルドカードで指定。

Dockerfile
FROM amazonlinux
COPY Dockerfile sample.conf* /etc/
CMD ["cat", "/etc/sample.conf"]

実行結果
dockerCOPY002.png

Dockerfileはコピーされてしまうので、必要であれば削除するなどの後処理を追加する。

参考

ワイルドカードなしの場合もエラーになる

Dockerfile
FROM amazonlinux
COPY Dockerfile sample.conf /etc/
CMD ["cat", "/etc/sample.conf"]

dockerCOPY003.png

ワイルドカードのみの場合もエラーになる

Dockerfile
FROM amazonlinux
COPY sample.conf* /etc/
CMD ["cat", "/etc/sample.conf"]

dockerCOPY004.png

5
3
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
5
3