During build time, I want to copy a file from the image (from folder /opt/myApp/myFile.xml), to my host folder /opt/temp
In the Dockerfile, I'm using the --mount as follows, trying to mount to my local test folder:
RUN --mount=target=/opt/temp,type=bind,source=test cp /opt/myApp/myFile.xml /opt/temp
I'm building the image successfully, but the local test folder is empty
any ideas?
Copying files from the image to the host at build-time is not supported.
This can easily be achieved during run-time using volumes.
However, if you really want to work-around this by all means, you can have a look in the custom build outputs documentation, that introduced support for this kind of activity.
Here is a simple example inspired from the official documentation:
Dockerfile
FROM alpine AS stage-a
RUN mkdir -p /opt/temp/
RUN touch /opt/temp/file-created-at-build-time
RUN echo "Content added at build-time" > /opt/temp/file-created-at-build-time
FROM scratch as custom-exporter
COPY --from=stage-a /opt/temp/file-created-at-build-time .
For this to work, you need to launch the build command using these arguments:
DOCKER_BUILDKIT=1 docker build --output out .
This will create on your host, aside the Dockerfile, a directory out with the file you need:
.
├── Dockerfile
└── out
└── file-created-at-build-time
cat out/file-created-at-build-time
Content added at build-time
Related
Hello could someone pleas help me on copying docker(I'm starter) host file into my jupyter/pyspark-notebook images. I've pulled this notebook from docker as public available.
I've created a Dockerfile which contains this.
FROM jupyter/pyspark-notebook:latest
ADD /home/abdoulaye/Documents/M2BIGDATA/Jaziri /bin/bash
I've changed /bin/bash to . but nothing is visible.
when I execute docker built it's like it copy files as shown in output below.
when I go to my my notebook I did note found folders. I check my snapshot if I can found these copied folder but I'm very confused.
In clear, I've an running notebook in my docker I use it in y navigator but I can not load data. I like to copy data in place where I can access it in notebook.
You can not copy using absoult path, the path should be relative to Dockerfile, so /home/abdoulaye/Documents/M2BIGDATA/Jaziri this path inside Dockerfile is not correct. Copy file to Dockerfile context and then copy like
ADD M2BIGDATA/Jaziri /work
Now First thing, you should not copy files from host to executable files directory.
For instance,
FROM alpine
copy hello.txt /bin/sh
If you copy like this, it will create a problem to run command inside container as sh or bash will be replaced or corrupt.
2nd, while you are building the docker image with invalid context, it should be the same where your Dockerfile is, so better to run the directory where you place the Dockerfile.
docker build -t my-jupyter .
3rd, you should not run cp command inside container to copy files from host to container.
docker cp /home/abdoulaye/Documents/M2BIGDATA/Jaziri container_id:/work
it will copy your files to /work path of the container.
I am using COPY command in my docker file on top of ubuntu 16.04. I am getting error as no such file or directory eventhough the directory is present. In the below docker file I want to copy the directory "auth" present inside workspace directory to the docker image (at path /home/ubuntu) and then build the image.
FROM ubuntu:16.04
RUN apt-get update
COPY /home/ubuntu/authentication/workspace /home/ubuntu
WORKDIR /home/ubuntu/auth
a Dockerfile COPY command can only refer to files under the context - the current location of the Dockerfile, aka .
so you have a few options now:
if it is possible to copy the /home/ubuntu/authentication/workspace/ directory content to somewhere inside your project before the build (so now it will be included in your Dockerfile context and you can access it via COPY ./path/to/content /home/ubuntu) it can be great. but sometimes you dont want it.
instead of copying the directory, bind it to your container via a volume:
when you run the container, add a -v option:
docker run [....] -v /home/ubuntu/authentication/workspace:/home/ubuntu [...]
mind that a volume is designed so any change you made inside the container dir(/home/ubuntu) will affect the bound directory on your host side (/home/ubuntu/authentication/workspace) and vice versa.
i found a something over here: this guy is forcing the Dockerfile to accept his context- he is sitting inside the /home/ubuntu/authentication/workspace/ directory, and running there
docker build . -f /path/to/Dockerfile
so now inside his Dockerfile he can refer to /home/ubuntu/authentication/workspace as his context (.)
My basic requirement is to create docker image and deploy it to docker registry.
I have a pre-configured application folder(/home/myfolder) in my jenkins server(to do this configuration I have used ansible script). Then I need to create docker image from that folder and deploy it to docker registry.
What's the best way to do this? Please help me with this as I'm new to docker.
please find my Dockerfile below
#Download base image ubuntu 16.04
FROM ubuntu
WORKDIR /dockerprojects
#copy the zip file to docker folder
COPY /wso2telcohub-4.0.0-SNAPSHOT /home/myfolder/dockerprojects/IGW/dockerCI
COPY cp /wso2is-km-5.6.0 /home/myfolder/dockerprojects/IGW/dockerCI
CMD [“bash”]
There are a bunch of things in that Dockerfile that potentially can go sideways. I will comment on them one by one here:
#Download base image ubuntu 16.04
FROM ubuntu
If you intend to use the ubuntu:16.04 image, you need to specify it. Without a specific tag, the FROM instruction will look for the latest tag, in this case the find the image ubuntu:latest for you.
WORKDIR /dockerprojects
This command sets the workdir inside the docker image, so that when the container starts, the sessions PWD will be set to /dockerprojects. This is important because all other commands durring the build and when the container is started will be relative to this location in the file structure.
#copy the zip file to docker folder
COPY /wso2telcohub-4.0.0-SNAPSHOT /home/myfolder/dockerprojects/IGW/dockerCI
This command will copy the file /wso2telcohub-4.0.0-SNAPSHOT from the "host machine", the machine where the docker image is being built, into the image at the location /home/myfolder/dockerprojects/IGW/dockerCI. If the location does not already exist in the image, then it will create a file named dockerCI at the location /home/myfolder/dockerprojects/IGW/. I don't think that this is what you want.
Also, your comment states that this is a zip file, but it seems to be missing an extension like .zip or .gz - I believe that you are not referencing the file correctly.
COPY cp /wso2is-km-5.6.0 /home/myfolder/dockerprojects/IGW/dockerCI
This instruction will not execute. For the COPY instruction you don't need to use a "cp" command. If you removed "cp" from the line however, it would try to copy the file or directory /wso2is-km-5.6.0 from the host machine (that's a file in the root of the filesystem) to the location /home/myfolder/dockerprojects/IGW/dockerCI inside the resulting image.
CMD [“bash”]
The CMD instruction simply sets the image to start a new bash shell when started - which will make the container exit immediatly when the bash command completes.
I have a feeling that the source location of the files that you want to put in the image is not in the root of the host machine. But probably at /home/myfolder/dockerprojects/ on the host that you mention. I have asked you to clearify the location of the files that you want in the image in a comment on your question.
Update
The error that you are getting 'no such file or directory' means that the source file that you are referencing in the COPY instruction, does not exist.
The COPY instruction works like this:
COPY <sourcepath> <targetpath>
Where the <sourcepath> is the path of the file on the machine where the image is being built. This path is relative to the Dockerfile (or the build context if specified), unless it starts with a /, then it is relative to the root of the filesystem on the host machine. And the targetpath is the desired path inside the resulting image.
Let's say that I have the following folder structure:
/home/myfolder/dockerprojects/
├── Dockerfile
├── wso2telcohub-4.0.0-SNAPSHOT.zip
├── wso2is-km-5.6.0/
│ ├── anotherfile.txt
And I wanted all the files in the path /home/myfolder/dockerprojects/ to be put inside the docker image, below the path /app. I would do this with a Dockerfile like:
FROM ubuntu:16.04
WORKDIR /app
COPY . /app/
Or each file individually like this:
FROM ubuntu:16.04
WORKDIR /app
COPY ./wso2telcohub-4.0.0-SNAPSHOT.zip /app/wso2telcohub-4.0.0-SNAPSHOT.zip
COPY ./wso2is-km-5.6.0 /app/wso2is-km-5.6.0
That would leave me with the following in the docker image:
/app/
├── Dockerfile
├── wso2telcohub-4.0.0-SNAPSHOT.zip
├── wso2is-km-5.6.0/
│ ├── anotherfile.txt
I have a project which is Maven based multi module project
It has various modules with in, like common-utils, web, theme, etc, etc
And also in the root location it has the Dockerfile which is not default one but I have named it Dockerfile.cli due to some requirements
Dockerfile.cli contents here:-
FROM tomcat
ENV NEUW_LOG_HOME /neuw/web/logs
RUN echo "running the image, making it a container :-)"
RUN mkdir -p "/neuw/web/theme-v4"
COPY web/target/web.war /usr/local/tomcat/webapps/ROOT.war
COPY theme-v4 /neuw/web/theme-v4
CMD ["catalina.sh", "jpda", "run"]
Now why I am here -> am getting the below error while building the image:-
COPY failed: stat /var/lib/docker/tmp/docker-builder838877607/web/target/web.war: no such file or directory
The command I use to run the image build is like below and running it on the root of the project which contains both theme and web folder:-
docker build -f Dockerfile.cli -t neuw/web:snapshot-30 .
Any hints and help for the issue?
Which directory are you in when you run this command? Could you do ls /web/target/ from that directory? I ask because I think your Dockerfile is expecting to find a web.war in ./web/target relative to the directory you are running in.
Edit (to save anyone digging through the comments on this): The target directory did contain the file but it was invisible to docker due to a .dockerignore file with **/target.
I am new to docker.
I would like to understand the following questions. I have been searching but I can't find the answers to my questions.
Why do I always get a wrong path when I tried to copy the file?
Does that mean I can only copy the files into the docker image from the same directory where I have my dockerfile? Is there a way to COPY files from other directories on the host?
Is there a way to passing in host's environment variables directly in the Dockerfile without using "ARG" and --build-arg flag?
Below is what I currently have
file structure is like this:
/home/user1/docker
|__ Dockerfile
In the Dockerfile:
From
ARG BLD_DIR=/tmp
RUN mkdir /workdir
WORKDIR /workdir
COPY ${BLD_DIR}/a.file /workdir
I ran
root#localhost> echo $BLD_DIR
/tmp/build <-- BLD_DIR is a custom variable; meaning it's different on each dev env
docker build --build-arg BLD_DIR=${BLD_DIR} -t docker-test:1.0 -f Dockerfile
Always got error like
COPY failed: stat
/var/lib/docker/tmp/docker-builder035089075/tmp/build/a.file: no such file
or directory
In a Dockerfile, you can only copy files that are available in the current Docker build context.
By default, all files in the directory where you run your docker build command are copied to the Docker context folder.
So, when you use ADD or COPY commands, all your paths are in fact relative to build folder, as the documentation states:
Multiple resources may be specified but the paths of files and directories will be interpreted as relative to the source of the context of the build.
This is voluntary because building an image using docker build should not depend on auxiliary files on your system: the same Docker image should not be different if built on 2 different machines.
However, you can have a directory structure like such:
/home/user1/
|___file1
|___docker/
|___|___ Dockerfile
If you run docker build -t test -f docker/Dockerfile . in the /home/user1 folder, your build context will be /home/user1, so you can COPY file1 in your Dockerfile.
For the very same reason, you cannot use environment variables directly in a Dockerfile. The idea is that your docker build command should "pack" all the information needed to generate the same image on 2 different systems.
However, you can hack your way around it using docker-compose, like explaned here: Pass host environment variables to dockerfile.