I am trying to copy file from docker container to host. I have attached the snapshot in which I listed all the containers as well as container file structure. But when I execute docker cp, it says it could not find the file. Can anyone tell me what wrong am I doing or if I am missing something. I am very new to docker so please guide me through.
I think you need to absolute path to the file inside the container. See the following.
Copying files from Docker container to host
docker cp <CONTAINER>:/<ABS_PATH>/procfile .
As documented for the docker cp command:
The docker cp command assumes container paths are relative to the
container’s / (root) directory
Thus CONTAINER:file.txt is equivalent to CONTAINER:/file.txt as paths are interpreted relative to the root /
Related
I have a docker image, and I am running it now (finishing with bash)
When I do, I have a file structure inside the container.
However, this is not some file structure mapped (with -v) from outside the container. These files and folders exist only inside the container.
My question is, since it is bothersome to be opening each file with vi and navigating from the terminal, is there a way that I can open vscode on these files?
Be aware that these files do not exist outside the container
I found how to do it from this link
However I used the "attach to running container" command
I rarely do that but when I have to I usually mount an empty volume to the container, then exec into the container copy the folder which I need into that empty volume, which is then replicated on my host machine. From my host machine I then open it in vscode.
However please be careful if you have sensitive information in that container, not to expose something by accident.
So the steps are:
Create empty volume ( docker-compose example )
Note do not overwrite the folder/file which you want to extract. containerpath is path which does not exist in the container prior to creating it.
volume:
- ./hostpath:/containerpath
Find docker id so that you can use it to exec into it:
docker ps
Exec into the container:
docker exec -it <container_id> /bin/sh
Copy the file/folder to that empty volume:
cp -r folder containerpath
Exit the container and look at your files in ./hostpath folder.
The use case is that I want to download and image that contains python code files. Assume the image does not have any text editor installed. So i want to mount a drive on host, so that files in the container show up in this host mount and i can use different editors installed on my host to update the code. Saving the changes are to be reflected in the image.
if i run the following >
docker run -v /host/empty/dir:/container/folder/with/code/files -it myimage
the /host/empty/dir is still empty, and browsing the container dir also shows it as empty. What I want is the file contents of /container/folder/with/code/files to show up in /host/empty/dir
Sébastien Helbert answer is correct. But there is still a way to do this in 2 steps.
First run the container to extract the files:
docker run --rm -it myimage
In another terminal, type this command to copy what you want from the container.
docker cp <container_id>:/container/folder/with/code/files /host/empty/dir
Now stop the container. It will be deleted (--rm) when stopped.
Now if you run your original command, it will work as expected.
docker run -v /host/empty/dir:/container/folder/with/code/files -it myimage
There is another way to access the files from within the container without copying it but it's very cumbersome.
Your /host/empty/dir is always empty because the volume binding replaces (overrides) the container folder with your empty host folder. But you can not do the opposite, that is, you take a container folder to replace your host folder.
However, there is a workaround by manually copying the files from your container folder to your host folder. before using them as you have suggested.
For exemple :
run your docker image with a volume maaping between you host folder and a temp folder : docker run -v /host/empty/dir:/some-temp-folder -it myimage
copy your /container/folder/with/code/files content into /some-temp-folder to fill you host folder with you container folder
run you container with a volum mapping on /host/empty/dir but now this folder is no longer empty : run -v /host/empty/dir:/container/folder/with/code/files -it myimage
Note that steps 1 & 2 may be replaced by : Copying files from Docker container to host
I created own Dockerfile, during building I inserted to /opt/wilfly/log my log4j.xml.
Now I need create volume /mnt/data/logs/application:/opt/wildfly/log
I run command
sudo docker run --name=myapp -v /mnt/data/logs/application:/opt/wildfly/log -d -i -t application
But when I look in docker container, folder /opt/wilfly/log is empty. In this folder should by log4j.xml.
Thank you.
Maybe you should move it into another directory.
For example move log4j.xml to /opt/wilfly/ and set logging path to /opt/wilfly/log.
When you run the container, log4j.xml will not disappear.
When you mount the data, the folder from your host "override" your mounted folder within the container.
Thus, there are some options you can do:
copy the log4j.xml into your local /mnt/data/logs/application folder and run the container as you did.
remove the -v /mnt/data/logs/application:/opt/wildfly/log and use the original log4j.xml that you were added during the image build.
Please note that you can also mount only the file if you like (rather than the entire floder): -v /mnt/data/logs/application/log4j.xml:/opt/wildfly/log/log4j.xml but it won't change the behavior - the file from your host will be mounted into the container and not in the opposite direction.
It may sound trivial but I couldn't find a easy way to copy multiple files into the root folder of a docker volume. I am using Ubuntu Xenial 16.04 and Docker 1.12.1. For example if I have an Ubuntu container with the volume /my_data:
docker run --name my_container -v /my_data -d ubuntu:latest
In my host machine I have a folder called /tmp/my_data/ with multiple files inside, and I would like to copy all those files into the volume /my_data in my_container. I have tried the following approaches but none of them work:
docker cp /tmp/my_data my_container:/
docker cp /tmp/my_data/* my_container:/my_data/
Does someone know a work around for this issue?
Actually it was easier than I though, just need to add a dot in the host path and it will work as expected, copying all files and folders within /my_data folder
docker cp /tmp/my_data/. my_container:/my_data
As a workaround you can create a loop:
for i in /tmp/my_data/*;do docker cp /tmp/my_data/"$i" my_container:/my_data/;done
*Note: The specific workaround wont copy hidden files or folders inside the my_data folder.
I used to inspect docker's container file using docker inspect -f '{{.Id}}' container_id, then the container's files are located in /var/lib/docker/aufs/mnt/{container_full_id}(I didn't change the default configuration, so it's the default location), the version of docker is 1.6.1 at that time.
But now, I'm running docker 1.10.3, I used the same command docker inspect -f '{{.Id}}' container_id, but I can't find the folder in the location that it used to be, nor in /var/lib/docker/aufs/diffs/{container_full_id}.
Is it docker modify its folder structure, or is there anything wrong with my commands?
Any help will be appreciated. Thanks.
by writing "docker inspect containername" You should get full JSON info about container. There is some info about the paths like ResolvConfPath (so the location of the container files as well)
docker-layer provide an easy way to find a container's filesystem root mount :
# docker-layer -m musing_wiles
/var/lib/docker/aufs/mnt/c83338693ff190945b2374dea210974b7213bc0916163cc30e16f6ccf1e4b03f
Edit 2018-03-28 :
docker-layer has been replaced by docker-backup