Add project folder in docker container - docker

I want to copy my project files/folders that contains all my python-related files and subfolders to add to the docker var container
Docker container-->Files-->var

You did not provide much information, so I try to guess what you need.
I think you should use Docker volume mounting by:
docker run -v /local/project/folder:/var/project/folder # ... other options ...

Related

docker mount data from 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.

unable to understand docker cp command

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 /

Docker - access existing container files on host machine

I have a docker container which has some data in let's say /opt/files. File A and B. How can I start that container and access these files on my host machine?
I'm using Docker for Windows (Hyper-V). When i start the container with:
docker run -it -v C:/tmp:/opt/files myImage
I see an empty folder on my windows machine and inside of the container. Any new files I create there are of course reflected on both sides but how can I access files that are already in the container (e.g. because they're added in the Dockerfile)?
You can't share from inside container to host. There are two ways to do it
Copy the files from container
docker cp <containerid>:<file_path_inside_container> localpath
Share a folder other than the one where files will be generated
docker run -it -v C:/tmp:/opt/files_temp myImage
Then you get inside the container copy files from /opt/files to /opt/files_temp
Once your container is started, you can copy files inside it to your host.
Use docker cp for this (https://docs.docker.com/engine/reference/commandline/cp/).
Example : docker cp CONTAINER:SRC_PATH DEST_PATH|-

Docker add files to VOLUME

I have a Dockerfile which copies some files into the container and after that creates a VOLUME.
...
ADD src/ /var/www/html/
VOLUME /var/www/html/files
...
In the src folder is an files folder and in this files folder are some files I need to have copied to the VOLUME the first time the container gets started.
I thought the first time the container gets created it uses the content of the original dir specified in the volume but this is not the case.
So how can I get the files into this folder?
Do I need to create an extra folder and copy it with a runscript (I hope not)?
Whatever you put in your Dockerfile is just evaluated at build time (and not when you are creating a new container).
If you want to make file from the host available in your container use a data volume:
docker run -v /host_dir:/container_dir ...
In case you just want to copy files from the host to a container as a one-off operation you can use:
docker cp /host_dir mycontainer:/container_dir
The issue is with your ADD statement. Also you might not understand how volumes are accessed. Compare your efforts with the demo below:
FROM alpine #, or your favorite tiny image
ADD src/files /var/www/html/files
VOLUME /var/www/html/files
Build an image called 'dataimg':
docker build -t dataimg .
Use the dataimg image to create a data container named 'datacon':
docker run --name datacon dataimg /bin/cat
Mount the volume from datacon in your nginx container:
docker run --volumes-from datacon nginx ls -la /var/www/html/files
And you'll see the listing of /var/www/html/files reflects the contents of src/files

How to copy multiple files into a docker data volume

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.

Resources