Cannot bind on top of a volume in portainer - docker

I added an NFS4 volume which works fine. Now in one of my containers, I want to access a sub directory in the volume.
Is it possible to add a bind on top of the volume in the config for example:
(volume) container: /ext volume: external_drive
(bind) container: /config volume: /ext/config
I tried it and doesn't seem to work. Do I have to create a volume for each sub directory?
Binding a sub directory from a volume without having to create a volume for each sub directory.

Related

Copy data from docker-compose auto-created volume to new external volume

When you use a volume in docker-compose, docker will create a volume with the volume name appended to the directory name: [directory containing yml]_[volume name]
/var/containers/my-important-server-v0.23/docker-compose.yml
volumes:
server-db:
# Volume name: my-important-server-v0.23_server-db
Now when you move and rename the directory containing docker-compose.yml for reasons, it will create a new volume with the new directory prepended to the volume name.
In order to prevent this, and allow multiple compose files to use the same volume, we should have created a volume manually:
docker volume create server-db
volumes:
server-db:
external: true
How can we transfer the files from my-important-server-v0.23_server-db to server-db?
This is what I tried and seemed to make sense, but it doesn't work as expected.
docker volume create server-db
OLD=$(docker volume inspect my-important-server-v0.23_server-db | jq -r .[0].Mountpoint)
NEW=$(docker volume inspect server-db | jq -r .[0].Mountpoint)
sudo rsync -va $OLD/ $NEW/
Now here is the problem. The directory containing MariaDB files have different sizes. Apparently you can't simply copy the files like that.

How to write to my filesystem from docker container

I need to save incoming images, I decided to save it in my filesystem, but unfortunately, it is unavailable from a docker container, I am about to save images inside the home directory
I am using golang and this the path where I am saving
home, _ := os.UserHomeDir()
dir := home + "/Desktop/5" (root/Desktop/5)
how can I save images inside my filesystem not inside the container
I have tried to add volume inside my docker-compose file
volumes:
#path inside container
- /root
# path in my filesystem to map
- $HOME/Desktop/root
but it doesn't work
You have to mount a directory on your host to the docker container:
volumes:
- "/home/myname/root:/root"
With this volume definition in docker-compose, whatever the container writes under /root in the container should show up in your /home/myname/root directory.

Docker-compose and volumes

When I create a volume manually and include it in docker-compose, if I don't prefix the volume tag with docker_, docker compose creates a new volume prefixed with docker_
For example:
I create a volume with:
docker volume create myvolume
It's visible at /var/lib/docker/volumes/myvolume.
I include it in my docker-compose yaml file, but when I run docker-compose, a new volume is created at /var/lib/docker/volumes/docker_myvolume
If I call my volume docker_myvolume and include that in my docker-compose yaml, it uses it and doesn't create it's own.
Is this normal behavior?
Yes, this is normal behavior. When you specify a volume in your docker-compose.yml file without a leading driver_ prefix, Docker Compose will create a new volume with a name that is prefixed with driver_. This is because Docker Compose uses a default driver for creating and managing volumes, which is the local driver.
You can specify a volume in your docker-compose.yml file with the external option to tell Docker Compose to use an existing volume instead of creating a new one. For example:
version: '3'
services:
myservice:
volumes:
- type: volume
source: myvolume
target: /app/data
volume:
external: true
This will tell Docker Compose to use the existing volume myvolume instead of creating a new one.
Yes, Compose generally prefixes things with its project name. This includes containers, networks, and named volumes. In general, if you actually need to interact with these things, there is an equivalent docker-compose command that chooses the correct name (e.g., docker-compose exec).
In general you shouldn't be directly modifying things inside /var/lib/docker. That directory tree is Docker's private state and there are no particular guarantees about the format of files there. If your use case involves directly interacting with the volume files from the host, either use a /host/path:/container/path bind mount or explicitly specify the storage location using volume options.

How to mount Docker directory into host directory with docker-compose

Imagine I have a Docker container containing some static data.
Now for development purpose I want the content of the container directory /resources mounted to my local working directory ..
docker-compose.yml:
version: '3.2'
services:
resources:
image: <private_registry>/resources:latest
volumes:
- ./resources:/resources
When running docker-compose up the folder resources is created in my working directory, but it has no content, whereas the container has content in /resources/
When using a named volume and inspecting it, it works like expected.
Docker provides initialization of the the volume source to the content of your image in a specific scenario:
It must be a named volume, not a host volume (mapping a path into the container)
The volume source must be empty, once there is data inside the directory it will not be changed by docker
Only on creation of the container (while the container is running it won't reinitialize the folder)
The option to disable the copy has not been set (this is the "nocopy" option in the compose file).
You are currently stuck at the first requirement, but it is possible to map any folder from the host into the container using a named volume that performs a bind mount. Here are some examples of three different ways to do that:
# create the volume in advance
$ docker volume create --driver local \
--opt type=none \
--opt device=/home/user/test \
--opt o=bind \
test_vol
# create on the fly with --mount
$ docker run -it --rm \
--mount type=volume,dst=/container/path,volume-driver=local,volume-opt=type=none,volume-opt=o=bind,volume-opt=device=/home/user/test \
foo
# inside a docker-compose file
...
volumes:
bind-test:
driver: local
driver_opts:
type: none
o: bind
device: /home/user/test
...
Your example would look more like:
version: '3.2'
services:
resources:
image: <private_registry>/resources:latest
volumes:
- resources:/resources
volumes:
resources:
driver: local
driver_opts:
type: none
o: bind
device: /full/path/to/resources
Note that this directory must exist on the host in advance. The bind mount will fail without it, and unlike a host mount, docker will not create it for you.
There are a couple of things here. First, when you mount a host directory it 'shades' any existing content on the given path, effectively replacing it with the contents of the mount. So, your resources directory on your host is hiding any content in your container.
There is no easy solution to your problem. When I want to edit files in a container and on the host, I keep the files on the host and mount them in the container. If I want a copy of a container, I mount a host dir to a different dir in the container and arrange for the files to be copied.

Docker volume is empty for jenkins compose file, not copying any files

I am trying to create a jenkins and nexus integration using docker compose file. Where in my jenkins updated with few plugins using Dockerfile and volume created under /var/lib/jenkins/.
VOLUME ["/var/lib/jenkins/"]
in compose file am trying to map my volume to local store /opt/jenkins/
jenkins:
build: ./jenkins
ports:
- 9090:8080
volumes:
- /opt/jenkins/:/var/lib/jenkins/
But Nothing is copying to my persistence directory(/opt/jenkins/).
I can see in all my jenkins jobs created under _data/jobs/ directory under some volume. not in my volume defined /var/lib/jenkins/
Can any one help me on this why this is happening?
From the documentation:
Volumes are initialized when a container is created. If the container’s base image contains data at the specified mount point, that existing data is copied into the new volume upon volume initialization. (Note that this does not apply when mounting a host directory.)
And in the mount a host directory as data volume:
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.
So basically you are overlaying (hiding) anything that was in var/lib/jenkins. Can your image function if those things are hidden?

Resources