Start a Docker volume and give it a name - docker

I want to know how to start a Docker container with a named volume. I've tried this
docker run -it --name container1 -v path:path --name volumename image bin/bash
But the container was also named "volumename"
How can I resolve this issue?

First of all, if you don't have an existing image then you have to create one by using:
docker volume create --name [volume name]
For instance:
docker volume create --name namedvolume
If you did this you can check if it has been created. Just type:
docker volume ls
You did all right, if it shows your created volume.
Last step:
docker run -v [volume name]:[container directory]
With a directory:
docker run -it --name container -v data-volume:/data image /bin/bash

Related

How to get inside docker container to see the mounted volume?

I am trying to buld a simple docker file that has a debian image.
Also, I want to mount my local volume inside the docker container.
The problem I have is that how do I get inside the container to see the volume mounted.
$docker run -d -it bash --mount type=bind,source="$(pwd)",target=/app docker_test:latest
43db16a76d50f1da0f8589c9ec460080ccef40122c9bc54abad3230dbbfe7885
I believe this 43db16a.. is container id. Even I try to attach to this container id I get an an error message. It says you cannot attach to the stop container. What am I missing here.
It works if I do
docker run -d -it --name test_docker1 --mount type=bind,source="$(pwd)"/,target=/app docker_test:latest
and then
docker attach
d6bd3cc6dc667e742d0bb3c7fbec58935046c1bf7a2e0b6806d48817082c05be
Also, it works when I do
$docker run --rm -ti --mount type=bind,source="$(pwd)"/,target=/app docker_test:latest
In another terminal do a docker ps, then look for the image you are looking for and copy the id, then do a docker exec -ti <your-image> bash there you have a bash terminal inside the container and you can check the mounted volume.

Docker shared volumes create and attach problem

I've a question about docker shared volumes.
I know that if I run a container with -v option I create a volume that I can share to another container with --volumes-from:
docker run -d -v DataVolume1:/datavolume1 --name container1 image1:v1.0.0
docker run --name container2 --volumes-from container1 image2:v1.0.0
But I cannot understand the full behaviour of this. It seems like volume from container 1 is a master and the same volume in container 2 is a slave. So container 2 can write and container 1 read or only the opposite?
Why I cannot use -v option on all my container like that?
docker run -d -v DataVolume1:/datavolume1 --name container1 image1:v1.0.0
docker run -d -v DataVolume1:/datavolume1 --name container2 image2:v1.0.0
or create a volume with:
docker volume create --name DataVolume1
and then attach to the two container with:
docker run -d -v DataVolume1:/datavolume1 --name container1 image1:v1.0.0
docker run -d -v DataVolume1:/datavolume1 --name container2 image2:v1.0.0
Is there some trouble because each -v recreate the volume and cut the link with the previous container? Or something other?
Because with two "docker run -v" I could also specify different mounting path for the same volume, so if it work for me is better, but I never see anyone use this way so what's the problem?
Thanks in advance!

docker volume during docker run

I am trying to mount library present in the container into docker volume during docker run . The command is as below:
docker run -d --name mbus-docker -it --rm --mount source=/mbus/lib/libMurata.a,target=/mbus_volume mbus-docker
I have verified by execing into the container that the library is present in path /mbus/lib/libMurata.a
When I try to mount the library on to volume.
I am getting the below error:
docker: Error response from daemon: create /mbus/lib: "/mbus/lib" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.
If you want to mount /mbus/lib/libMurata.a onto /mbus_volume path inside container then specify the type for mount as bind.
Your docker run command should be
docker run -d --name mbus-docker -it --rm --mount type=bind,source=/mbus/lib/libMurata.a,target=/mbus_volume/ mbus-docker
This will mount /mbus/lib/libMurata.a onto /mbus_volume/ folder.
The error you got "/mbus/lib" includes invalid characters for a local volume name says /mbus/lib is invalid volume name. Because the default bind type for mount option is type volume. In this case it will try to create a volume locally on your system with the name /mbus/lib which is an invalid volume name.
Please go through this.
Hope this helps.
Update:
If volume named mbus_volume exists on your host. Then try this:
docker run -d --name mbus-docker -it --rm --mount type=volume,source=mbus_volume,target=/mbus/lib/ mbus-docker
you can just use:
docker run -d --name mbus-docker -it --rm -v /mbus/lib/libMurata.a:/mbus_volume/libMurata.a mbus-docker

How to properly share a folder between few docker containers in read mode?

I have Docker installed in top of a CentOS system.
I tried to use volume but each new container is deleting (or hidding) the content of the folder to share.
My volume is always empty after a Docker run.
In order to create my containers, I use
docker run -dit --name $CONTAINER_NAME -p $PORT:8080 \
-v $VOLUME_PATH:/opt/conf/ \
$IMAGE_NAME
I aim at sharing a folder from the host between few Docker containers (to READ) AND I want also to write into this folder from the host.
What is an elegant way to do that ?
One solution is to use Data Volume Containers.
First, create a data volume container
docker run -d --name <data-volume-name> -v /<data-volume-name> ubuntu
You can add any data you want in this container.
Create your containers that will share by using the option volume-from
Let's create container foo and container bar using the shared datacontainer :
docker run -it --name foo --volumes-from=<data-volume-name> ubuntu
docker run -it --name bar --volumes-from=<data-volume-name> centos
Enjoy yourself
Each container in my example is mapped to the root folder.
From either bar or foo you can see /, in the filesytem.
You can also use volume field.
Create a volume
docker volume create --name <volume-name>
Create containers foo and bar that witl be mapped to the volume
docker run -dit --name foo -v test-volume:/path/in/container/ <image-name>
docker run -dit --name bar -v test-volume:/path/in/container/ <image-name>
Each container that will write in the volume will be visible by other.

How to add directory to existing Data container in Docker?

I have made a data container in Docker with the directory /tmp:
sudo docker create -v /tmp --name datacontainer ubuntu
I will add another directory to this existing data container like /opt.
How can i do this?
You cannot add a new data volume to an existing (created or running) container.
With docker 1.9+, you would use instead docker volume create:
docker volume create --name my-tmp
docker volume create --name my-opt
Then you can mount those volumes to any container you want (when you run those containers, not when they are already running)
docker run -d -P \
-v my-tmp:/tmp \
-v my-opt:/opt \
--name mycontainer myimage

Resources