0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Docker for Mac の不思議なパス解決の話

Last updated at Posted at 2021-04-21

forbidden path outside the build context: .. () ってなんやねん

みなさん、Dockerfile 内で、 COPY .. <path-to-image-dir> として、

Step 5/6 : COPY .. /memo_dir
COPY failed: forbidden path outside the build context: .. ()

と怒られた経験、ございませんか?

僕はつい先程怒られてカチきれてました。

このエラーは何を言っているのかというと、”オメーどうして、docker (docker-compose )が支配しているフォルダの外を見ようとしてンだ” ということです。

これは、例えばプロジェクト構造が、

project_A
  |- docker-files
  |   `- docker-compose.yml
  |- go.mod
  `- app
      `- main.go

みたいなときによく起こるエラーのようで、下記で紹介するような docker-compose x dockerfile の書き方をすると起こります。

version: "3.9"
services:
  print_memo:
    build:
      context: ./../
      dockerfile: workspace/app/Dockerfile
FROM alpine:latest
MAINTAINER MokkeMeguru <meguru.mokke@gmail.com>
LABEL version 20-10-5
WORKDIR /memo_dir
## .. を見に行っているので、linux ではエラー
COPY .. /memo_dir 
ENTRYPOINT ["/bin/cat", "memo.txt"]

この問題の厄介なところは、 Docker for Mac ではエラーが起きない という点です。

なので、Cloud Run などへデプロイする際にぶっ壊れる可能性がちょっとあるわけです。

検証

検証用コード https://github.com/MokkeMeguru/docker-path-test

比較するのは、 workspace/app/dockerfileworkspace-for-linux/app/dockerfile です。

  • workspace/app/dockerfile
FROM alpine:latest
MAINTAINER MokkeMeguru <meguru.mokke@gmail.com>
LABEL version 20-10-5
WORKDIR /memo_dir
COPY .. /memo_dir
ENTRYPOINT ["/bin/cat", "memo.txt"]
  • workspace-for-linux/app/dockerfile
FROM alpine:latest
MAINTAINER MokkeMeguru <meguru.mokke@gmail.com>
LABEL version 20-10-5
WORKDIR /memo_dir
COPY . /memo_dir
ENTRYPOINT ["/bin/cat", "memo.txt"]

docker-compose.yml は共通して次のようなものを使います。

version: "3.9"
services:
  print_memo:
    build:
      # context で上の階層を支配域に加えている 
      context: ./../
      dockerfile: workspace-for-linux/app/Dockerfile

MacOS

以下環境で検証しました。

os: macos
docker: 20.10.5
docker-compose: 1.29.0 

実行すると、次のような感じになります。

$ cd workspace
$ docker-compose build  
$ docker-compose run print_memo 
Creating network "workspace_default" with the default driver
Creating workspace_print_memo_run ... done
pwd is ./docker-path-test

$ cd ..
$ cd workspace-for-linux
$ docker-compose build  
$ docker-compose run print_memo        
Creating network "workspace-for-linux_default" with the default driver
Creating workspace-for-linux_print_memo_run ... done
pwd is ./docker-path-test

なんと同じフォルダを見に行きます。面白いですね ()

Linux

以下環境で検証しました。
https://github.com/MokkeMeguru/docker-path-test/runs/2397135788
https://github.com/MokkeMeguru/docker-path-test/actions/runs/769480930

os: ubuntu-20.04
docker: 20.10.5
docker-compose: 1.29.0
# workspace
Step 5/6 : COPY .. /memo_dir
COPY failed: forbidden path outside the build context: .. ()
Service 'print_memo' failed to build
Error: Process completed with exit code 1.
# workspace-for-linux
Creating network "workspace-for-linux_default" with the default driver
Creating workspace-for-linux_print_memo_run ... 
Creating workspace-for-linux_print_memo_run ... done
pwd is ./docker-path-test

forbidden path outside the build context: .. () が表れていることが確かに確認できると思います。ハハッ

対策

COPY .. <path> は極力使わないようにしよう

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?