Mounting host directory to container - docker

I am launching a container for my application. But my app needs few config files to login. Files are stored in host directory. How can I mount the host filepath to container?
host directory : /opt/myApp/config
Docker command used currently :
sudo docker run container -d --name myApp-container -p 8090:8080 myApp-image
Please suggest the changes in docker command to achieve this.

You need to use -v/--volume key in such way:
-v <host dir>:<container dir>:ro
In your case it will be:
-v /opt/myApp/config:/opt/myApp/config:ro
You can use this key multiple times. You can also drop :ro part if you want directory to be writable.
See Docker documentation on volumes.

Related

Docker Windows Bind Mount not Copying and Refreshing Data

I'm using Docker on Windows. Versions are engine: 20.10.14, desktop: 4.7.0. In my current director, I have a DockerFile (unimportant for now) and an index.html file.
I created an nginx docker container with a bind mount to copy these files into the container: docker container run -d --name nginx_cust -p 80:80 -v %cd%:/usr/share/nginx/html nginx.
When I access localhost:80, I don't see my index.html file reflected, and when I enter the running container with bash docker container exec -it nginx_cust bash and check the mounted directory, it's empty:
Inspecting the container, I see that the bind mount does look correct,
and I don't see anything in the container log about this. Any ideas why this is not working?
After a lot of playing around, I realized that this got fixed when I moved the input files to different directory - one that was less-deeply nested. I strongly suspect there was some long filename constraint being violated silently.

Empty host volume in docker container

I am using Docker for Windows, and wanted to mount a host directory with files I would want to use in RStudio in a container for a bioconductor image. To mount the host directory I have used
docker run -d -v //c/Users/myR:/home/rstudio/myR2 -e PASSWORD=password -p 8787:8787 bioconductor/bioconductor_docker:devel
When I open the RStudio interface in the web browser I can see the directory myR2 is created, but it is empty. I have read that I should first share the host directory from Settings > Share folders, but i do not see this option in the Docker version I use (4.5.1). Any help? Thanks!

When using docker option --mount the target folder is seen as not absolute, while there is no issue when using -v

I am playing around with docker and ran into an issue when mounting docker volumes with --mount instead of -v. It appears to me that the error popping up is not valid, but probably I am missing a small detail here.
The path to which I want bind the created image in the container is seen as not absolute in the --mount scenario.
I am running Docker on a windows 10 machine
I pulled the jenkins/jenkins:lts image and want to spin up 2 containers that use the same configuration. As said before I use this just to play around with docker, and am exploring how the volume system works.
What i did is create a docker volume that is used to share the configuarion.
docker volume create jenkins_cfg
Then I tried to run 2 containers. The first container started with:
docker run -d -p 8081:8080 --name jenkins2 -v jenkins_cfg:/var/jenkins_home jenkins/jenkins:lts
Which works fine..
The second container started with:
docker run -d -p 8085:8080 --name jenkin5 --mount source=jenkins_cfg,target=var/jenkins_home jenkins/jenkins:lts
This results in the error
"C:\Program Files\Docker\Docker\Resources\bin\docker.exe: Error response from daemon: invalid mount config for type "volume": invalid mount path: 'var/jenkins_home' mount path must be absolute.
See 'C:\Program Files\Docker\Docker\Resources\bin\docker.exe run --help'."
Also /var/jenkins_home is not working properly.
While the -v also asks for the same target folder , i would assume that this folder would also work in the target option of --mount. Probably, I am overlooking something here ...
I figured out that the target folder should be preceeded by //
so the docker command would look like
docker run -d -p 8085:8080 --name jenkin5 --mount source=jenkins_cfg,target=//var/jenkins_home jenkins/jenkins:lts
Still no clue why // has to be added, maybe someone can clarify on that one
Actually mount binds are like mounting a part of physical disk volume to the containers. But volumes are like virtual memory you can't access them independently without containers but bind mounts can be accessed independently
Your mount binds should be an absolute path in your host
Hope this helps your cause

Docker for Mac: Using Persistent Storage

I have recently discovered Docker for Mac (version 1.13.1). I am trying to work out how to use persistent storage.
What is the correct syntax for creating and using persistent storage?
I would suggest you read Manage data in containers.
One option is to mount a host directory:
docker run -d -P --name web -v /src/webapp:/webapp training/webapp python app.py
This would mount the directory /src/webapp located on your host to directory /webapp in the container.

Understanding what host means in Docker

I was just trying to experiment around with some docker commands, particularly the -v command. I see the below command in the documentation for docker.
$ docker run -d -P --name web -v /src/webapp:/opt/webapp training/webapp python app.py
The following explanation is provided:
This will mount the host directory, /src/webapp, into the container at
/opt/webapp.
I fail to understand the initial part , I.E. This will mount the host directory, /src/webapp, , what does host mean in this scenario/context ? can somebody explain ? I am having a real hard time understanding what host mean , can anybody explain ?
The Documentation can be found HERE
"Host" generally means "the physical computer on which you are running Docker" (or other virtualization service).
In your example, -v /src/webapp:/opt/webapp will expose the /src/webapp directory on the computer running Docker inside the web container as the directory /opt/webapp.

Resources