Docker mount not putting files to the hosts directory - docker

I have created a mount on my container which maps a physical path on the server to a path within the docker container. However, when files are placed within the containers path, those files are not appearing on the servers path (and vice versa)
Here is my docker run cmd:
docker run -d -p 127.0.0.1:7001:5000 --name myContainer myContainer -v /var/www/Images:/app/wwwroot/
Server is running CentOS. My application that runs within this docker container places files in the app/wwwroot folder within its container. I expected these files to also appear on the servers /var/www/Images folder but they do not.
Any ideas why?
Thanks

I expected these files to also appear on the servers /var/www/Images
folder but they do not.
You map mount a directory or path /app/wwwroot will be overridden (hide) by the host files, as -v option tells to the docker I am going to override anything inside Docker with host files.
When you use a bind mount, a file or directory on the host machine is mounted into a container. The file or directory is referenced by its full or relative path on the host machine.
bind-mounts
Or if you expect to copy from container then one way is to start container
docker run -it --rm --name test my_container
then copy files from container
docker cp my_container:/app/wwwroot/ /var/www/Images
Now bind you have docker files under /var/www/Images.

Related

How to mount docker container with host to view the config of container in mounted host folder?

I have docker image that runs the products within it successfully with a given set of configuration in the config folder of the container. Now the team would like to view the config folder/files within the container outside of the container i.e. in the host folder . I use the command
docker run -p 8080:8080
-v /opt/docker/config:/opt/components/DISTRIBUTOR/config \
--name distributor --env-file file.env -d distributor
Expected : The host folder is empty initially before running the above command and I am expecting the content of config folder of the container to be displayed in the host folder after the execution of the above command.
Actual : On running of the above command, the config folder in the container becomes empty as the host folder and fails to start the server.
How to mount the volume to view folder/files from container to host (even if in Read only mode) without impacting the files/folders in the container ? Host to container works fine, but that's not what I am looking at.

ubuntu 16.04: docker not syncing its directory with the host's directory

I am trying to run a docker container. This docker container contains a set of files inside a directory /mnt. I want this directory to be automatically copied to the host's machine /mnt directory, upon the creation of the container, and anything that will be changed in the container's /mnt directory should also be changed on the host's directory.
I am running this command:
docker run -d --restart always -v /mnt:/mnt <image name>
What is happening is that, instead of the container's directory being copied to the host, the opposite is happening. The host's empty /mnt directory is being copied and overriding the container's /mnt directory.
Why is this happening? And how can I solve it?
The correct way would be for you to design the container in a way that it did not create the data in the container, but used the mount /mnt instead.
But if you must, a standard volume may do what you want:
docker volume create hostmnt
docker run -d --restart always -v hostmnt:/mnt <image name>
After that you can do a shell script to copy or map the volume to wtv directory you want.
But my suggestion would be to docker cp the content, and redirect all data that need being saved to the host or to a data environment, if possible.

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|-

Bind-mount a host directory into a volume of a running docker container

Let's say that I start a docker container with a bind-mounted local folder:
docker run --rm -v /ux1/dmtest:/data -it ubuntu
Then, locally - not inside the container, I bind-mount a directory from another fs into /ux1/dmtest:
mkdir /ux1/dmtest/bm
mount --bind /ux0/bm /ux1/dmtest/bm
Now, from the container, I see /data/bm/ and I can write content to it, but this content will not be visible on the host on /ux0/bm.
Where is this content stored?
And is there any way to mount additional storage into a running docker container (this workaround clearly doesn't work)?
Mounts done after the fact won't be seen by the container due to mount namespaces that Docker uses. The files will be in the /ux1/dmtest directory that was in place before your second bind mount.
If you do want to use a bind mount, put it in place, and then start the docker daemon, and then your container will see it.

How to mount a directory in a Docker container to the host?

Assume that i have an application with this simple Dockerfile:
//...
RUN configure.sh --logmyfiles /var/lib/myapp
ENTRYPOINT ["starter.sh"]
CMD ["run"]
EXPOSE 8080
VOLUME ["/var/lib/myapp"]
And I run a container from that:
sudo docker run -d --name myapp -p 8080:8080 myapp:latest
So it works properly and stores some logs in /var/lib/myapp of docker container.
My question
I need these log files to automatically saved in host too, So how can i mount the /var/lib/myapp from the container to the /var/lib/myapp in host server (without removing current container) ?
Edit
I also see Docker - Mount Directory From Container to Host, but it doesn't solve my problem i need a way to backup my files from docker to host.
First, a little information about Docker volumes. Volume mounts occur only at container creation time. That means you cannot change volume mounts after you've started the container. Also, volume mounts are one-way only: From the host to the container, and not vice-versa. When you specify a host directory mounted as a volume in your container (for example something like: docker run -d --name="foo" -v "/path/on/host:/path/on/container" ubuntu), it is a "regular ole" linux mount --bind, which means that the host directory will temporarily "override" the container directory. Nothing is actually deleted or overwritten on the destination directory, but because of the nature of containers, that effectively means it will be overridden for the lifetime of the container.
So, you're left with two options (maybe three). You could mount a host directory into your container and then copy those files in your startup script (or if you bring cron into your container, you could use a cron to periodically copy those files to that host directory volume mount).
You could also use docker cp to move files from your container to your host. Now that is kinda hacky and definitely not something you should use in your infrastructure automation. But it does work very well for that exact purpose. One-off or debugging is a great situation for that.
You could also possibly set up a network transfer, but that's pretty involved for what you're doing. However, if you want to do this regularly for your log files (or whatever), you could look into using something like rsyslog to move those files off your container.
So how can i mount the /var/lib/myapp from the container to the /var/lib/myapp in host server
That is the opposite: you can mount an host folder to your container on docker run.
(without removing current container)
I don't think so.
Right now, you can check docker inspect <containername> and see if you see your log in the /var/lib/docker/volumes/... associated to the volume from your container.
Or you can redirect the result of docker logs <containername> to an host file.
For more example, see this gist.
The alternative would be to mount a host directory as the log folder and then access the log files directly on the host.
me#host~$ docker run -d -p 80:80 -v <sites-enabled-dir>:/etc/nginx/sites-enabled -v <certs-dir>:/etc/nginx/certs -v <log-dir>:/var/log/nginx dockerfile/nginx
me#host~$ ls <log-dir>
(again, that apply to a container that you start, not an existing running one)

Resources