Dockerで上の階層はCOPYできないので、ビルドコンテキストを上の階層にする
ディレクトリ構成がこういう場合のCOPYとビルドコンテキストについて。
app - docker - Dockerfile - src
※ ビルドコンテキストとは、docker buildコマンドを実行したときのカレントディレクトリのこと
NGパターン
下記のように、Dockerfileがあるディレクトリで実行して上の階層をCOPYしようとするとエラーになる。
Dockerfile
COPY ../
ビルドコマンド
cd app/docker docker build -t [イメージ名:タグ] .
エラー
COPY failed: Forbidden path outside the build context: ../
OKパターン
上の階層はCOPYできないため、このようにCOPYしたいディレクトリで実行しDockerfileのパスを指定すれば正しく動作する。
Dockerfile
COPY .
ビルドコマンドの場合
cd app docker build -t [イメージ名:タグ] -f docker/Dockerfile .
docker-compose.yamlの場合
build: context: ./ dockerfile: ./docker/Dockerfile