Docker: adding a file from a parent directory

 

In my Dockerfile I've got :

ADD ../../myapp.war /opt/tomcat7/webapps/

That file exists as ls ../../myapp.war returns me the correct file but when I execute sudo docker build -t myapp . I've got :

Step 1 : ADD ../../myapp.war /opt/tomcat7/webapps/
2014/07/02 19:18:09 ../../myapp.war: no such file or directory

Does somebody know why and how to do it correctly?

share improve this question
 

3 Answers

up vote 21down voteaccepted

Unfortunately, (for practical and security reasons I guess), if you want to add/copy local content, it must be located under the same root path than the Dockerfile.

From the documentation:

The <src> path must be inside the context of the build; you cannot ADD ../something/something, because the first step of a docker build is to send the context directory (and subdirectories) to the docker daemon.

EDIT: There's now an option (-f) to set the path of your Dockerfile ; it can be used to achieve what you want, see @Boedy 's response nelow.

share improve this answer
 
5 
Is there a "clean" workaround for this? I'd rather not restructure my whole project directory just to accommodate this. – ben_frankly Nov 21 '14 at 18:35
   
As said by @Günter, there is a workaround here superuser.com/a/842690/136024 ... is it really "clean"? Well at least it's a "workaround" :) – Anthony O. Dec 2 '14 at 8:39
   
See better answer from @Boedy stackoverflow.com/a/34300129/2950621 – nmgeek Jun 19 at 19:27

You can build the Dockerfile from the parent directory:

docker build -t <some tag> -f <dir/dir/Dockerfile> .
share improve this answer
 
   
Worked smoothly ;) – Souciance Eqdam Rashti Mar 2 at 10:51
   
Thank you! This works fine on a local box, but Docker Hub fails to build the image since it tries to do it from its same directory (tbh, just what one would normally expect). Is there any way to do the same trick in Docker Hub? – Marcel Hernandez Mar 25 at 20:31
   
Not that I know of. You could push the image to the registry instead of using automated build. – Boedy Mar 29 at 9:15 
1 
This should be marked as the right answer – Courtland Caldwell Jun 15 at 19:09

The solution for those who use composer is to use a volume pointing to the parent folder:

#docker-composer.yml

foo:
  build: foo
  volumes:
    - ./:/src/:ro

But I'm pretty sure the can be done playing with volumes in Dockerfile.

share improve this answer

转载于:https://www.cnblogs.com/kungfupanda/p/5645596.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值