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?
You can build the Dockerfile from the parent directory:
docker build -t <some tag> -f <dir/dir/Dockerfile> .
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.
With docker-compose you could set context folder:
#docker-compose.yml version: '3.3' services: yourservice: build: context: ./ dockerfile: ./docker/yourservice/Dockerfile
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.
Adding some 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's 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/
Command used to build the docker image
docker build -t test:latest -f setup/docker/Dockerfile .
Since -f caused another problem, I developed another solution.
- Create a base image in the parent folder
- Added the required files.
- Used this image as a base image for the project which in a descendant folder.
The -f flag does not solved my problem because my onbuild image looks for a file in a folder and had to call like this:
-f foo/bar/Dockerfile foo/bar
-f foo/bar/Dockerfile .
Also note that this is only solution for some cases as -f flag