My directory Structure as follows
Dockerfile downloads
I want to add downloads to /tmp
ADD downloads /tmp/
COPY down* /tmp
ADD ./downloads /tmp
Nothings works. It copies the contents of downloads into tmp. I want to copy the downloads floder. Any idea?
ADD . tmp/
copies Dockerfile also. i dont want to copy Dockerfile into tmp/
I believe that you need:
COPY downloads/ /tmp/downloads/
That will copy the contents of the downloads directory into a directory called /tmp/downloads/ in the image.
Note: The directory itself is not copied, just its contents
From the dockerfile reference about COPY and ADD, it says Note: The directory itself is not copied, just its contents., so you have to specify a dest directory explicitly.
RE: https://docs.docker.com/engine/reference/builder/#copy
e.g.
Copy the content of the src directory to /some_dir/dest_dir directory.
COPY ./src /some_dir/dest_dir/
You can use:
RUN mkdir /path/to/your/new/folder/
COPY /host/folder/* /path/to/your/new/folder/
I could not find a way to do it directly with only one COPY call though.
The best for me was:
COPY . /tmp/
With following .dockerignore file in root
Dockerfile
.dockerignore
# Other files you don't want to copy
This solution is good if you have many folders and files that you need in container and not so many files that you don't need.
Otherwise user2807690' solution is better.
First tar the directory you want to ADD as a single archive file:
tar -zcf download.tar.gz download
Then ADD the archive file in Dockerfile:
ADD download.tar.gz tmp/
If you folder does not end with a /it is considered a file, so you should something like write ADD /abc/ def/ if you want to copy a folder.
Related
I have a folder tmpdata with slightly different permissions at the top level of my project, which is used by a separate container to be able to restart with data.
I list this tmpdata/ folder in my .dockerignore file because I don't want it to be part of the build for the main app.
In my Dockerfile, I do have a COPY . . directive though. I thought it would copy everything except whatever's listed in .dockerignore, but when I try to build, I get:
failed to solve with frontend dockerfile.v0: failed to read dockerfile: error from sender: open tmpdata: permission denied
My question is, why isn't the .dockerignore file preventing the build process from even trying to touch this folder? Is there a way to have the COPY command respect (ignore) entries in the .dockerignore file?
UPDATE even after refactoring the dockerfile to use selective COPY commands that only copy needed files, docker build still fails, complaining about the same tmpdata folder.
UPDATE 2 my docker ignore file:
.dockerignore
docker-compose.yml
.env
.env.*
.git
test/
src/test/
**/*.spec.*
**/*.test.*
**/node_modules/
npm-debug.log
.gitignore
.cache
.DS_Store
.thumbsdb
dist/
_unused/
.vscode/
**/*.md
**/.git
tmpdata/
I am trying to replace some files in a folder in a docker image. I am using the following command inside Dockerfile:
COPY /home/adietz/Work/20_BSP/Jenkins/venvs/linux_selenium/lib/python2.7/site-packages/browsermobproxy /usr/local/lib/python2.7/dist-packages/browsermobproxy
which results in an error
Step 4/12 : COPY /home/adietz/Work/20_BSP/Jenkins/venvs/linux_selenium/lib/python2.7/site-packages/browsermobproxy /usr/local/lib/python2.7/dist-packages/browsermobproxy
lstat home/adietz/Work/20_BSP/Jenkins/venvs/linux_selenium/lib/python2.7/site-packages/browsermobproxy: no such file or directory
Replacing COPY with ADD results in the same error. Also the following command
COPY /home/adietz/Work/20_BSP/Jenkins/venvs/linux_selenium/lib/python2.7/site-packages/browsermobproxy /usr/local/lib/python2.7/dist-packages/
and gives identical(!) error.
Both paths are folders. The folder in the docker image already exists; I just want to replace the files.
What am I doing wrong here...?
It seems you cannot use absolute paths in the COPY command AND you can only copy files which are inside the folder you are running the docker command.
So to copy these files you have to do e.g. the following steps
cp -r /home/adietz/Work/20_BSP/Jenkins/venvs/linux_selenium/lib/python2.7/site-packages/browsermobproxy .
and then add to the Dockerfile:
COPY browsermobproxy/ /usr/local/lib/python2.7/dist-packages/
A symbolic link also does not work...
I had this same issue and realized that COPY or ADD won't work with node_modules that are referenced instead of directly installed into the project. When I switched this it worked for me.
Is it possible to copy multiple files to different locations in a Dockerfile?
I'm looking to go from:
COPY outputs/output/build/.tools /root/.tools
COPY outputs/output/build/configuration /root/configuration
COPY outputs/output/build/database/postgres /root/database/postgres
I have tried the following, but no joy:
COPY ["outputs/output/build/.tools /root/.tools","outputs/output/build/configuration /root/configuration","outputs/output/build/database/postgres /root/database/postgres"]
Not sure if this is even possible.
Create a file .dockerignore in your docker build context directory. Create a soft link (ln) of the root directory you want to copy and exclude the directories you don't want in your .dockerignore.
In your case, you don't need the soft link as the directory is already in the docker build context. Now, add the directories in the .dockerignore file that you don't want eg. if you don't want bin directory you can do that as,
# this is .dockerignore file
outputs/output/build/bin*
Finally in your Dockerfile,
COPY outputs/output/build/ /root/
Details are here.
it looks like you are trying to do bash style [] brace expansion
RUN command uses sh NOT bash
see this SO article discussing it
Bash brace expansion not working on Dockerfile RUN command
or the docker reference
https://docs.docker.com/engine/reference/builder/#/run
I have to create a docker file that copies MyApp directory into the image.
MyApp
-libs
-classes
-resources
Libs directory has around 50 MB and it is not frequently changing where as Classes directory is around 1 MB and it is subjected to frequent changes. To optimize the docker build, I planned to add Libs directory at the beginning of the Dockerfile and add other directories at the end of the Dockerfile. My current approach is like this
ADD MyApp/libs /opt/MyApp/libs
## do other operations
ADD MyApp/classes /opt/MyApp/resources
ADD MyApp/classes /opt/MyApp/classes
This is not a maintainable format as in future I may have some other directories in the MyApp directory to be copied into the docker image. My target is to write a docker file like this
ADD MyApp/libs /opt/MyApp/libs
## do other operations
ADD MyApp -exclude MyApp/libs /opt/MyApp
Is there a similar command to exclude some files in a directory which is copied into the docker image?
I considered the method explained by #nwinkler and added few steps to make the build consistent.
Now my context directory structure is as follows
-Dockerfile
-MyApp
-libs
-classes
-resources
-.dockerignore
-libs
I copied the libs directory to the outer of the MyApp directory. Added a .dockerignore file which contains following line
MyApp/libs/*
Updated the Dockerfile as this
ADD libs /opt/MyApp/libs
## do other operations
ADD MyApp /opt/MyApp
Because dockerignore file ignores MyApp/lib directory, there is no risk in over-writing libs directory I have copied earlier.
This is not possible out of the box with the current version of Docker. Using the .dockerignore file will also not work, since it would always exclude the libs folder.
What you can do is wrapping your docker build in a shell script and copy the MyApp folder (minus the libs folder), and the libs folder into temporary directories before calling docker build.
Something like this:
#!/usr/bin/env bash
rm -rf temp
mkdir -p temp
# Copy the whole folder
cp -r MyApp temp
# Move the libs folder up one level
mv temp/MyApp/libs temp
# Now build the Docker image
docker build ...
Then you could change your Dockerfile to copy from the temp directory:
ADD temp/libs /opt/MyApp/libs
## do other operations
ADD temp/MyApp /opt/MyApp
There's a risk that the second ADD command will remove the /opt/MyApp/libs folder from the image. If that happens, you might have to reverse the ADD commands and add the libs folder after everything else.
I have read http://docs.docker.com/engine/reference/builder/#add however I met a problem. I want to copy the local directory go to docker /user/local/
I tried:
ADD go /usr/local/
and:
ADD /go/ /usr/local/
also:
RUN chmod 0755 /usr/local/go/src/make.bash
However, I see the following error message:
/usr/local/go/src/make.bash: No such file or directory
but the local go directory does contain make.bash.
ADD go /usr/local/
will copy the contents of your local go directory in the /usr/local/ directory of your docker image.
To copy the go directory itself in /usr/local/ use:
ADD go /usr/local/go
or
COPY go /usr/local/go
Indeed ADD go /usr/local/ will add content of go folder and not the folder itself, you can use Thomasleveil solution or if that did not work for some reason you can change WORKDIR to /usr/local/ then add your directory to it like:
WORKDIR /usr/local/
COPY go go/
or
WORKDIR /usr/local/go
COPY go ./
But if you want to add multiple folders, it will be annoying to add them like that, the only solution for now as I see it from my current issue is using COPY . . and exclude all unwanted directories and files in .dockerignore, let's say I got folders and files:
- src
- tmp
- dist
- assets
- go
- justforfun
- node_modules
- scripts
- .dockerignore
- Dockerfile
- headache.lock
- package.json
and I want to add src assets package.json justforfun go so:
in Dockerfile:
FROM galaxy:latest
WORKDIR /usr/local/
COPY . .
in .dockerignore file:
node_modules
headache.lock
tmp
dist
In this way, you ignore node_modules headache.lock tmp dist so they will not be added!
Or for more fun (or you like to confuse more people make them suffer as well :P) can be:
*
!src
!assets
!go
!justforfun
!scripts
!package.json
In this way you ignore everything, but exclude what you want to be copied or added only from the "ignore list".
It is a late answer but adding more ways to do the same covers even more cases.
You can use COPY. You need to specify the directory explicitly. It won't be created by itself
COPY go /usr/local/go
Reference: Docker CP reference
As the official docs state:
The directory itself is not copied, just its contents.
The trick is to concat in the <dest> path also the folder name, like this:
COPY src ./src
Even if ./src does not exist in the container yet, the command COPY internally creates it and copies the content of src into the new folder (which is ./src).
This can help if you want to add all files to a specified location
#ADD XML SUITE files
ADD src/test/resources/xmls/* /usr/share/tag/