Docker: adding a file from the parent directory

In my Dockerfile , I have:

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

This file exists as ls ../../myapp.war returns the correct file to me, but when I execute sudo docker build -t myapp . , I have:

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

Does anyone know why and how to do it right?

+110
docker
Jul 02 '14 at 17:24
source share
6 answers

You can create a Docker file from the parent directory:

 docker build -t <some tag> -f <dir/dir/Dockerfile> . 
+128
Dec 15 '15 at 21:54
source share

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

From the documentation :

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

EDIT: Now there is an option ( -f ) to set the path to your Dockerfile; it can be used to achieve what you want, see @Boedy response nelow.

+84
Jul 02 '14 at 20:01
source share

With docker-compose, you can set the context folder:

 #docker-compose.yml version: '3.3' services: yourservice: build: context: ./ dockerfile: ./docker/yourservice/Dockerfile 
+50
Jul 27 '17 at 14:07
source share

The solution for those using 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 you can play with volumes in the Dockerfile .

+5
Jan 27 '16 at 13:01
source share

Since -f caused another problem, I developed a different solution.

  • Create a base image in the parent folder
  • Added necessary files.
  • This image is used as the base image for the project, which is located in the child folder.

The -f flag did not solve my problem, because my onbuild image onbuild looking for a file in the folder and had to call like this:

-f foo/bar/Dockerfile foo/bar

instead

-f foo/bar/Dockerfile .

Also note that this is only a solution for some cases like -f flag

+2
Sep 29 '16 at 20:19
source share

Adding code snippets to support the accepted answer.

Directory structure:

 setup/ |__docker/DockerFile |__target/scripts/<myscripts.sh> src/ |__<my source files> 

Docker file entry:

 RUN mkdir -p /home/vagrant/dockerws/chatServerInstaller/scripts/ RUN mkdir -p /home/vagrant/dockerws/chatServerInstaller/src/ WORKDIR /home/vagrant/dockerws/chatServerInstaller #Copy all the required files from host file system to the container file system. COPY setup/target/scripts/install_x.sh scripts/ COPY setup/target/scripts/install_y.sh scripts/ COPY src/ src/ 

The command used to create the docker image

 docker build -t test:latest -f setup/docker/Dockerfile . 
0
Feb 04 '19 at 4:20
source share



All Articles