How to update and sync Docker container files using a volume - docker

I'm trying to use a volume to edit the project files using Visual Studio Code from a folder on my desktop to sync with a Docker container. I'm not sure if I'm doing it correctly because my changes aren't being shown in the container, even when I manually restart the container. Are there any additional steps needed or did I reference the "www" folders wrong?
The Docker container has an Ubuntu project with files in the /var/www/ directory.
docker run -it -v /Users/.../Desktop/docker/test2/bh_files:/www -v /www/ -p 8080:8080 k/bh:latest

docker run -it -v /Users/.../Desktop/docker/test2/bh_files:/www -v /www/ -p 8080:8080 k/bh:latest
You are linking your project folder with the /www/ folder inside your container NOT /var/www/. Simply update the path and it should work.
Edit: Change your container volume path as docker run -it -v /Users/.../Desktop/docker/test2/bh_files:/var/www -p 8080:8080 k/bh:latest
I am not really sure that you need the second volume -v /www/. This serves no purpose without a host folder.

Related

Dockerfile : How to mount host directory in container path?

If i have inside my localhost a log folder at:
/var
/logs
apache.logs
elasticsearch.logs
etc...
And i want to mount /var/logs directory of my host, into a path inside a Docker container, like /usr/var/logs/ , how do i do that within a dockerfile ? So each time a log file is updated, it would be accessible within the container too.
Thank you
You can not mount a volumn in Dockerfile
Because:
Dockerfile will build an image, image is independent on each machine host.
Image should be run everywhere on the same platform for example on linux platform it can be running on fedora, centos, ubuntu, redhat...etc
So you just mount volumn in to the container only. because container will be run on specify machine host.
Hope you understand it. Sorry for my bad English.
You can achieve it in two ways - https://docs.docker.com/storage/bind-mounts/
--mount
$ docker run -d -it --name devtest --mount type=bind,source=/var/logs,target=/usr/var/logs image:tag
-v
$ docker run -d -it --name devtest -v /var/logs:/usr/var/logs image:tag
Try -v option of docker run command.
docker run -itd -v /var/logs:/usr/var/logs image-name
This will mount /var/logs directory of host on to /usr/var/logs directory of container.
Hope this helps.
Update:
To mount directory with source and dest in dockerfile make use of this hack (Not 100% sure though).
RUN --mount=target=/usr/var/logs,type=bind,source=/var/logs

docker mount volume dir ubuntu

I'm trying to use docker to do this:
Run Docker image, make sure you mount your User (for MAC) or home (for
Ubuntu) directory as a volume so you can access your local files
The code that I've been given is:
docker run -v /Users/:/host -p 5000:5000 -t -i bjoffe/openface_flask_v2 /bin/bash
I know that the part that I should modify to my local files is -v /Users/:/host, but I am unsure how to do so.
The files I want to load in the container are inside home/user/folder-i-want-to-read
How should this code be written?
Bind mount is just a mapping of the host files or directories into a container files or directories. That basically pointing to the same physical location on disk.
In your case, you could try this command,
docker container run -it -p 5000:5000 -v /home/user/folder-i-want-to-read/:/path_in_container bjoffe/openface_flask_v2 /bin/bash
And, once run verify that directories from the path on host home/user/folder-i-want-to-read are loaded in the container path which you have mapped.

Docker Container Mount Folder

I am trying to mount my VM Machine folder to Container using below command
sudo docker run -d -it --name devtest \
-v /home/minhaj/GOQTINDOOR:/home/user:Z therecipe/qt:linux bash
But do not see any folder on my Container home/user. Please advise what is wrong in my command or do I need to execute more commands to mount folder on Container.
Your issue is that you are running the container in detached mode. Remove -d
sudo docker run -it --name devtest -v /home/minhaj/GOQTINDOOR:/home/user therecipe/qt:linux bash​
After this if you compile something inside the container and copy it is inside the /home/user folder it will be automatically available inside /home/minhaj/GOQTINDOOR. You can copy and delete any file inside /home/minhaj/GOQTINDOOR. But you can't delete the /home/minhaj/GOQTINDOOR folder itself as it the mount point.
Any files or folder inside /home/minhaj/GOQTINDOOR can be deleted from inside the container by delete them from /home/user folder.
docker cp command is only required if you want to copy a file which is not there in any mounted path.
For that you can use
docker cp <containerid>:<pathinsidecontainer> <pathonhost>

Does Docker update contents of volume when mounted if changes are made in Dockerfile?

I have Jenkins running in a Docker container. The home directory is in a host volume, in order to ensure that the build history is preserved when updates to the container are actioned.
I have updated the container, to create an additional file in the home directory. When the new container is pulled, I cannot see the changed file.
ENV JENKINS_HOME=/var/jenkins_home
RUN mkdir -p ${JENKINS_HOME}/.m2
COPY settings.xml ${JENKINS_HOME}/.m2/settings.xml
RUN chown -R jenkins:jenkins ${JENKINS_HOME}/.m2
VOLUME ["/var/jenkins_home"]
I am running the container like this:
docker run -v /host/directory:/var/jenkins_home -p 80:8080 jenkins
I had previous run Jenkins and so the home directory already exists on the host. When I pull the new container and run it, I see that the file .m2/settings.xml is not created. Why is this please?
Basically when you run:
docker run -v /host-src-dir:/container-dest-dir my_image
You will overlay your /container-dest-dir with what is in /host-src-dir
From Docs
$ docker run -d -P --name web -v /src/webapp:/webapp training/webapp python app.py
This command mounts the host directory, /src/webapp, into the
container at /webapp. If the path /webapp already exists inside the
container’s image, the /src/webapp mount overlays but does not remove
the pre-existing content. Once the mount is removed, the content is
accessible again. This is consistent with the expected behavior of the
mount command.
This SO question is also relevant docker mounting volumes on host
It seems you want it the other way around (i.e. the container is source and the host is destination).
Here is a workaround:
Create the volume in your Dockerfile
Run it without -v i.e.: docker run --name=my_container my_image
Run docker inspect --format='{{json .Mounts}}' my_container
This will give you output similar to:
[{"Name":"5e2d41896b9b1b0d7bc0b4ad6dfe3f926c73","Source":"/var/lib/docker/volumes/5e2d41896b9b1b0d7bc0b4ad6dfe3f926c73/_data","Destination":"/var/jenkins_home","Driver":"local","Mode":"","RW":true,"Propagation":""}]
Which means your dir as it is on container was mounted into the host directory /var/lib/docker/volumes/5e2d41896b9b1b0d7bc0b4ad6dfe3f926c73/_data
Unfortunately, I do not know a way to make it mount on a specific host directory instead.

How to access a directory in hosts machine from inside a docker container?

I'm using docker version Docker version 1.9.1, build a34a1d5 on ubuntu:14.04. I have created a docker container with a volume option like this docker run -it -p 80:8080 -v host/folder:container/folder ubuntu:14.04 /bin/bash. host/folder has some files that i'm trying to access from container/folder. The files from host/folder isnt available from container/folder.
when you mount a host folder as a volume with -v you must specify absolute paths, so replace host/folder and container/folder with the absolute path.
Something like docker run -it -p 80:8080 -v /home/uday/host/folder:/container/folder ubuntu:14.04 /bin/bash (of course set the correct path to your host/folder directory)

Resources