How to mount a volume on docker container - docker-volume

Why is this simple docker command is not mapping volume for me?
docker run -dit -v ./home:/home ubuntu bash
i have also tried this command and it also didn't work.
docker run -dit -v ./home:/var/www/html linode/lamp bash
i have seen others mapping volumes like this but it is not working for me. i'm running docker on windows 10 desktop. do i need to create 'home' foler or this command will also create 'home' folder for me.? please help. thank you

Related

Understanding a Docker .Sh file

The .sh file I am working with is:
docker run -d --rm -it --gpus '"device=0,1,2,3"' --ipc=host -v $HOME/Folder:/Folder tr_xl_container nohup python /Path/file.py -p/Path/ |& tee $HOME/Path/log.txt
I am confused about the -v and everything after that. Specifically, the -v $HOME/Folder:/Folder tr_xl_container section and -p/Path/. If someone would be able to help breakdown what those commands mean or point me to a reference that does, that would be very much appreciated. I checked Docker documentation and Linux command line documentation and did not come up with anything too helpful.
A docker run command is split up in 3 parts:
docker options
the image to run
a command for the container
In your case -d --rm -it --gpus '"device=0,1,2,3"' --ipc=host -v $HOME/Folder:/Folder are docker options.
tr_xl_container is the image name.
nohup python /Path/file.py -p/Path/ is the command sent to the container.
The last part, |& tee $HOME/Path/log.txt isn't run in the container, but takes the output from the docker run command and saves it in $HOME/Path/log.txt.
As for -v $HOME/Folder:/Folder, it's a volume mapping or more precisely, a bind mount. It creates a directory in the container with the path /Folder that is linked to the directory $Home/Folder on the host machine. That makes files in the host directory visible inside the container and if the container does anything with files in the /Folder directory, those changes will be visible in the host directory.
The command after the image name is for the container and it's up to the container what to do with it. From looking at it, it looks like it runs a Python program stored in /Path/file.py in the image. But to be sure, you'll need to know what the image does.

Issue in saving file from docker container to host

I am using below command to save files generated from Docker container to host machine. But my files are not being saved after I exit the container. I tried different ways but none is working.
docker run --rm -it -v "$(pwd)/sever-data/src:/data" test bash
Thank you
I tried the following command to save container data to the host directory and its working perfectly fine.
sudo docker run --rm -it -v $(pwd):/sever-data/src -w /sever-data/src

Plone on Docker always starts from scratch

I'm trying to develope Plone project with Docker, i have used this official image of Plone 5.2.0, the images is built a run perfectly with:
$ docker build -t plone-5.2.0-official-img .
$ docker run -p 8080:8080 -it plone-5.2.0-official-cntr
But the plone restarts each time i run the docker container asking to create the project from skratch.
Anybody could help me with this.
Thanks in advance.
You can also use a volume for data like:
$ docker run -p 8080:8080 -it -v plone-data:/data plone-5.2.0-official-cntr
The next time you'll run a new container it will re-use previous data.
If this helps,
Volumes are the docker way to persist data. You can read it up over here
When running the container just add a -v option and specify your path to store your data.
$ docker run -p "port:port" -it -v "path"
This is expected behavior, because docker run starts a new container, which doesn't have the state from your previous container.
You can use docker start CONTAINER, which will have the state from that CONTAINER's setup
https://docs.docker.com/engine/reference/commandline/start/
A more common approach is to use docker-compose.yml and docker-compose up -d, which will, in most cases, reuse previous state.
https://docs.docker.com/compose/gettingstarted/

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 host volume Windows

I'm using Windows 10 and Docker version 18.03.1-ce, build 9ee9f40.
I'm trying to do docker run -it --volume="./Users:/app" ubuntu:16.04 or docker run -it -v Users:/app ubuntu:16.04 but it's not working, my directory in Docker still empty.
I have read this post and it doesn't help.
UPD: Solved.
Important step was to go to Docker Settings > Shared Drives and uncheck and check your shared drive again.
Then you can mount your volume like this:
docker run -v /host_mnt/c/Users/User/Path:/docker/path image
This command should fix it .
docker run -it -v ${PWD}:/app ubuntu:16.04
Not sure what error you got, so I am assuming
your host directory may not be correct.(solution is based on this)
Permission issue.
Please check the permissions of your folders and user permission

Resources