How to run a docker containter linked to a previously created volume - docker

I have a named volume with stuff in it.
I would like to provide this volume as I provide a path: docker run -v /host:/path.in.docker.container - this works for paths. I'd like to do the same with a volume I manually created and filled.
I know about --volumes-from, but how do i first connect the volume to the empty container.

You can create a volume thanks to docker create, see the documentation, then, mount this volume-container with the option --volume of the command docker run as in docker run -v volumename:/data -it my_image.

Related

Strange issue about applying docker named volume for new container

As I understand from all the information I found, the docker host volume can be created from three ways:
1- by ignoring the host-path (it will automatically create a directory with random ID)
2- by specify the host-path (it will also automatically create a directory with random ID)
3- by named volume and specify to a host-path
So I was trying first 2 ways:
$ docker run --name mongo-docker -v /data/db -p 27017:27017 -d mongo
$ docker run --name mongo-docker2 -v $(pwd)/data/:/data/db -p 27222:27017 -d mongo
And I look at the docker volume list:
$ docker volume ls
DRIVER VOLUME NAME
local 5b829a731245cb7fe3a1f28aca4c4c3c3791105be228182ccb9b2f72319180c8
local fb058e804412fb56b2096e2cb903e3ae73647ef6ca076ad9003708b80f94ffc5
It looks just what I was expected.
But when I tried the last one, by created first a volume:
$ docker volume create mongoVol
mongoVol
$ docker volume ls
DRIVER VOLUME NAME
local mongoVol
and use it for host-volume path, came up like this:
$ docker run --name mongo-docker3 -v mongoVol:/data/db -p 27322:27017 -d mongo
86bea0e52c9f395268665e191edc59f795d07266f17667502c7fa32879a6e021
$ docker volume ls
DRIVER VOLUME NAME
local 0de25c92be504d0a6b9bb9c83aa8a6fe17bf9bc195562314ca49edb1c4cf4377 <=== create a new one for new container?
local mongoVol
Why is this create a new directory for it? Shouldn't it be just the "mongoVol" volume?
I can’t find answers to related questions on any forum, post nor any videos....
The mongo image's Dockerfile has two directories named in a VOLUME statement. You're mounting content on /data/db but not on /data/configdb.
If the Dockerfile declares a directory as a VOLUME and nothing is explicitly mounted there, Docker automatically creates an anonymous volume (your first case). That's what results in the additional volume appearing in the docker volume ls listing.

Docker different volume path for specific container

By default docker uses /var/lib/docker/volumes/ for any started container.
Is there any way to launch a new container and have it consume all the required disk on a different specified path on the host?
Basically have the root volume different.
For a specific container only, the simplest way i think would be to use docker volumes, Create docker volume and then attach the volume to the container. So the process running on the container uses up the share, so this is using the disk you would like to use.
More information on the following webpage,
https://docs.docker.com/storage/volumes/
you can define the volume path.
docker run -it --rm -v PWD$:/MyVolume ubuntu bash
This command will use the current folder where you execute the command from.
In the container you'll find your file under /MyVolume.
jens#DESKTOP:~$ docker run -it --rm -v $PWD:/MyVolume ubuntu bash
root#71969d68099e:/# cd /MyVolume/
root#71969d68099e:/MyVolume# ls
But you can define any path:
docker run -it --rm -v /home/someuser/somevolumepath:/MyVolume ubuntu bash
Almost the same is available in docker compose.
ports:
- "80:8080"
- "443:443"
volumes:
- $HOME/userhome/https_cert:/etc/nginx/certs
Jens

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

Inject configuration into volume before Docker container starts

I am looking for a way to create a Docker volume and put some data on it just before a specific container is started - which needs the configuration on startup.
I do not want to modify the container. I would like to use a vanilla container straight from the Docker Hub.
Any ideas?
Update
I did not mention that all this has to be done in a compose file. If I would do it manually, I could wait for the configuration injecting container to finish.
Absolutely! Just create your volume beforehand, attach it to any container (A base OS like Ubuntu would work great), add your data, and you're good to go!
Create the volume:
docker volume create test_volume
Attach it to an instance where you can add data:
docker run --rm -it --name ubuntu_1 -v test_volume:/app ubuntu /bin/sh
Add some data:
Do this within the container; which you are in from the previous command.
touch /app/my_file
Exit the container:
exit
Attach the volume to your new container:
Of course, replace ubuntu with your real image name.
docker run --rm -it --name ubuntu_2 -v test_volume:/app ubuntu /bin/sh
Verify the data is there:
~> ls app/
my_file

Mounting volumes on Bluemix containers and sharing between them does not work

I've created a volume with
$ cf ic volume create mosquitto_config
This information shows up as expected:
$ cf ic volume list
mosquitto_config
Then, I've created two containers that are based on an image, which contains the VOLUME ["/etc/mosquitto"] line in its Dockerfile, and on which I'm able to log in via SSH:
$ cf ic run -p 22:22 --volume mosquitto_config:/etc/mosquitto --name ssh-test registry.ng.bluemix.net/{reg-name}/{image-name}:latest
$ cf ic run -p 22:22 --volume mosquitto_config:/etc/mosquitto --name ssh-test-2 registry.ng.bluemix.net/{reg-name}/{image-name}:latest
After logging in, I see the mount point /etc/mosquitto as directory on both containers. However, if I create a file in that directory within one container, the new file does not show up in the other container. As far as I understand the volume concept, the new file should show up in the other container. Is it currently not working or how do you set it up correctly?
this kind of way to share volumes I think is not supported by docker.
In order to give a container access to another container’s volumes, you can simply give the –volumes-from argument to docker run. For example:
$ docker run -it -h NEWCONTAINER --volumes-from container-test debian /bin/bash
All the volumes mounted in 'container-test' will be available to 'NEWCONTAINER' (with the same mount options)
It’s important to note that it works even if the container-test is not running: a volume will never be deleted as long as a container is linked to it.
For further help check this url
http://container-solutions.com/understanding-volumes-docker/

Resources