14
9

More than 3 years have passed since last update.

dockerfileで親ディレクトリを参照したい!

Posted at

はじめに

会社の研修で詰まったところをメモ程度に書いていく

やりたかったこと

以下のようなディレクトリ構造の時、dokerfileから親ディレクトリのindex.htmlをCOPYしたかった

ディレクトリ構造
content/
 │
 ├ .infrastructure/
 │   ∟ dockerfile
 │
 ├ files/
     ∟ index.html
dockerfile
FROM: nginx:latest
COPY: ../files/ /usr/share/nginx/html
WORKDIR /usr/share/nginx/html

これで.infrastructureディレクトリで docker buildを実行。

実行結果

.infrastructure $ docker build . -t docker-html
-- 実行結果 --
Sending build context to Docker daemon  3.072kB
Step 1/2 : FROM nginx:latest
 ---> 9beeba249f3e
Step 2/2 : COPY ../files/  /usr/share/nginx/html
COPY failed: Forbidden path outside the build context: ../files/ ()

このようにCOPY failed: Forbidden path outside the build context: ../files/ ()と表示され親ディレクトリを参照できない

解決方法

-f オプションを使って、contentディレクトリからdocker buildコマンドを実行する
以下の通り

# docker build -f [dockerfileの指定] -t [イメージ名] [イメージを作成する時に参照するディレクトリ]
$ docker build -f .infrastructure/dockerfile -t image_name .

注意点

最後の.忘れないように! 
今回の場合はカレントディレクトリからimageを作成するため.

まとめ

dockerfileから別のディレクトリのファイルを参照することはできない。
なので、親ディレクトリから-fオプションを用いてコマンドを実行すれば良い。

14
9
1

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
14
9